检测2条线的交点

发布于 2024-08-20 13:41:27 字数 1042 浏览 6 评论 0原文

我的表单上有 2 个 (VisualBasic.PowerPacks)LineShapes:

alt text http://lh4.ggpht.com/_1TPOP7DzY1E/S2cIJan7eHI/AAAAAAAADAw/qwA0jFHEbBM/s800/intersection.png

当我单击其中一个时,会出现一个特定的上下文菜单。用户可以移动这些线。上下文菜单与一行相关联。但是,如果用户单击交叉点(如果存在),我需要显示另一个菜单,该菜单将选择一条交叉线来执行操作。

现在,我想知道如何检测 2 条(或更多)条线在点击点相交,因为在这种情况下应该出现另一个上下文菜单。

我尝试做的事情:

    private void shapeContainer1_MouseDown(object sender, MouseEventArgs e)
    {
        // right click only
        if (e.Button == MouseButtons.Right)
        {
            LineShape target = 
                (shapeContainer1.GetChildAtPoint(e.Location) as LineShape);

            if (target != null)
            {
                Console.WriteLine(new Point(target.X1, target.Y1));
            }

        }
    }

我想容器中只有 LineShapes。这表明,如果任何 LineShape 位于鼠标下方,ShapeContainer 将不会引发 MouseDown 事件。

但这段代码只给了我最上面的一行,但我也想要其他行的列表。

I have 2 (VisualBasic.PowerPacks)LineShapes on my form:

alt text http://lh4.ggpht.com/_1TPOP7DzY1E/S2cIJan7eHI/AAAAAAAADAw/qwA0jFHEbBM/s800/intersection.png

When I click on one of them, a specific context menu appears. The lines can be moved by the user. A context Menu is associated with a line. However, if the user clicks in the intersection point(if exists) I need to display an other menu, that will select one of intersection lines to perform an action.

Now, I wonder how to detect that 2 (or more) lines are intersected in the click point, because an other context-menu should appear in this case.

What I tried to do:

    private void shapeContainer1_MouseDown(object sender, MouseEventArgs e)
    {
        // right click only
        if (e.Button == MouseButtons.Right)
        {
            LineShape target = 
                (shapeContainer1.GetChildAtPoint(e.Location) as LineShape);

            if (target != null)
            {
                Console.WriteLine(new Point(target.X1, target.Y1));
            }

        }
    }

I suppose I have only LineShapes in the container. This told, the ShapeContainer will not raise a MouseDown event if any LineShape will be under the mouse.

But this code gives me only the mostTop line, but I want a list of others too.

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

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

发布评论

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

评论(5

我的影子我的梦 2024-08-27 13:41:27

在您的坐标网络中,有两条线:y1 = ax + c1y2 = bx + c2。找到 x1=x2y1=y2 的交点
y = ax + c1,y = bx + c2
ax + c1 = bx + c2
x = (c2 - c1)/(a - b)
然后检查交点是否超出线边界并计算接近度+/-像素或两个。

In your coordinate network, you have two lines with y1 = ax + c1 and y2 = bx + c2. Find the intersection point where x1=x2 and y1=y2
y = ax + c1, y = bx + c2
ax + c1 = bx + c2
x = (c2 - c1)/(a - b)
Then check that the intersection point is not beyond the line borders and calculate proximity +- pixel or two.

要走干脆点 2024-08-27 13:41:27

您只需要计算两条线段的交点。这相当简单。

此处描述了完整的工作算法。它可以离线工作,由两点定义线段,因此应该很容易适应您的情况。

You'll need to just compute the intersection of two line segments. This is fairly simple.

A full, working algorithm is described here. It works off line segments defined by two points, so should be easy to adapt to your situation.

獨角戲 2024-08-27 13:41:27

serhio,这很简单数学...

计算出线条的交点(可能是添加它们并存储结果时执行此操作),然后查看鼠标是否足够近以保证显示上下文菜单,这样您就不需要像素完美点击。

serhio, thats simple Maths...

Work out the intersection points of your lines (probably do this when they are added and store result), then see if the mouse is close enough to warrent displaying the context menu so you don't need pixel perfect clicking.

姐不稀罕 2024-08-27 13:41:27

除了线相交算法(如本页上的几个人所示)之外,您还需要将上下文菜单与线解耦。在伪代码中,您将需要类似的内容:

onLine1Click:
if intersection then handle intersection
else handle line1 click

onLine2Click:
if intersection then handle intersection
else handle line2 click

此处理可以显示上下文菜单。我相信需要使用 if/then/else 来解决您剩下的问题。

Apart from the line intersection algorithm (as shown by several people on this page), you need to decouple the context menu from the lines. In pseudo code you'll need something like:

onLine1Click:
if intersection then handle intersection
else handle line1 click

onLine2Click:
if intersection then handle intersection
else handle line2 click

This handling can be showing the context menu. I believe this if/then/else is required to resolve your remaining issue.

掩耳倾听 2024-08-27 13:41:27
    /// obtains a list of shapes from a click point
    private List<LineShape> GetLinesFromAPoint(Point p) 
    {
        List<LineShape> result = new List<LineShape>();
        Point pt = shapeContainer1.PointToScreen(p);

        foreach (Shape item in shapeContainer1.Shapes)
        {
            LineShape line = (item as LineShape);
            if (line != null && line.HitTest(pt.X, pt.Y))
            {
                result.Add(line);                    
            }
        }
        return result;
    }

    private void shapeContainer1_MouseDown(object sender, MouseEventArgs e)
    {
        // right click only
        if (e.Button == MouseButtons.Right)
        {
            List<LineShape> shapesList = GetLinesFromAPoint(e.Location);
            Console.WriteLine(DateTime.Now);
            Console.WriteLine("At this point {0} there are {1} lines.", 
                e.Location, shapesList.Count);
        }
    }
    /// obtains a list of shapes from a click point
    private List<LineShape> GetLinesFromAPoint(Point p) 
    {
        List<LineShape> result = new List<LineShape>();
        Point pt = shapeContainer1.PointToScreen(p);

        foreach (Shape item in shapeContainer1.Shapes)
        {
            LineShape line = (item as LineShape);
            if (line != null && line.HitTest(pt.X, pt.Y))
            {
                result.Add(line);                    
            }
        }
        return result;
    }

    private void shapeContainer1_MouseDown(object sender, MouseEventArgs e)
    {
        // right click only
        if (e.Button == MouseButtons.Right)
        {
            List<LineShape> shapesList = GetLinesFromAPoint(e.Location);
            Console.WriteLine(DateTime.Now);
            Console.WriteLine("At this point {0} there are {1} lines.", 
                e.Location, shapesList.Count);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文