Point in polygon Algorithm in C Language

Source: Internet
Author: User

Point in polygon Algorithm in C Language


This article uses the shoot method to determine whether a point is inside a polygon.C LanguageProgram. Many years ago, I implementedAlgorithm. But over time, I decided to rewrite this code. By referring to Zhou peide's book computational ry and combining my practices and experience, I believe that the implementation of this algorithm is the best code you have ever encountered.

This is an implementation program of a small Algorithm in C language. I didn't want to put it here. However, when I want to implement such an algorithm myself, I want to find a ready-made algorithm on the Internet, but I don't know if it meets my needs. I have no confidence in the code I wrote when I was studying in college, so I decided to write another one and put it here to help readers.

First, the point structure is defined as follows:

The following is a reference clip:
/* Vertex structure */
Typedef struct
{
Double X, Y;
} Vertex_t;

The polygon referred to in this algorithm refers to a closed simple polygon composed of a series of point sequences. Its first and last points can be or are not the same point (the first and last points are not required to be the same point ). Such a polygon can be of any shape, including multiple edges in an absolute line. Therefore, the polygon structure is defined as follows:

The following is a reference clip:
/* Vertex list structure-polygon */
Typedef struct
{
Int num_vertices;/* Number of vertices in list */
Vertex_t * vertex;/* vertex array pointer */
} Vertexlist_t;

To speed up the identification, first calculate the external rectangle (rect_t) of the polygon and determine whether the vertex falls within the outsourcing rectangle. Only the points that meet the conditions of the external rectangle enter the next calculation step. To this end, the method vertices_get_extent in the outsourcing rectangle of the rect_t structure and the vertex set is introduced. The Code is as follows:

The following is a reference clip:
/* Bounding rectangle type */
Typedef struct
{
Double min_x, min_y, max_x, max_y;
} Rect_t;
/* Gets extent of vertices */
Void vertices_get_extent (const vertex_t * VL, int NP,/* In vertices */
Rect_t * RC/* out extent */)
{
Int I;
If (NP> 0 ){
RC-> min_x = RC-> max_x = VL [0]. X; RC-> min_y = RC-> max_y = VL [0]. Y;
} Else {
RC-> min_x = RC-> min_y = RC-> max_x = RC-> max_y = 0;/* = 0? No vertices at all */
}
For (I = 1; I
{
If (VL [I]. x <RC-> min_x) RC-> min_x = VL [I]. X;
If (VL [I]. Y <RC-> min_y) RC-> min_y = VL [I]. Y;
If (VL [I]. x> RC-> max_x) RC-> max_x = VL [I]. X;
If (VL [I]. Y> RC-> max_y) RC-> max_y = VL [I]. Y;
}
}

When a vertex falls within a polygon enclosed rectangle, it is necessary to determine whether the vertex (v) is within the polygon (Vl: NP. In this program, a Ray B (V, W) is drawn from the test point (v) horizontally, and the number of intersection points between B and VL edge are calculated as C, judging whether a vertex is in a polygon based on the odd interior-even principle (C indicates that V is in VL; otherwise, V is not in VL.

The principle is not detailed. To determine whether there is an intersection between line segments, introduce the following function:

(1) is_same determines whether the two (p, q) points are (1) No (0) on the same side of the line L (l_start, l_end;

(2) is_intersect is used to judge the intersection of two line segments (not a straight line) S1 and S2 (1) No (0;

The following is a reference clip:
/* P, q is on the same of line L */
Static int is_same (const vertex_t * l_start, const vertex_t * l_end,/* line L */
Const vertex_t * P,
Const vertex_t * q)
{
Double dx = l_end-> X-l_start-> X;
Double DY = l_end-> Y-l_start-> Y;
Double dx1 = p-> X-l_start-> X;
Double dy1 = p-> Y-l_start-> Y;
Double dx2 = Q-> X-l_end-> X;
Double dy2 = Q-> Y-l_end-> Y;
Return (dx * dy1-dy * dx1) * (dx * dy2-dy * dx2)> 0? 1: 0 );
}
/* 2 line segments (S1, S2) are intersect? */
Static int is_intersect (const vertex_t * s1_start, const vertex_t * s1_end,
Const vertex_t * s2_start, const vertex_t * s2_end)
{
Return (is_same (s1_start, s1_end, s2_start, s2_end) = 0 &&
Is_same (s2_start, s2_end, s1_start, s1_end) = 0 )? 1: 0;
}

The following function pt_in_poly is used to determine whether the vertex (v) IS (1) No (0) in the polygon (Vl: NP:

The following is a reference clip:
Int pt_in_poly (const vertex_t * VL, int NP,/* polygon VL with NP vertices */
Const vertex_t * V)
{
Int I, j, K1, K2, C;
Rect_t RC;
Vertex_t W;
If (NP <3)
Return 0;
Vertices_get_extent (VL, NP, & rc );
If (V-> x <RC. min_x | V-> x> RC. max_x | V-> Y <RC. min_y | V-> Y> RC. max_y)
Return 0;
/* Set a horizontal beam L (* V, W) from V to the ultra right */
W. x = RC. max_x + dbl_epsilon;
W. Y = V-> Y;
C = 0;/* intersection points counter */
For (I = 0; I
{
J = (I + 1) % NP;
If (is_intersect (VL + I, VL + J, V, & W ))
{
C ++;
}
Else if (VL [I]. Y = W. Y)
{
K1 = (NP + i-1) % NP;
While (K1! = I & VL [k1]. Y = W. Y)
K1 = (NP + k1-1) % NP;
K2 = (I + 1) % NP;
While (K2! = I & VL [k2]. Y = W. Y)
K2 = (k2 + 1) % NP;
If (K1! = K2 & is_same (v, & W, VL + K1, VL + K2) = 0)
C ++;
If (K2 <= I)
Break;
I = k2;
}
}
Return C % 2;
}

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.