.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

What is a Valid Value for the "Tolerance" parameter in "IntersectsWith" ?

9 REPLIES 9
Reply
Message 1 of 10
rmdw
1300 Views, 9 Replies

What is a Valid Value for the "Tolerance" parameter in "IntersectsWith" ?

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

9 REPLIES 9
Message 2 of 10
hgasty1001
in reply to: rmdw

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

Message 3 of 10
rmdw
in reply to: hgasty1001

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

Message 4 of 10
hgasty1001
in reply to: rmdw

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

 

 

Message 5 of 10
owenwengerd
in reply to: rmdw

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 Wengerd
ManuSoft
Message 6 of 10
rmdw
in reply to: owenwengerd

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*);
    }

Message 7 of 10
mcicognani
in reply to: rmdw

If you're searching for an alternative method, you may look here:

 

http://through-the-interface.typepad.com/through_the_interface/2012/09/overriding-the-grips-of-an-au...

 

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.

 

Message 8 of 10
rmdw
in reply to: mcicognani

VERY interesting code, Massimo!  I've just looked over and will study more intently over the next 48 hrs.

 

Much appreciated!

 

Robert

Message 9 of 10
owenwengerd
in reply to: rmdw


@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));
    }

 

--
Owen Wengerd
ManuSoft
Message 10 of 10
SEANT61
in reply to: rmdw

" . . . .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);
        }

 

 


************************************************************
May your cursor always snap to the location intended.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost