TheSwamp

Code Red => .NET => Topic started by: waterharbin on November 19, 2011, 02:29:15 AM

Title: How to get the cross point of two lines?
Post by: waterharbin on November 19, 2011, 02:29:15 AM
Hello,there. I want to get the cross point of two lines,which property should I access? There are three casese, see the image. Point A and Point B are the endpoint of the lines,but I want get point C.
Title: Re: How to get the cross point of two lines?
Post by: Kerry on November 19, 2011, 04:57:59 AM

Without code

Determine the StartPoint and EndPoint of each Line object.

The Geometry Namespace has a Line2d and a Line3d Class.

These classes are derived from LinearEntity2d or LinearEntity3d which have several methods named IntersectWith
The signatures for the LinearEntity3d Methos are
    public Point3d[] IntersectWith(LinearEntity3d line);
    public Point3d[] IntersectWith(PlanarEntity plane);
    public Point3d[] IntersectWith(LinearEntity3d line, Tolerance tolerance);
    public Point3d[] IntersectWith(PlanarEntity plane, Tolerance tolerance);

One option may be something like
Code: [Select]
    private static Point3d lineIntersectors(Point3d pt1, Point3d pt2, Point3d pt3, Point3d pt4)
    {
      using (Line3d l1 = new Line3d(pt1, pt2),
                    l2 = new Line3d(pt3, pt4))
      {
        return l1.IntersectWith(l2)[0];
      }
    }
Title: Re: How to get the cross point of two lines?
Post by: Kerry on November 19, 2011, 05:21:59 AM
Or you may choose to use the IntersectWith Method from the Entity Class
if you want explicit control over the Intersection Type

Signatures :

public void IntersectWith(Entity entityPointer,
                                        Intersect intersectType,
                                        Point3dCollection points,
                                        IntPtr thisGraphicSystemMarker,
                                        IntPtr otherGraphicSystemMarker)
{
}

public enum Intersect
{
    OnBothOperands,
    ExtendThis,
    ExtendArgument,
    ExtendBoth
}


 

 

 
Title: Re: How to get the cross point of two lines?
Post by: Kerry on November 19, 2011, 06:23:06 AM
Here is something to play with ..

Code: [Select]

//
// CodeHimBelonga KDUB 2011.11.19.

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof (KDUB.IntersectExample.IntersectionTest))]

namespace KDUB.IntersectExample
{
    public class IntersectionTest
    {
        [CommandMethod("Test", CommandFlags.Modal)]
        public void IntersectionTest01() {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            Point3d IntersectionPoint;

            using (Transaction tr = db.TransactionManager.StartTransaction()) {
                ObjectId id1 = SelectSpecificObjectType("\nSelect Line entity: ",
                                                        typeof (Line));
                if (id1.IsNull) {
                    return;
                }
                ObjectId id2 = SelectSpecificObjectType("\nSelect next Line entity: ",
                                                        typeof (Line));
                if (id2.IsNull) {
                    return;
                }
                Line line1 = (Line) tr.GetObject(id1, OpenMode.ForRead);
                Line line2 = (Line) tr.GetObject(id2, OpenMode.ForRead);

                IntersectionPoint = lineIntersectors(line1.StartPoint,
                                                     line1.EndPoint, line2.StartPoint,
                                                     line2.EndPoint);
                tr.Commit();
            }
            AcadApp.ShowAlertDialog(string.Format("X: {0} \nY: {1} \nZ: {2} \n",
                                                  IntersectionPoint.X, IntersectionPoint.Y,
                                                  IntersectionPoint.Z));
        }

        private static Point3d lineIntersectors(Point3d spt1, Point3d ept1, Point3d spt2,
                                                Point3d ept2) {
            using (Line3d line1 = new Line3d(spt1, ept1),
                          line2 = new Line3d(spt2, ept2)) {
                return line1.IntersectWith(line2)[0];
            }
        }

        private static ObjectId SelectSpecificObjectType(string prompt,
                                                         System.Type objectClass) {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            using (
                Transaction tr =
                    ed.Document.Database.TransactionManager.StartTransaction()) {
                while (true) {
                    PromptEntityResult res = ed.GetEntity(prompt);
                    if (res.Status != PromptStatus.OK) {
                        return ObjectId.Null;
                    }
                    DBObject ob = tr.GetObject(res.ObjectId, OpenMode.ForRead);
                    if (objectClass.IsAssignableFrom(ob.GetType())) {
                        return res.ObjectId;
                    }
                    ed.WriteMessage("\nInvalid selection, {0} entity expected",
                                    RXClass.GetClass(objectClass).DxfName);
                }
            }
        }
    }
}

And a piccy

and the source solution zipped

Title: Re: How to get the cross point of two lines?
Post by: waterharbin on November 26, 2011, 10:48:40 AM
Hellp,Kerry.I appreciate it very much.Thanks for help me so much.Happy everyday! You are a great man!
Title: Re: How to get the cross point of two lines?
Post by: It's Alive! on November 27, 2011, 04:16:03 AM
You are a great man!
++
Title: Re: How to get the cross point of two lines?
Post by: Kerry on November 27, 2011, 04:22:17 AM

Thank you waterharbin ... I'm pleased the code and post helps.

and thank you Dan.

Regards