Determine the intersection point of two lines

I have 2 (VisualBasic.PowerPacks) LineShapes in 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. Lines can be moved by the user. The context menu is associated with the line. However, if the user clicks at the intersection point (if exists), I need to display another menu that will select one of the intersection lines to perform the action.

Now it’s interesting how to detect that 2 (or more) lines intersect at the click point, because in this case another context menu should appear.

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 assume that in the container there are only LineShapes. This said that ShapeContainer would not raise a MouseDown event if any LineShape was under the mouse.

But this code only gives me the largest line, but I need a list of others as well.

+4
source share
5 answers
  /// 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); } } 
0
source

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 outside the line borders and calculates the proximity + - a pixel or two.

+3
source

You just need to calculate the intersection of the two line segments. It is pretty simple.

A complete, working algorithm is described here. It works with linear segments defined by two points, so it is easy to adapt to your situation.

+2
source

serhio, thats simple Maths ...

Design the intersection points of your lines (maybe do it when they are added and save the result), and then see if the mouse is enough for warrent to display a context menu so you don't have to click a pixel.

0
source

In addition to the line crossing algorithm (as shown by several people on this page), you need to separate the context menu from the lines. In the pseudocode, you will need something like:

 onLine1Click: if intersection then handle intersection else handle line1 click onLine2Click: if intersection then handle intersection else handle line2 click 

This processing may display a context menu. I believe that if / then / else is required to solve the remaining problem.

0
source

Source: https://habr.com/ru/post/1299968/


All Articles