Message 1 of 7
Get angle between LineSegments (polyline).
Not applicable
08-07-2012
12:42 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello!
I would like to get angle between lines.
My code:
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 < 0 & sin2 >= 0 & cos1 >= 0 & sin1 >=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 "";
}
Everything seems to work fine, but I have trouble when I rotate my polyline (image in attachment).
I'm getting the negative angle - value is OK, but is with "-" 😕
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.
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 😛
Thanks for any help 🙂