-
Notifications
You must be signed in to change notification settings - Fork 0
/
pt_in_poly.cpp
37 lines (36 loc) · 1.2 KB
/
pt_in_poly.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define X 0
#define Y 1
//returns true if point is inside of the polygon
bool baryc_tri_based ( double pgon[][2], int numverts, double point[2] )
{
double *vtx0 = pgon[numverts-1];
double *vtx1 = pgon[0] ;
int count = 0;
for (int i = 0;i<numverts;i++) {
double x0X = vtx0[X] - point[X];
double x1X = vtx1[X] - point[X];
//check that point[X] is between vtx0[X] and vtx1[X]
if ( x0X * x1X <= 0 ) {
double a = vtx1[Y] - point[Y];
double b = vtx0[Y] - point[Y];
//c is signed distance from the line
double c = ( x0X * a ) - ( x1X * b );
//check that the point is above the line
if ( x1X * c <= 0 && x0X * c >= 0) {
if ( c > 0 && x0X > 0 && x1X < 0 )
count += 2;
else if ( c < 0 && x0X < 0 && x1X > 0 )
count -= 2;
else if ( c > 0 )
count++;
else if ( c < 0 )
count--;
else if ( x0X != 0 || x1X != 0 || a * b <= 0)
return true;
}
}
vtx0 = vtx1;
vtx1 += 2;
}
return (count==2);
}