确定 (x,y,z) 点是否位于由点数组定义的形状内部

发布于 2024-10-16 21:14:50 字数 802 浏览 10 评论 0 原文

如果我有一个点数组 (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; }

它需要一些工作,但这就是它的核心。

再次感谢

If I have an array of points (x,y,z) and am given a single point (x,y,z), what code do I use to determine if that point resides within the shape defined by the array?

I am drawing a blank on this one...

I'm using C#

EDIT

Thanks for the responses guys, from the comments I have found this link (http://alienryderflex.com/polygon/) which explains the process quite well.

Thanks!

FYI:

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; }

It'll need some work, but thats the guts of it.

Thanks again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

善良天后 2024-10-23 21:14:50

使用已知位于形状外部的点,并检查从该点到给定点的直线是否穿过形状的表面。如果它穿过奇数个曲面,则给定点位于形状内部。

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.

长安忆 2024-10-23 21:14:50

根据 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.

请叫√我孤独 2024-10-23 21:14:50

如果您要在画布上绘制形状,这是一个快速且简单的解决方案。

    private void Canvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.OriginalSource is Polygon)
        {
            //do something
        }
    }

“多边形”可以是 System.Windows.Shapes 中的任何形状。

If you are drawing Shapes on a Canvas this is a quick and easy Solution.

    private void Canvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.OriginalSource is Polygon)
        {
            //do something
        }
    }

"Polygon" can be any shape from System.Windows.Shapes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文