Polygon LinePolygonIntersection( Polygon poly, float x1, float y1, float x2, float y2 ) { int size = poly->numberOfVertices, i, k; float x3, y3, x4, y4, xInt, yInt; Polygon intersections = NewPolygon(10); for (i=0;i<size;i++) { x3 = poly->vertices[i].x; y3 = poly->vertices[i].y; k = ( i + 1 ) % size; x4 = poly->vertices[k].x; y4 = poly->vertices[k].y; k = LineLineIntersection( x1, y1, x2, y2, x3, y3, x4, y4, &xInt, &yInt ); switch ( k ) { case 2: AddPolygonVertex(intersections,x3,y3); AddPolygonVertex(intersections,x4,y4); break; case 1: AddPolygonVertex(intersections,xInt,yInt); case 0: break; } } return intersections; }
bool PhysicsSystem::InsideConcaveShape(const Vector3* shapePoints, const int numPoints, const Vector3& testPoint){ int intersectionCount = 0; //Counting hom many times we cross the line. for( int i = 0; i < numPoints; ++i){ const int i0 = i; const int i1 = (i+1)%numPoints; const Vector3& p0 = shapePoints[i0]; const Vector3& p1 = shapePoints[i1]; bool intersect = LineLineIntersection(Line_c(p0,p1), Line_c(testPoint, testPoint + Vector3(1000,1000,0))); if (intersect){ intersectionCount++; } } //Even = false; if(intersectionCount % 2 == 0){ return false; } else {return true;} }