检测2条线的交点
我的表单上有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的坐标网络中,有两条线:
y1 = ax + c1
和y2 = bx + c2
。找到x1=x2
和y1=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
andy2 = bx + c2
. Find the intersection point wherex1=x2
andy1=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.
您只需要计算两条线段的交点。这相当简单。
此处描述了完整的工作算法。它可以离线工作,由两点定义线段,因此应该很容易适应您的情况。
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.
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.
除了线相交算法(如本页上的几个人所示)之外,您还需要将上下文菜单与线解耦。在伪代码中,您将需要类似的内容:
此处理可以显示上下文菜单。我相信需要使用 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:
This handling can be showing the context menu. I believe this if/then/else is required to resolve your remaining issue.