<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re : Get angle between LineSegments (polyline). in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569504#M54032</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Look at the docs for the Vector3d.GetAngleTo() method.&lt;/P&gt;&lt;P&gt;Used with a single argument: v1.GetAngleTo(v2) it returns the angle between the vectors in the range 0 to PI (as Vector2d.GetAngleTo() does).&lt;/P&gt;&lt;P&gt;If you pass a second argument as reference vector: v1.GetAngleTo(v2, referenceVector) it reurns the result in the range 0 to 2*PI.&lt;/P&gt;&lt;P&gt;The reference vector is used to determinate the direction the angle is define (right hand rule).&lt;/P&gt;&lt;P&gt;Using the pline.Normal as refrence vector insures it won't change whatever the pline rotation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a command example:&lt;/P&gt;&lt;PRE&gt;        [CommandMethod("Test")]
        public void Test()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a polyline: ");
            peo.SetRejectMessage("Only a polyline.");
            peo.AddAllowedClass(typeof(Polyline), true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    Polyline pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                    for (int i = 0; i &amp;lt; pline.NumberOfVertices - 2; i++)
                    {
                        LineSegment3d l1 = pline.GetLineSegmentAt(i);
                        LineSegment3d l2 = pline.GetLineSegmentAt(i + 1);
                        double angle = GetPolylineShape(l1, l2, pline.Normal);
                        ed.WriteMessage("\nAngle between {0} and {1}: {2}", i, i + 1, Converter.AngleToString(angle, AngularUnitFormat.Degrees, 2));
                        if (angle &amp;gt; Math.PI)
                            ed.WriteMessage(" ({0:0.00})", (angle - Math.PI * 2.0) * 180.0 / Math.PI);
                    }
                }
            }
            catch
            {
                ed.WriteMessage("\nInvalid polyline.");
            }
        }

        private double GetPolylineShape(LineSegment3d l1, LineSegment3d l2, Vector3d normal)
        {
            Vector3d v1 = l1.EndPoint - l1.StartPoint;
            Vector3d v2 = l2.EndPoint - l2.StartPoint;
            return v1.GetAngleTo(v2, normal);
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;[EDIT] : the upper code works the same as gasty1001's, excpeted it isn't related to WCS coordinates. It will work whatever the polyline normal.&lt;/P&gt;</description>
    <pubDate>Wed, 08 Aug 2012 08:49:48 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2012-08-08T08:49:48Z</dc:date>
    <item>
      <title>Get angle between LineSegments (polyline).</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3568906#M54026</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to get angle between lines.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;My code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        public static string GetPolylineShape(LineSegment3d l1, LineSegment3d l2)
        {
            Vector3d _a = l1.EndPoint.GetVectorTo(l1.StartPoint);
            Vector3d _b = l2.EndPoint.GetVectorTo(l2.StartPoint);
            Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;

            double angle = _b.GetAngleTo(_a);

            double Ax = l1.StartPoint.X;
            double Ay = l1.StartPoint.Y;
            double Bx = l1.EndPoint.X;
            double By = l1.EndPoint.Y;
            double Cx = l2.EndPoint.X;
            double Cy = l2.EndPoint.Y;

            double BAx = Ax - Bx;
            double BAy = Ay - By;
            double BCx = Cx - Bx;
            double BCy = Cy - By;

            double BAd = Math.Sqrt(BAx * BAx + BAy + BAy);
            double BCd = Math.Sqrt(BCx * BCx + BCy + BCy);

            double sin1 = BAy / BAd;
            double cos1 = BAx / BAd;
            double sin2 = BCy / BCd;
            double cos2 = BCx / BCd;

            ed.WriteMessage(string.Format("\nsin1: {0} | cos1: {1}", Math.Round(sin1, 4), Math.Round(cos1, 4)));
            ed.WriteMessage(string.Format("\nsin2: {0} | cos2: {1}", Math.Round(sin2, 4), Math.Round(cos2, 4)));

            double DotProduct = BAx * BCx + BAy * BCy;
            double CrossProductLength = BAx * BCy - BAy * BCx;

            if (cos2 &amp;lt; 0 &amp;amp; sin2 &amp;gt;= 0 &amp;amp; cos1 &amp;gt;= 0 &amp;amp; sin1 &amp;gt;=0)
                angle = -angle;
            
            ed.WriteMessage(string.Format("\nAngle: {0} | Dot: {1} Cross: {2}", Math.Round(angle * 180 / Math.PI,4), Math.Round(DotProduct,4), Math.Round(CrossProductLength,4)));

            return "";
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Everything seems to work fine, but I have trouble when I rotate my polyline (image in attachment).&lt;/P&gt;&lt;P&gt;I'm getting the negative angle - value is OK, but is with "-" &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I was trying to catch this "exception" by if statement with sinus/cosinus/dot/cross products, but ... hmmm ... doesn't work as you could see.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought that I can set up new UCS for first linesegment and calculate the angle in modified UCS, but ... I don't know how to do this (I mean, set up or transform UCS). But I'm not sure if this would be a good idea &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for any help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Aug 2012 19:42:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3568906#M54026</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-08-07T19:42:54Z</dc:date>
    </item>
    <item>
      <title>Re : Get angle between LineSegments (polyline).</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569032#M54027</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;you can use the Vector3d.GetAngleTo() with the pline normal as reference vector:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;private&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;double&lt;/SPAN&gt;&amp;nbsp;GetPolylineShape(&lt;SPAN&gt;LineSegment3d&lt;/SPAN&gt;&amp;nbsp;l1,&amp;nbsp;&lt;SPAN&gt;LineSegment3d&lt;/SPAN&gt;&amp;nbsp;l2,&amp;nbsp;Vector3d&amp;nbsp;normal)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Vector3d&amp;nbsp;v1&amp;nbsp;=&amp;nbsp;l1.EndPoint&amp;nbsp;-&amp;nbsp;l1.StartPoint;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Vector3d&amp;nbsp;v2&amp;nbsp;=&amp;nbsp;l2.EndPoint&amp;nbsp;-&amp;nbsp;l2.StartPoint;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;return&lt;/SPAN&gt;&amp;nbsp;v1.GetAngleTo(v2,&amp;nbsp;normal);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Aug 2012 21:17:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569032#M54027</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2012-08-07T21:17:06Z</dc:date>
    </item>
    <item>
      <title>Re : Get angle between LineSegments (polyline).</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569146#M54028</link>
      <description>&lt;P&gt;Hey,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thaks for the reply. Could you explain me what I should pass as normal parameter?&lt;/P&gt;&lt;P&gt;I tried to pass Polyline's normal and first LineSegment normal, but it doesn't work. Emmm ... it works well, I've got correct angle, but only for those angles which I have negatives. For previous, I'm getting nothing &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Aug 2012 22:06:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569146#M54028</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-08-07T22:06:17Z</dc:date>
    </item>
    <item>
      <title>Re : Get angle between LineSegments (polyline).</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569210#M54029</link>
      <description>&lt;P&gt;In attachment is image with more advanced Polyline.&lt;/P&gt;&lt;P&gt;We are starting from point 1 and I would like to get angle between 1/2 - 2/3. Function returns correct value.&lt;/P&gt;&lt;P&gt;But angles 2/3-3/4 and 3/4-4/5 are positive, but should be negatives (angle is measured clockwise, not counterclockwise like in 1/2-2/3).&lt;/P&gt;</description>
      <pubDate>Tue, 07 Aug 2012 22:54:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569210#M54029</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-08-07T22:54:53Z</dc:date>
    </item>
    <item>
      <title>Re : Get angle between LineSegments (polyline).</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569222#M54030</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure but may be if you obtain the angles in this way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;theta1 = _a.GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis.Negate())&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;theta2 = _b.GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis.Negate())&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can check the sign from (theta1-theta2), or you can get the angle directly from :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;theta = _a.GetAngleTo(_b, Vector3d.ZAxis.Negate())&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;just guessing, not tested in code&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Gaston Nunez&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Aug 2012 23:16:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569222#M54030</guid>
      <dc:creator>hgasty1001</dc:creator>
      <dc:date>2012-08-07T23:16:57Z</dc:date>
    </item>
    <item>
      <title>Re: Get angle between LineSegments (polyline).</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569502#M54031</link>
      <description>&lt;P&gt;I think that the code is OK, I'm getting correct values of angles.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe the problem is that I'm calculating polyline's angles where I have more than 1 angle and I should check in loop the previous angle?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2012 08:28:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569502#M54031</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-08-08T08:28:55Z</dc:date>
    </item>
    <item>
      <title>Re : Get angle between LineSegments (polyline).</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569504#M54032</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Look at the docs for the Vector3d.GetAngleTo() method.&lt;/P&gt;&lt;P&gt;Used with a single argument: v1.GetAngleTo(v2) it returns the angle between the vectors in the range 0 to PI (as Vector2d.GetAngleTo() does).&lt;/P&gt;&lt;P&gt;If you pass a second argument as reference vector: v1.GetAngleTo(v2, referenceVector) it reurns the result in the range 0 to 2*PI.&lt;/P&gt;&lt;P&gt;The reference vector is used to determinate the direction the angle is define (right hand rule).&lt;/P&gt;&lt;P&gt;Using the pline.Normal as refrence vector insures it won't change whatever the pline rotation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a command example:&lt;/P&gt;&lt;PRE&gt;        [CommandMethod("Test")]
        public void Test()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a polyline: ");
            peo.SetRejectMessage("Only a polyline.");
            peo.AddAllowedClass(typeof(Polyline), true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    Polyline pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                    for (int i = 0; i &amp;lt; pline.NumberOfVertices - 2; i++)
                    {
                        LineSegment3d l1 = pline.GetLineSegmentAt(i);
                        LineSegment3d l2 = pline.GetLineSegmentAt(i + 1);
                        double angle = GetPolylineShape(l1, l2, pline.Normal);
                        ed.WriteMessage("\nAngle between {0} and {1}: {2}", i, i + 1, Converter.AngleToString(angle, AngularUnitFormat.Degrees, 2));
                        if (angle &amp;gt; Math.PI)
                            ed.WriteMessage(" ({0:0.00})", (angle - Math.PI * 2.0) * 180.0 / Math.PI);
                    }
                }
            }
            catch
            {
                ed.WriteMessage("\nInvalid polyline.");
            }
        }

        private double GetPolylineShape(LineSegment3d l1, LineSegment3d l2, Vector3d normal)
        {
            Vector3d v1 = l1.EndPoint - l1.StartPoint;
            Vector3d v2 = l2.EndPoint - l2.StartPoint;
            return v1.GetAngleTo(v2, normal);
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;[EDIT] : the upper code works the same as gasty1001's, excpeted it isn't related to WCS coordinates. It will work whatever the polyline normal.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2012 08:49:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-angle-between-linesegments-polyline/m-p/3569504#M54032</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2012-08-08T08:49:48Z</dc:date>
    </item>
  </channel>
</rss>

