0

I want to add additional feature of my project in C#, I can already draw lines in my program but I want to detect INTERSECTING LINES of a one line drawn and display the point they've intersect. Is it possible? Thank you

My program also includes computing for Perpendicular Distance, here is the sample code:

    public static Double PerpendicularDistance(Point Point1, Point Point2, Point Point)
    {
        Double area = Math.Abs(.5 * (Point1.X * Point2.Y + Point2.X * Point.Y + Point.X * Point1.Y - Point2.X * Point1.Y - Point.X * Point2.Y - Point1.X * Point.Y));
        Double bottom = Math.Sqrt(Math.Pow(Point1.X - Point2.X, 2) + Math.Pow(Point1.Y - Point2.Y, 2));
        Double height = area / bottom * 2;

        return height;
    }
}

The POINT here is a class for my X and Y coordinates.

mica
  • 1
  • 2
  • 1
    See [this related question](http://stackoverflow.com/questions/385305/efficient-maths-algorithm-to-calculate-intersections). – user703016 Jan 01 '12 at 17:09
  • The short answer to your question 'Is it possible?' is: yes ;-) The longer one: what is your problem? Have you researched the problem? Be more specific, and people will be able to help you. – Andre Jan 01 '12 at 17:11
  • My problem is I want to know how will get the intersection of a ONE LINE drawing in C# and display it using a message box. Thank you – mica Jan 01 '12 at 17:15
  • Agree with Andre here, with the information given it is difficult to solve the problem. You can solve the intersection of straight lines by solving the line equation, e.g. http://www.geog.ubc.ca/courses/klink/gis.notes/ncgia/u32.html#SEC32.3 However you mention curved lines? Do you have the input point arrays of these lines? What information do you have and what is it that you want? – Dr. Andrew Burnett-Thompson Jan 01 '12 at 17:20
  • I've edited the question, hopefully it's clearer now. Thank you! – mica Jan 01 '12 at 17:27

1 Answers1

0

If you are trying to find the intersection of two line, then the solution is fairly trivial.

If the two line are in the form Ax + By = C:

float delta = a1*b2 - a2*b1;
if(delta == 0) 
    throw new ArgumentException("Lines are parallel");

float x = (b2*c1 - b1*c2)/delta;
float y = (a1*c2 - a2*c1)/delta;

My concern is comment above that says there is only one drawn line. I'm not sure what you mean. Does it mean that the app provides one line and the user the other, or are we dealing in curved lines where the line intersects itself?

  • It is the actually the second one " curved lines where the line intersects itself".. – mica Jan 01 '12 at 17:28
  • very interesting problem!!! This is the only way I can think of to do this, but I'm not sure it would be very efficient code-wise. I'm sure there are a number of optimizations that would eliminate the need to try obviously non-intersecting lines. Any curved line can be approximated by breaking it into a number of line segments. The program could then iterate through them to see if any of the line segments intersect. – Gerald P. Wright Jan 01 '12 at 17:38
  • further clarification.... if we broke the curve into 10 line segments, then you would use the above on s1-s2, then s1-s3, comparing s1 with the other segments, then s2 with all the other segments and so on. Unfortunately, this runs in roughly n*n time, so performance will not be great. – Gerald P. Wright Jan 01 '12 at 17:43
  • I think that's a good idea.. but how will I implement it in C# based on my code above? I only have 2 points for X and Y, the Point1 and Point2.. – mica Jan 01 '12 at 17:48
  • I'm not sure. Based upon the fact that you only have p1 and p2, then can I safely assume these represent something like the MouseDown and MouseUp coordinates in some sort of freehand drawing program? If that is the case, then you could use the onMouseMove event to capture the Cursor.Position. – Gerald P. Wright Jan 01 '12 at 17:58
  • After that, how will I proceed with getting the intersection of a line? – mica Jan 01 '12 at 18:07
  • I'm not sure I follow. The MouseDown event gives you p1. Assuming OnMouseOver works like I think it does, each time onMouseOver fires it give another point (P sub i - Pi). Whne the user lets go of the Mouse the MouseUp event fires giving you Pn (what you now call p2). These consecutive points represent all of the line segments of the curve. You would then need to loop through all of the segments p1-p2, p1-p3 to p1-pn, then p2-p3, etc. – Gerald P. Wright Jan 01 '12 at 18:14