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

getting the coordinates of intersection between a circle and a line in x-y plane

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
663 Views, 2 Replies

getting the coordinates of intersection between a circle and a line in x-y plane

I am writing a code that will find the intersection point of a circle with a line, both of which are in x-y plane. 

Below is my code and to my dismay, the code could not give me the intersection point. Can someone point me out in the right direction on where the failure comes from. I am cluelss because there were no build errors and debug errors. A picture is also attached for greater clarity.autocad intersect.jpg

 

 

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices.DocumentExtension

Namespace sweeping
    Public Class intersecting
        <CommandMethod("ITSSPS")>
        Public Shared Sub SweepAlongPath()

            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor

            Using tr As Transaction = db.TransactionManager.StartTransaction()

                'getting point1
                Dim ppo1 As PromptPointOptions = New PromptPointOptions(vbLf & "choose/click the centre of the first circle:")
                Dim ppr1 As PromptPointResult = doc.Editor.GetPoint(ppo1)
                Dim pt1 As Point3d = ppr1.Value
                If ppr1.Status = PromptStatus.Cancel Then Exit Sub

                Dim pdo11 As PromptDoubleOptions = New PromptDoubleOptions(vbLf & "The radius of the first cycle? Type 5 as 5.0")
                Dim pdr11 As PromptDoubleResult = doc.Editor.GetDouble(pdo11)
                Dim pd11 As Double = pdr11.Value
                If pdr11.Status = PromptStatus.Cancel Then Exit Sub



                'getting point2
                Dim ppo2 As PromptPointOptions = New PromptPointOptions(vbLf & "choose/click the centre of the second circle:")
                Dim ppr2 As PromptPointResult = doc.Editor.GetPoint(ppo2)
                Dim pt2 As Point3d = ppr2.Value
                If ppr2.Status = PromptStatus.Cancel Then Exit Sub

                Dim pdo21 As PromptDoubleOptions = New PromptDoubleOptions(vbLf & "The radius of the second cycle? Type 5 as 5.0")
                Dim pdr21 As PromptDoubleResult = doc.Editor.GetDouble(pdo21)
                Dim pd21 As Double = pdr21.Value
                If pdr21.Status = PromptStatus.Cancel Then Exit Sub



                Dim line12 As Line = New Line(pt1, pt2)
                Dim Cir1, Cir2 As Circle
                Cir1 = New Circle() : Cir1.Center = pt1 : Cir1.Normal = New Vector3d(0, 0, 1) : Cir1.Radius = pd11
                Cir2 = New Circle() : Cir2.Center = pt2 : Cir2.Normal = New Vector3d(0, 0, 1) : Cir2.Radius = pd21

                Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
                Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                Dim Cir11 As Entity = DirectCast(Cir1, Entity)
                Dim line121 As Entity = DirectCast(line12, Entity)


                btr.AppendEntity(Cir11)
                tr.AddNewlyCreatedDBObject(Cir11, True)

                btr.AppendEntity(line121)
                tr.AddNewlyCreatedDBObject(line121, True)

                Dim its3dpts As Point3dCollection = New Point3dCollection()
                line121.IntersectWith(Cir11, Intersect.OnBothOperands, its3dpts, IntPtr.Zero, IntPtr.Zero)


                For Each pt3d As Point3d In its3dpts
                    Dim ptt As Point3d = pt3d
                    Dim Cir3 As Circle = New Circle()
                    Cir3.Center = ptt : Cir3.Normal = New Vector3d(0, 0, 1) : Cir3.Radius = 450
                    ed.WriteMessage(pt3d.ToString)
                    btr.AppendEntity(Cir3)
                    tr.AddNewlyCreatedDBObject(Cir3, True)
                Next

                tr.Commit()

            End Using
        End Sub
    End Class
End Namespace







 

 

 

 

2 REPLIES 2
Message 2 of 3
_gile
in reply to: Anonymous

Hi,

 

For geometric calculations, especially if it is not necessary to use database resident entities, it is preferable to use purely geometric objects from the Autodesk.AutoCAD.Geometry namespace.
 
        private Point3dCollection GetIntersections1(Point3d pt1, double rad1, Point3d pt2, double rad2)
        {
            Point3dCollection result = new Point3dCollection();
            LineSegment3d line = new LineSegment3d(pt1, pt2);
            CircularArc3d circ1 = new CircularArc3d(pt1, Vector3d.ZAxis, rad1);
            CircularArc3d circ2 = new CircularArc3d(pt2, Vector3d.ZAxis, rad2);

            Point3d[] inters1 = circ1.IntersectWith(line);
            if (inters1 != null)
                result.Add(inters1[0]);

            Point3d[] inters2 = circ2.IntersectWith(line);
            if (inters2 != null)
                result.Add(inters2[0]);

            return result;
        }
And for a so simple task, you can also hard code some very basic math:
 
        private Point3dCollection GetIntersections2(Point3d pt1, double rad1, Point3d pt2, double rad2)
        {
            Point3dCollection result = new Point3dCollection();
            double dist = pt1.DistanceTo(pt2);

            if (dist >= rad1)
                result.Add(pt1 + (pt2 - pt1).GetNormal() * rad1);

            if (dist >= rad2)
                result.Add(pt2 + (pt1 - pt2).GetNormal() * rad2);

            return result;
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3
Anonymous
in reply to: _gile

Thanks a lot. this englightened me!

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report