圆弧和多段线排序问题

1、多段线封闭图形排序

 internal class Program
 {
     private static void Main(string[] args)
     {
         List<Hv_Point> points = new List<Hv_Point>()
         {
             new Hv_Point(1,1),
             new Hv_Point(2,2),
             new Hv_Point(1,2),
             new Hv_Point(2,1),
         };
         List<Hv_Point> sortedPoints = SortPolyLinePoints(points);
         foreach (Hv_Point Hv_Point in sortedPoints)
         {
             Console.WriteLine($"{Hv_Point.X},{Hv_Point.Y}");
         }
     }

     private static List<Hv_Point> SortPolyLinePoints(List<Hv_Point> points)
     {
         //计算质心
         Hv_Point centorid = CalCentorid(points);

         //计算每个顶点相对于质心的极角,按照极角排序
         return points.OrderBy(p => CalculateAngle(centorid, p)).ToList();
     }

     private static double CalculateAngle(Hv_Point centorid, Hv_Point p)
     {
         double dx = p.X - centorid.X;
         double dy = p.Y - centorid.Y;
         return Math.Atan2(dx, dy);
     }

     private static Hv_Point CalCentorid(List<Hv_Point> points)
     {
         double sumX = points.Sum(p => p.X);
         double sumY = points.Sum(p => p.Y);
         int count = points.Count;
         return new Hv_Point(sumX / count, sumY / count);
     }
     
 }

 public class Hv_Point
 {
     public double X;
     public double Y;

     public Hv_Point(double x, double y)
     {
         X = x;

         Y = y;
     }
 }

2、圆弧和多段线拼接通用方法

class Program
{
    static void Main()
    {
	// 圆弧的起点和终点
	Point arcStart = new Point(100, 100);
	Point arcEnd = new Point(200, 100);

	// 多段线的起点和终点
	Point[] polylineStarts = new Point[]
	{
		new Point(200, 100), // 第一条多段线的起点
		new Point(200, 200), // 第二条多段线的起点
		new Point(100, 200)  // 第三条多段线的起点
	};

	Point[] polylineEnds = new Point[]
	{
		new Point(200, 200), // 第一条多段线的终点
		new Point(100, 200), // 第二条多段线的终点
		new Point(100, 100)  // 第三条多段线的终点
	};
        // 创建 PathGeometry
        PathGeometry pathGeometry = new PathGeometry();

        // 添加圆弧
        ArcSegment arcSegment = new ArcSegment(
            arcEnd, // 圆弧的终点
            new Size(50, 50), // 圆弧的半径
            0, // 旋转角度
            false, // 是否大于 180 度
            SweepDirection.Clockwise, // 圆弧方向
            true // 是否平滑连接
        );
        PathFigure pathFigure = new PathFigure
        {
            StartPoint = arcStart, // 圆弧的起点
            IsClosed = true // 闭合路径
        };
        pathFigure.Segments.Add(arcSegment);

        // 添加多段线
        for (int i = 0; i < polylineStarts.Length; i++)
        {
            LineSegment lineSegment = new LineSegment(polylineEnds[i], true);
            pathFigure.Segments.Add(lineSegment);
        }

        // 将 PathFigure 添加到 PathGeometry
        pathGeometry.Figures.Add(pathFigure);

        // 输出路径的点
        Console.WriteLine("Path Points:");
        foreach (var point in pathGeometry.GetFlattenedPathGeometry().Figures[0].Segments)
        {
            if (point is LineSegment line)
            {
                Console.WriteLine($"Line: {line.Point}");
            }
            else if (point is ArcSegment arc)
            {
                Console.WriteLine($"Arc: {arc.Point}");
            }
        }
    }
}
posted @ 2025-03-10 22:56  孤沉  阅读(16)  评论(0)    收藏  举报