确定 (x,y,z) 点是否位于由点数组定义的形状内部
如果我有一个点数组 (x,y,z) 并给出一个点 (x,y,z),我应该使用什么代码来确定该点是否位于数组定义的形状内?
我在这个上画了一个空白...
我正在使用 C#
编辑
感谢大家的回复,从评论中我找到了这个链接 (http://alienryderflex.com/polygon/)很好地解释了这个过程。
谢谢!
仅供参考:
bool pointInPolygon() {
int i, j=polySides-1 ;
boolean oddNodes=NO ;
for (i=0; i<polySides; i++) {
if (polyY[i]<y && polyY[j]>=y
|| polyY[j]<y && polyY[i]>=y) {
if (polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x) {
oddNodes=!oddNodes; }}
j=i; }
return oddNodes; }
它需要一些工作,但这就是它的核心。
再次感谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用已知位于形状外部的点,并检查从该点到给定点的直线是否穿过形状的表面。如果它穿过奇数个曲面,则给定点位于形状内部。
Use a point that you know is outside the shape, and check if the line from that point to the given point passes through the surfaces of the shape. If it passes through an odd number of surfaces, the given point is inside the shape.
根据 Guffa 的回答,确定一条线是否与一个曲面相交比听起来更难。这是其背后的数学原理:直线和平面的交点。您必须采用该基本算法(其中涉及找到每个表面到该点的法线,然后确定法线和直线之间的角度以形成直角三角形,您可以找到该直角三角形的第三个点;WPF 的 Media3D 库具有 Points 函数和使这一切变得更容易的向量),然后确定您找到的点是否在该曲面的边界内与该曲面的平面相交。为此,您可以对该面积 > 的表面进行任何 2D 投影。 0,并执行“多边形中的点”测试,这是您尝试执行的“多面体中的点”测试的 2D 版本。
祝你好运。
Further to Guffa's answer, it's harder than it sounds to determine if a line intersects a surface. Here's the math behind that: Intersection of lines and planes. You have to take that basic algorithm (which involves finding the normal of each surface to that point, then determining the angle between the normal and the line to form a right triangle that you find the third point of; WPF's Media3D library has functions on Points and Vectors that make all this easier), then determine if the point you found intersects the plane of the surface within the bounds of that surface. To do THAT, you can take any 2D projection of that surface that has an area > 0, and perform the "point in polygon" test, which is the 2D version of the "point in polyhedron" test you're trying to do.
Good luck.
如果您要在画布上绘制形状,这是一个快速且简单的解决方案。
“多边形”可以是 System.Windows.Shapes 中的任何形状。
If you are drawing Shapes on a Canvas this is a quick and easy Solution.
"Polygon" can be any shape from System.Windows.Shapes.