C-language implementation of dot in polygon algorithm

Source: Internet
Author: User

This paper uses the Ray method to determine whether the point is within the Polygon C language program. Years ago, I realized such an algorithm myself. But over time, I decided to rewrite the code. Referring to Zhou Peide's "computational Geometry", combined with my practice and experience, I believe that this is the optimal code you have encountered so far in the implementation of this algorithm.

This is a C-language small algorithm implementation program, would not want to put here. However, when I want to implement such an algorithm, want to find a ready-made on the Internet, the study came down unexpectedly a meet the needs of the. I had no confidence in the code I wrote when I was a college student, so I decided to write a new one and put it here, readers. Also increase the number of clicks Blog.

First, define the point structure as follows:

以下是引用片段:
/* 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 end point can be or not the same point (not forcing the end point is the same point). Such a polygon can be of any shape, including multiple edges on an absolute line. Therefore, the definition of the polygon structure is as follows:

以下是引用片段:
/* Vertex list structure – polygon */
typedef struct
{
int num_vertices; /* Number of vertices in list */
vertex_t *vertex; /* Vertex array pointer */
} vertexlist_t;

In order to speed up the discriminant speed, first compute the bounding rectangle (rect_t) of the polygon, judge whether the point falls in the outer package rectangle, only the point that satisfies the condition in the outer package rectangle, then enters the next calculation. For this reason, the method of outsourcing rectangular structure rect_t and the outsourcing rectangle of the point collection is introduced Vertices_get_extent, the code is as follows:

以下是引用片段:
/* 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;
}
}

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.