Simple problem - find if a point is inside a convex Polygon. There is algorithm described yet due to be beeng an in Wolfram language and I ve got something wrong. This is what I have:
private static bool PointInside2D(Vector2 point, Vector2 lineStart, Vector2 lineEnd) {
var v1 = lineStart - point;
var edge = lineStart - lineEnd;
return !(edge.x * v1.y - edge.y * v1.x < 0);
}
private static bool PointInsideRect2D(Vector2 point, IList<Vector2> rect) {
var lastPoint = rect.Count - 1;
bool? lastResult = null;
for (var i = 0; i < lastPoint; ++i) {
if (lastResult == null) {
lastResult = PointInside2D(point, rect[i], rect[i + 1]);
}
else {
if (lastResult != PointInside2D(point, rect[i], rect[i + 1])) {
return false;
}
}
}
return lastResult == PointInside2D( point, rect[lastPoint], rect[0] );
}
and it does not work sadly... I looked at some refrence implementations here tried them seems also not to work..
test data I use is for convex:
[(262.8, 669.1); (1623.9, 718.2); (200.4, 895.4); (1817.8, 1540.8)]
and (288, 815)
and (1078, 890)
as test points.
Can any one explain what I've got wrong in that algorithm/its implementations?