坐标系的生成

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.");
 }
posted @ 2024-09-22 00:49  孤沉  阅读(7)  评论(0编辑  收藏  举报