public class ConvexPolygon2D{
public Point2D[] Corners;
public ConvexPolygon2D(Point2D[] corners){
Corners = corners;
}
}
public class Point2D{
public double X;
public double Y;
public Point2D(double x, double y){
X = x;
Y = y;
}
}
//THIS FUNCTION IS WHAT I WANT TO DRAW ATTENTION TO
public bool IsPointInsidePoly(Point2D test, ConvexPolygon2D poly){ //So this here doesnt seem to work, either the algorithm is wrong or my input is wrong, this is what I want to figure out with your help.
int i;
int j;
bool result = false;
for (i = 0, j = poly.Corners.Length - 1; i < poly.Corners.Length; j = i++){
if ((poly.Corners[i].Y > test.Y) != (poly.Corners[j].Y > test.Y) &&
(test.X < (poly.Corners[j].X - poly.Corners[i].X) * (test.Y - poly.Corners[i].Y) / (poly.Corners[j].Y - poly.Corners[i].Y) + poly.Corners[i].X)){
result = !result;
}
}
return result; //ALWAYS RETURNS FALSE -- THIS IS THE PROBLEM
}
private ConvexPolygon2D GetIntersectionOfPolygons(ConvexPolygon2D poly1, ConvexPolygon2D poly2){
List<Point2D> clippedCorners = new List<Point2D>();
//Add the corners of poly1 which are inside poly2
for (int i = 0; i < poly1.Corners.Length; i++){
if (IsPointInsidePoly(poly1.Corners[i], poly2)) //<-- Problem here
AddPoints(clippedCorners, new Point2D[] { poly1.Corners[i] });
}
//Add the corners of poly2 which are inside poly1
for (int i = 0; i < poly2.Corners.Length; i++){
if (IsPointInsidePoly(poly2.Corners[i],poly1)) //<-- Also problem here
AddPoints(clippedCorners, new Point2D[]{ poly2.Corners[i]});
}
//Add the intersection points
for (int i = 0, next = 1; i < poly1.Corners.Length; i++, next = (i + 1 == poly1.Corners.Length) ? 0 : i + 1){
AddPoints(clippedCorners, GetIntersectionPoints(poly1.Corners[i], poly1.Corners[next], poly2));
}
return new ConvexPolygon2D(OrderClockwise(clippedCorners.ToArray()));
}