座標系的生成

孤沉發表於2024-09-22

1、包裝一下Point類,增加Id

public class PointWithId
{
    public int Id { get; set; }
    public Point Coordinate { get; set; }

    public PointWithId(int id, Point coordinate)
    {
        Id = id;
        Coordinate = coordinate;
    }
}

2、獲取第一象限的座標
我們使用Linq生成(0,0)到(4,4)的25個座標

// 使用Enumerable.Range和LINQ生成帶有Id的PointWithId物件列表
List<PointWithId> originalPoints = Enumerable.Range(1, 25) // Id從1到9
    .Select((id, index) => new PointWithId(id, new Point(index / 5, index % 5)))
    .ToList();

3、同理可以獲取其他3個象限的座標

 List<PointWithId> secondQuadrantPoints = originalPoints.Select(p => new PointWithId(p.Id, new Point(-p.Coordinate.X, p.Coordinate.Y))).ToList();
 List<PointWithId> thirdQuadrantPoints = originalPoints.Select(p => new PointWithId(p.Id, new Point(-p.Coordinate.X, -p.Coordinate.Y))).ToList();
 List<PointWithId> fourthQuadrantPoints = originalPoints.Select(p => new PointWithId(p.Id, new Point(p.Coordinate.X, -p.Coordinate.Y))).ToList();

4、對於標定板上的座標,我們需要填充真值

 // 已知點(17, 4)的Id
 int knownPointId = 2; // 假設已知點的Id是2
 Point knownPointNewPosition = new Point(17, 4);

 // 找到已知點在原始列表中的物件
 PointWithId knownPoint = originalPoints.Find(p => p.Id == knownPointId);
 if (knownPoint != null)
 {
     // 計算偏移量
     var deltaX = knownPointNewPosition.X - knownPoint.Coordinate.X;
     var deltaY = knownPointNewPosition.Y - knownPoint.Coordinate.Y;

     // 使用偏移量計算其他點的新位置
     foreach (PointWithId point in originalPoints)
     {
         Point newPosition = new Point(
             point.Coordinate.X + deltaX,
             point.Coordinate.Y + deltaY
         );
         point.Coordinate = newPosition; // 更新點的位置
     }

     // 列印新位置
     foreach (PointWithId point in originalPoints)
     {
         Console.WriteLine($"Id: {point.Id}, Original: ({point.Coordinate.X}, {point.Coordinate.Y})");
     }
 }
 else
 {
     Console.WriteLine("Known point not found in the original list.");
 }

相關文章