I'm new to AutoCad programming with C# and am confused about something simple. I'm trying to create a C# method that determines whether two 2D lines actually intersect. So I started working with the code shown in the 4th comment here. The problem with this code is that it extends each of the two lines to infinity so they always intersect unless they're parallel. What I want is to determine if the two selected lines ACTUALLY intersect on the screen.
So I started building my own method to do this:
private static Point2d lineIntersects2D(Point2d spt1, Point2d ept1, Point2d spt2, Point2d ept2)
{
Line2d line1 = new Line2d(spt1, ept1);
Line2d line2 = new Line2d(spt2, ept2);
return line1.IntersectWith(line2, *Tolerance*);
}
But I can't figure out what precisely is the syntax for the Tolerance value. I assume that it is equivalent to zero but don't know the format.
Can anyone help?
Robert
Hi,
The tolerance is not 0, it can't be null if you need a working program, as rounding is always present in floating point operations. There are 2 types of tolerances in the API, one for comparing points, and one for comparing vectors, the first has a default to 1E-10 the second default to 1E-12 , if you sense the need to change some value (a perfect valid option) you can change its values in a new instance of the tolerance structure.
1.- Get the values
Tolerance.Global.EqualVector
Tolerance.Global.EqualPoint
2.- Set the values
Dim tol as Tolerance=new Tolerance(EqualVector Value,EqualPoint Value)
Gaston Nunez
Thank you for that, Gaston!
Based on what you wrote, I think I'm heading in the wrong direction.
I'm simply trying to determine if two 2D line segments actually touch each other. Might you know how I could do this with C# code?
Robert
Hi,
May be you should start inspecting the documentation on the curve methods, my first would be the Curve.IsOn, it has several overloads, sure some may fit your case.
Gaston Nunez
Your method should work fine. You just need to supply a tolerance object or use the default Tolerance.Global. Tolerance values can be zero, but that's rarely a good idea.
Owen,
Just so I'm 100% clear, precisely what would you replace "*Tolerance*" with?
This is what I'm unclear about,
Robert
private static Point2d lineIntersects2D(Point2d spt1, Point2d ept1, Point2d spt2, Point2d ept2)
{
Line2d line1 = new Line2d(spt1, ept1);
Line2d line2 = new Line2d(spt2, ept2);
return line1.IntersectWith(line2, *Tolerance*);
}
If you're searching for an alternative method, you may look here:
There is a CheckIntersect() function you may be interested in. It's a portion of a math library I've been using for 20 years, from the time where math-coprocessor were optional and using trygonometric function was the worst you can do for precision.
The function also works with collinear or overlapping segments.
If you want to stick with IntersectWith() function, I may suggest you check distance from the intersection point to the segment ends, if the sum is equal to the segment lenght the point lays on the segment. You may want to check this with both segments.
VERY interesting code, Massimo! I've just looked over and will study more intently over the next 48 hrs.
Much appreciated!
Robert
@rmdw wrote:Just so I'm 100% clear, precisely what would you replace "*Tolerance*" with?
To use the default tolerance:
private static Point2d[] lineIntersects2D(Point2d spt1, Point2d ept1, Point2d spt2, Point2d ept2) { Line2d line1 = new Line2d(spt1, ept1); Line2d line2 = new Line2d(spt2, ept2); return line1.IntersectWith(line2); }
Or, more explicitly:
private static Point2d[] lineIntersects2D(Point2d spt1, Point2d ept1, Point2d spt2, Point2d ept2) { Line2d line1 = new Line2d(spt1, ept1); Line2d line2 = new Line2d(spt2, ept2); return line1.IntersectWith(line2, Tolerance.Global); }
Or a custom tolerance:
private static Point2d[] lineIntersects2D(Point2d spt1, Point2d ept1, Point2d spt2, Point2d ept2) { Line2d line1 = new Line2d(spt1, ept1); Line2d line2 = new Line2d(spt2, ept2); return line1.IntersectWith(line2, new Tolerance(0.0001, 0.00003)); }
" . . . .that determines whether two 2D lines actually intersect. So I started working with the code shown in the 4th comment here. The problem with this code is that it extends each of the two lines to infinity so they always intersect unless they're parallel. What I want is to determine if the two selected lines ACTUALLY intersect on the screen."
To take advantage of everything ManagedARX has to offer, how about this to determine actual intersection.
. . . . Point2d[] Temp = lineIntersects2D(Lin1SP, Lin1NP, Lin2SP, Lin2NP); if (Temp == null) ed.WriteMessage("\nNo Intersection!"); . . . . private static Point2d[] lineIntersects2D(Point2d spt1, Point2d ept1, Point2d spt2, Point2d ept2) { LineSegment2d line1 = new LineSegment2d(spt1, ept1); LineSegment2d line2 = new LineSegment2d(spt2, ept2); return line1.IntersectWith(line2, Tolerance.Global); }