简体   繁体   English

如何在线/折线C#上查找相邻点

[英]How to find neighbor Points on Line/Polyline c#

I have a problem with finding neighbor points on a line. 我在寻找一条线上的邻居点时遇到问题。

I have a polyline and a point. 我有一条折线和一点。 The point always stay on the line. 该点始终在线上。 Lines are always straight (not oblique). 线始终是直的(不倾斜)。

I would like to find previous and following points. 我想找到之前和之后的观点。 (on the image these are C and D). (在图像上,这些是C和D)。 What is the best way to do this? 做这个的最好方式是什么? It may be better to find previous point (then, finding the next point will be trivial). 找到上一点可能更好(然后,找到下一点将是微不足道的)。

My idea was use loops and if statements, but maybe there exists a better solution? 我的想法是使用循环和if语句,但也许存在更好的解决方案?

在此处输入图片说明

Here is a naive solution if you can fit your grid into int values. 如果您可以将网格适合int值,这是一个幼稚的解决方案。 It should cover the cases when the line is not "growing" like squared spiral. 它应该涵盖线不像平方螺旋线那样“增长”的情况。 When switching to double, be aware of possible rounding problems. 切换为双精度时,请注意可能的舍入问题。

            var line = new List<Point>
        {
            new Point("A", 0, 0),
            new Point("B", 1, 0),
            new Point("C", 1, 1),
            new Point("D", 3, 1),
            new Point("E", 3, 2),
            new Point("F", 4, 2)
        };

        var p = new Point("P",2,1);

        Point first = null;
        foreach (var point in line)
        {
            if (first != null)
            {
                if (p.X == first.X && p.X == point.X
                    && (p.Y >= first.Y && p.Y <= point.Y
                        || p.Y <= first.Y && p.Y >= point.Y))
                {
                    PrintResult(first, p, point);
                    break;
                }

                if (p.Y == first.Y && p.Y == point.Y
                    && (p.X >= first.X && p.X <= point.X
                        || p.X <= first.X && p.X >= point.X))
                {
                    PrintResult(first, p, point);
                    break;
                }

            }

            first = point;
        }

        Console.ReadKey();
    }

    private static void PrintResult(Point first, Point p, Point second)
    {
        Console.WriteLine(first.Name);
        Console.WriteLine(p.Name);
        Console.WriteLine(second.Name);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM