Editing Point-in-polygon
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 4: | Line 4: | ||
; GW Gourmet Functions Used: [[logZp1oZm1]] | ; GW Gourmet Functions Used: [[logZp1oZm1]] | ||
− | #include < | + | #include <math.h> |
#include <complex> | #include <complex> | ||
#include <iostream> | #include <iostream> | ||
Line 21: | Line 21: | ||
bool IsInsidePolygon(const cmplex &z, const cmplex *zv, const int N) | bool IsInsidePolygon(const cmplex &z, const cmplex *zv, const int N) | ||
{ | { | ||
− | bool close | + | double y(z.imag()); |
− | if ( | + | double x(z.real()); |
− | + | int i; | |
+ | bool close(false); | ||
+ | if (abs(zv[0]-zv[N])>REALSMALL){ | ||
+ | cout<<"PolyIsInside : Bad polygon"<<endl; | ||
exit(0); | exit(0); | ||
} | } | ||
//check if point is close to boundary of polygon | //check if point is close to boundary of polygon | ||
− | for ( | + | for (i=0; i<N; i++){ |
− | if ((( | + | if (((abs(z-zv[i])+abs(z-zv[i+1]))/(abs(zv[i+1]-zv[i])))<CLOSE_TO_LINE){ |
close=true;break; | close=true;break; | ||
} | } | ||
Line 34: | Line 37: | ||
//if not close, use faster ray-tracing algorithm | //if not close, use faster ray-tracing algorithm | ||
//adapted from Wm. Randolph Franklin <wrf@ecse.rpi.edu> | //adapted from Wm. Randolph Franklin <wrf@ecse.rpi.edu> | ||
− | |||
− | |||
if (!close) | if (!close) | ||
{ | { | ||
− | bool in | + | bool in(false); |
− | for ( | + | int j; |
+ | for (i=0, j=N-1; i<N; j=i++) { | ||
if ((((zv[i].imag()<=y) && (y<zv[j].imag())) || | if ((((zv[i].imag()<=y) && (y<zv[j].imag())) || | ||
((zv[j].imag()<=y) && (y<zv[i].imag()))) && | ((zv[j].imag()<=y) && (y<zv[i].imag()))) && | ||
Line 54: | Line 56: | ||
else | else | ||
{ | { | ||
− | double sum | + | cmplex Z; |
+ | double sum(0); | ||
for (int i=0;i<N; i++) | for (int i=0;i<N; i++) | ||
{ | { | ||
− | + | Z=(z-0.5*(zv[i]+zv[i+1]))/(0.5*(zv[i+1]-zv[i])); | |
sum+=logZm1oZp1(Z).imag(); | sum+=logZm1oZp1(Z).imag(); | ||
} | } | ||
− | return | + | return (fabs(sum)<REALSMALL ? false : true); |
} | } | ||
} | } |