【发布时间】:2025-11-28 21:10:01
【问题描述】:
简单的问题 - 找出一个点是否在凸多边形内。 There is algorithm described yet due to be beeng an in Wolfram language 我出了点问题。这就是我所拥有的:
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] );
}
很遗憾,它不起作用...我看了一些refrence implementations here 尝试过它们似乎也不起作用..
我使用的测试数据是针对凸的:
[(262.8, 669.1); (1623.9, 718.2); (200.4, 895.4); (1817.8, 1540.8)]
和(288, 815) 和(1078, 890) 作为测试点。
谁能解释我在该算法/它的实现中出了什么问题?
【问题讨论】:
-
当你说“我使用的测试数据是凸的”时,你的意思是这些点是多边形中的点吗?
标签: c# algorithm mono unity3d .net-3.5