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")})");
}
}