。
Here's an example of code in C that determines if a point is within the bounds of a polygon on a plane:
#include <stdio.h>
#include <math.h>
#define MAX_POLY_CORNERS 100
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
typedef struct {
double x;
double y;
} Point;
int pnpoly(int nvert, Point *vert, double testx, double testy) {
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((vert[i].y>testy) != (vert[j].y>testy)) &&
(testx < (vert[j].x-vert[i].x) * (testy-vert[i].y) / (vert[j].y-vert[i].y) + vert[i].x) )
c = !c;
}
return c;
}
int main() {
Point vertices[MAX_POLY_CORNERS];
int num_vertices = 0;
double testx, testy;
// Populate vertices array with the coordinates of the polygon's vertices
// ...
// Read in the x and y coordinates of the point to test
scanf("%lf%lf", &testx, &testy);
// Call pnpoly to determine if the point is within the polygon
int result = pnpoly(num_vertices, vertices, testx, testy);
if (result) {
printf("The point is within the polygon.\n");
} else {
printf("The point is not within the polygon.\n");
}
return 0;
}