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.

5
  • 1
    See this related question. Commented Jan 1, 2012 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. Commented Jan 1, 2012 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 Commented Jan 1, 2012 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. 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? Commented Jan 1, 2012 at 17:20
  • I've edited the question, hopefully it's clearer now. Thank you! Commented Jan 1, 2012 at 17:27

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

7 Comments

It is the actually the second one " curved lines where the line intersects itself"..
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.
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.
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..
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.