2010-09-20 16:21:47 +00:00
|
|
|
/**
|
|
|
|
* @file polygon_test_point_inside.cpp
|
|
|
|
*/
|
2008-11-14 22:40:31 +00:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <vector>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <PolyLine.h>
|
2008-11-14 22:40:31 +00:00
|
|
|
|
|
|
|
/* this algo uses the the Jordan curve theorem to find if a point is inside or outside a polygon:
|
|
|
|
* It run a semi-infinite line horizontally (increasing x, fixed y)
|
|
|
|
* out from the test point, and count how many edges it crosses.
|
|
|
|
* At each crossing, the ray switches between inside and outside.
|
|
|
|
* If odd count, the test point is inside the polygon
|
|
|
|
* This is called the Jordan curve theorem, or sometimes referred to as the "even-odd" test.
|
2010-09-20 16:21:47 +00:00
|
|
|
* Take care to starting and ending points of segments outlines, when the horizontal line crosses a segment outline
|
2008-12-03 10:32:53 +00:00
|
|
|
* exactly on an ending point:
|
|
|
|
* Because the starting point of a segment is also the ending point of the previous, only one must be used.
|
|
|
|
* And we do no use twice the same segment, so we do NOT use both starting and ending points of these 2 segments.
|
|
|
|
* So we must use only one ending point of each segment when calculating intersections
|
2010-10-28 13:02:07 +00:00
|
|
|
* but it cannot be always the starting or the ending point. This depend on relative position of 2 consectutive segments
|
2008-12-03 10:32:53 +00:00
|
|
|
* Here, the ending point above the Y reference position is used
|
|
|
|
* and the ending point below or equal the Y reference position is NOT used
|
|
|
|
* Obviously, others cases are irrelevant because there is not intersection.
|
2008-11-14 22:40:31 +00:00
|
|
|
*/
|
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
#define OUTSIDE false
|
|
|
|
#define INSIDE true
|
2008-11-14 22:40:31 +00:00
|
|
|
|
|
|
|
bool TestPointInsidePolygon( std::vector <CPolyPt> aPolysList,
|
2010-09-20 16:21:47 +00:00
|
|
|
int aIdxstart,
|
|
|
|
int aIdxend,
|
|
|
|
int aRefx,
|
|
|
|
int aRefy)
|
2008-11-14 22:40:31 +00:00
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Function TestPointInsidePolygon
|
2008-11-14 22:40:31 +00:00
|
|
|
* test if a point is inside or outside a polygon.
|
|
|
|
* the polygon must have only lines (not arcs) for outlines.
|
|
|
|
* @param aPolysList: the list of polygons
|
2010-09-20 16:21:47 +00:00
|
|
|
* @param aIdxstart: the starting point of a given polygon in m_FilledPolysList.
|
2010-10-28 13:02:07 +00:00
|
|
|
* @param aIdxend: the ending point of this polygon in m_FilledPolysList.
|
2010-09-20 16:21:47 +00:00
|
|
|
* @param aRefx, aRefy: the point coordinate to test
|
2008-11-14 22:40:31 +00:00
|
|
|
* @return true if the point is inside, false for outside
|
|
|
|
*/
|
|
|
|
{
|
2008-12-03 10:32:53 +00:00
|
|
|
// count intersection points to right of (refx,refy). If odd number, point (refx,refy) is inside polyline
|
|
|
|
int ics, ice;
|
|
|
|
int count = 0;
|
2008-11-14 22:40:31 +00:00
|
|
|
|
|
|
|
// find all intersection points of line with polyline sides
|
2010-09-20 16:21:47 +00:00
|
|
|
for( ics = aIdxstart, ice = aIdxend; ics <= aIdxend; ice = ics++ )
|
|
|
|
{
|
|
|
|
int seg_startX = aPolysList[ics].x;
|
|
|
|
int seg_startY = aPolysList[ics].y;
|
|
|
|
int seg_endX = aPolysList[ice].x;
|
|
|
|
int seg_endY = aPolysList[ice].y;
|
|
|
|
|
|
|
|
/* Trivial cases: skip if ref above or below the segment to test */
|
|
|
|
if( ( seg_startY > aRefy ) && (seg_endY > aRefy ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// segment below ref point, or one of its ends has the same Y pos as the ref point: skip
|
|
|
|
// So we eliminate one end point of 2 consecutive segments.
|
|
|
|
// Note: also we skip horizontal segments if ref point is on this horizontal line
|
|
|
|
// So reference points on horizontal segments outlines always are seen as outside the polygon
|
|
|
|
if( ( seg_startY <= aRefy ) && (seg_endY <= aRefy ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* refy is between seg_startY and seg_endY.
|
|
|
|
* note: here: horizontal segments (seg_startY == seg_endY) are skipped,
|
|
|
|
* either by the first test or by the second test
|
|
|
|
* see if an horizontal semi infinite line from refx is intersecting the segment
|
|
|
|
*/
|
|
|
|
|
|
|
|
// calculate the x position of the intersection of this segment and the semi infinite line
|
|
|
|
// this is more easier if we move the X,Y axis origin to the segment start point:
|
|
|
|
seg_endX -= seg_startX;
|
|
|
|
seg_endY -= seg_startY;
|
|
|
|
double newrefx = (double) (aRefx - seg_startX);
|
|
|
|
double newrefy = (double) (aRefy - seg_startY);
|
|
|
|
|
|
|
|
// Now calculate the x intersection coordinate of the line from (0,0) to (seg_endX,seg_endY)
|
|
|
|
// with the horizontal line at the new refy position
|
|
|
|
// the line slope = seg_endY/seg_endX;
|
|
|
|
// and the x pos relative to the new origin is intersec_x = refy/slope
|
2010-11-13 19:21:16 +00:00
|
|
|
// Note: because horizontal segments are skipped, 1/slope exists (seg_endY never == O)
|
|
|
|
double intersec_x = (newrefy * seg_endX) / seg_endY;
|
2010-09-20 16:21:47 +00:00
|
|
|
if( newrefx < intersec_x ) // Intersection found with the semi-infinite line from refx to infinite
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count & 1 ? INSIDE : OUTSIDE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Function TestPointInsidePolygon (overlaid)
|
|
|
|
* same as previous, but use wxPoint and aCount corners
|
|
|
|
*/
|
|
|
|
bool TestPointInsidePolygon( wxPoint *aPolysList, int aCount,wxPoint aRefPoint )
|
|
|
|
{
|
|
|
|
// count intersection points to right of (refx,refy). If odd number, point (refx,refy) is inside polyline
|
|
|
|
int ics, ice;
|
|
|
|
int count = 0;
|
|
|
|
// find all intersection points of line with polyline sides
|
|
|
|
for( ics = 0, ice = aCount-1; ics < aCount; ice = ics++ )
|
2008-11-14 22:40:31 +00:00
|
|
|
{
|
2008-11-23 16:31:35 +00:00
|
|
|
int seg_startX = aPolysList[ics].x;
|
|
|
|
int seg_startY = aPolysList[ics].y;
|
2008-12-03 10:32:53 +00:00
|
|
|
int seg_endX = aPolysList[ice].x;
|
|
|
|
int seg_endY = aPolysList[ice].y;
|
2008-11-14 22:40:31 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
/* Trivial cases: skip if ref above or below the segment to test */
|
2010-09-20 16:21:47 +00:00
|
|
|
if( ( seg_startY > aRefPoint.y ) && (seg_endY > aRefPoint.y ) )
|
2008-11-23 16:31:35 +00:00
|
|
|
continue;
|
2008-11-14 22:40:31 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
// segment below ref point, or one of its ends has the same Y pos as the ref point: skip
|
|
|
|
// So we eliminate one end point of 2 consecutive segments.
|
|
|
|
// Note: also we skip horizontal segments if ref point is on this horizontal line
|
|
|
|
// So reference points on horizontal segments outlines always are seen as outside the polygon
|
2010-09-20 16:21:47 +00:00
|
|
|
if( ( seg_startY <= aRefPoint.y ) && (seg_endY <= aRefPoint.y ) )
|
2008-11-23 16:31:35 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* refy is between seg_startY and seg_endY.
|
2008-12-03 10:32:53 +00:00
|
|
|
* note: here: horizontal segments (seg_startY == seg_endY) are skipped,
|
|
|
|
* either by the first test or by the second test
|
|
|
|
* see if an horizontal semi infinite line from refx is intersecting the segment
|
|
|
|
*/
|
2008-11-23 16:31:35 +00:00
|
|
|
|
|
|
|
// calculate the x position of the intersection of this segment and the semi infinite line
|
|
|
|
// this is more easier if we move the X,Y axis origin to the segment start point:
|
|
|
|
seg_endX -= seg_startX;
|
|
|
|
seg_endY -= seg_startY;
|
2010-09-20 16:21:47 +00:00
|
|
|
double newrefx = (double) (aRefPoint.x - seg_startX);
|
|
|
|
double newrefy = (double) (aRefPoint.y - seg_startY);
|
2008-12-03 10:32:53 +00:00
|
|
|
|
2008-11-23 16:31:35 +00:00
|
|
|
// Now calculate the x intersection coordinate of the line from (0,0) to (seg_endX,seg_endY)
|
|
|
|
// with the horizontal line at the new refy position
|
2008-12-03 10:32:53 +00:00
|
|
|
// the line slope = seg_endY/seg_endX;
|
2008-11-23 16:31:35 +00:00
|
|
|
// and the x pos relative to the new origin is intersec_x = refy/slope
|
2010-11-13 19:21:16 +00:00
|
|
|
// Note: because horizontal segments are skipped, 1/slope exists (seg_endY never == O)
|
|
|
|
double intersec_x = (newrefy * seg_endX) / seg_endY;
|
2008-12-03 10:32:53 +00:00
|
|
|
if( newrefx < intersec_x ) // Intersection found with the semi-infinite line from refx to infinite
|
|
|
|
count++;
|
2008-11-14 22:40:31 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
return count & 1 ? INSIDE : OUTSIDE;
|
2008-11-14 22:40:31 +00:00
|
|
|
}
|