Untitled

 avatar
unknown
plain_text
14 days ago
5.0 kB
1
Indexable
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

public class LargestInscribedRect
{
    // ... (Other functions remain the same)

    private static Polyline RotatingCalipers(List<Point2d> hullPoints)
    {
        if (hullPoints.Count < 3)
        {
            // Handle cases where the hull has fewer than 3 points
            if (hullPoints.Count == 2)
            {
                Polyline linePolyline = new Polyline();
                linePolyline.AddVertexAt(0, hullPoints[0], 0, 0, 0);
                linePolyline.AddVertexAt(1, hullPoints[1], 0, 0, 0);
                return linePolyline;
            }
            else
            {
                return null; // Or handle as appropriate for your application
            }
        }

        Polyline maxRect = null;
        double maxArea = 0;

        double angleIncrement = 0.01;
        for (double angle = 0; angle < Math.PI / 2; angle += angleIncrement)
        {
            // Rotate the hull points
            List<Point2d> rotatedHull = hullPoints.Select(p => RotatePoint(p, angle, hullPoints[0])).ToList();

            // Find the extreme points of the rotated hull
            double minX = rotatedHull.Min(p => p.X);
            double minY = rotatedHull.Min(p => p.Y);
            double maxX = rotatedHull.Max(p => p.X);
            double maxY বেশী = rotatedHull.Max(p => p.Y);

            // Find the supporting lines (calipers)
            Point2d p1 = new Point2d();
            Point2d p2 = new Point2d();
            Point2d p3 = new Point2d();
            Point2d p4 = new Point2d();

            for (int i = 0; i < rotatedHull.Count; i++)
            {
                Point2d p = rotatedHull[i];
                if (p.X == minX) p4 = p;
                if (p.X == maxX) p2 = p;
                if (p.Y == minY) p1 = p;
                if (p.Y == maxY) p3 = p;
            }

            // Find intersection points of supporting lines to form rectangle
            Point2d[] rectPoints = new Point2d[4];
            if (p1 != p2)
            {
                double slope = (p2.Y - p1.Y) / (p2.X - p1.X);
                double invSlope = -1 / slope;

                rectPoints[0] = IntersectLines(p1, slope, p4, invSlope);
                rectPoints[1] = IntersectLines(p1, slope, p3, invSlope);
                rectPoints[2] = IntersectLines(p2, slope, p3, invSlope);
                rectPoints[3] = IntersectLines(p2, slope, p4, invSlope);
            }
            else
            {
                rectPoints[0] = p1;
                rectPoints[1] = p3;
                rectPoints[2] = p2;
                rectPoints[3] = p4;
            }

            // Rotate the rectangle points back to the original coordinate system
            Point2d rp1 = RotatePoint(rectPoints[0], -angle, hullPoints[0]);
            Point2d rp2 = RotatePoint(rectPoints[1], -angle, hullPoints[0]);
            Point2d rp3 = RotatePoint(rectPoints[2], -angle, hullPoints[0]);
            Point2d rp4 = RotatePoint(rectPoints[3], -angle, hullPoints[0]);

            // Create a polyline from the rectangle points
            Polyline rect = new Polyline();
            rect.AddVertexAt(0, rp1, 0, 0, 0);
            rect.AddVertexAt(1, rp2, 0, 0, 0);
            rect.AddVertexAt(2, rp3, 0, 0, 0);
            rect.AddVertexAt(3, rp4, 0, 0, 0);
            rect.Closed = true;

            // Calculate the area of the rectangle
            double area = CalculateRectangleArea(rect);

            // Update the maximum rectangle if necessary
            if (area > maxArea)
            {
                maxArea = area;
                maxRect = rect;
            }
        }

        return maxRect;
    }

    // Helper function to find the intersection point of two lines
    private static Point2d IntersectLines(Point2d p1, double m1, Point2d p2, double m2)
    {
        if (double.IsInfinity(m1))
        {
            return new Point2d(p1.X, m2 * (p1.X - p2.X) + p2.Y);
        }
        else if (double.IsInfinity(m2))
        {
            return new Point2d(p2.X, m1 * (p2.X - p1.X) + p1.Y);
        }
        else
        {
            double x = (m1 * p1.X - m2 * p2.X - p1.Y + p2.Y) / (m1 - m2);
            double y = m1 * (x - p1.X) + p1.Y;
            return new Point2d(x, y);
        }
    }

    // Helper function to calculate the area of a rectangle
    private static double CalculateRectangleArea(Polyline rect)
    {
        if (rect.NumberOfVertices < 4) return 0;

        Point2d p1 = rect.GetPoint2dAt(0);
        Point2d p2 = rect.GetPoint2dAt(1);
        Point2d p3 = rect.GetPoint2dAt(2);

        double side1 = p1.GetDistanceTo(p2);
        double side2 = p2.GetDistanceTo(p3);

        return side1 * side2;
    }

    // ... (Other functions remain the same)
}
Leave a Comment