How to know LINE (not polyline) direction?

How to know LINE (not polyline) direction?

iPranavKulkarni
Advocate Advocate
450 Views
4 Replies
Message 1 of 5

How to know LINE (not polyline) direction?

iPranavKulkarni
Advocate
Advocate

I created rectangle shape by line and I want to know the direction (clockwise or anticlockwise).
Can someone pls help.

iPranavKulkarni_0-1687758827359.png

 

0 Likes
451 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant

Hi,

Clockwise and counterclockwise does not make sense for a single line.

You should work with polyline or points (summits of the rectangle).

You can check if a polygon (points sequence) is clockwise or counterclockwise by computing its signed area. If the area is negative, the polygon is clockwise.

Here's an example which computes both the polygon centroid and signed area.

 

        public static Point2d GetPolygonCentroid(Point2dCollection polygon, out double area)
        {
            Point2d cen = new Point2d();
            double tmpArea;
            area = 0.0;
            int last = polygon.Count - 1;
            Point2d p0 = polygon[0];

            for (int i = 1; i < last; i++)
            {
                var p1 = polygon[i];
                var p2 = polygon[i + 1];
                tmpArea = GetTriangleSignedArea(p0, p1, p2);
                cen += (GetTriangleCentroid(p0, p1, p2) * tmpArea).GetAsVector();
                area += tmpArea;
            }
            return cen.DivideBy(area);
        }

        public static Point2d GetTriangleCentroid(Point2d pt0, Point2d pt1, Point2d pt2) =>
            (pt0 + pt1.GetAsVector() + pt2.GetAsVector()) / 3.0;

        public static double GetTriangleSignedArea(Point2d pt0, Point2d pt1, Point2d pt2) =>
            ((pt1.X - pt0.X) * (pt2.Y - pt0.Y) -
            (pt2.X - pt0.X) * (pt1.Y - pt0.Y)) / 2.0;

 

 

A testing command:

 

        [CommandMethod("CENTROID")]
        public static void Centroid()
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;

            var ppo = new PromptPointOptions("\nPick a point: ");
            ppo.AllowNone = true;
            var points = new Point2dCollection();
            while (true)
            {
                var ppr = ed.GetPoint(ppo);
                if (ppr.Status == PromptStatus.None) break;
                if (ppr.Status != PromptStatus.OK) return;
                points.Add(new Point2d(ppr.Value.X, ppr.Value.Y));
            }
            if (2 < points.Count)
            {
                var centroid = GetPolygonCentroid(points, out double area);
                ed.Command("_point", new Point3d(centroid.X, centroid.Y, 0.0));
                ed.WriteMessage($"\nArea = {Math.Abs(area)} ({(area < 0.0 ? "Clockwise" : "Counterclockwise")})");
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

iPranavKulkarni
Advocate
Advocate

@_gile Thanks for your response. 
Actually Maximum times I am using lines because that is the requirement.

I am placing some objects on lines/polylines. using below piece of code I am inserting object.

double angle = Vector3d.XAxis.GetAngleTo(li.StartPoint.GetVectorTo(li.EndPoint), Vector3d.ZAxis);
                double length = li.StartPoint.DistanceTo(li.EndPoint);
                pt = li.GetPointAtDist(length * 0.5);
                using (BlockReference br = new BlockReference(pt, ObjId))
                {
                    br.Rotation = angle;
                    BlockTableRecord blkTblRec = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    ObjectId brId = blkTblRec.AppendEntity(br);
                    trans.AddNewlyCreatedDBObject(br, true);
                }


problem is objects position changing with direction of line.

iPranavKulkarni_0-1687768146435.png

1.Line drawn CW direction so object placed at left side of line

2.line drawn CCW direction and object place at right side of line.

I need parameter from which I can get direction of line.

 

 

 

 

0 Likes
Message 4 of 5

_gile
Consultant
Consultant

As said upper the line 'direction' only make sense for clockwise or counterclockwise when it's compared to the other lines componing the polygon.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

kerry_w_brown
Advisor
Advisor

@iPranavKulkarni ,

Hi,

1.Line drawn CW direction so object placed at left side of line

2.line drawn CCW direction and object place at right side of line.

I need parameter from which I can get direction of line.

You do have the direction of the line. !!

Seems to me you're complaining about the programming language not being able to guess what you want.
As Gile explained, the location and rotation of the block is being determined by what you tell it to do.
If you want it to be different you will need to program it to be different.
Perhaps you will need to incorporate a prompt asking for the user to accept ( or mirror ) the block properties.

Regards,

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes