用C#实现,获取两点之间连线上。坐标为整数的点
时间: 2024-03-12 20:44:56 浏览: 34
您可以使用以下代码获取两个整数坐标点之间的连线上的所有点:
```csharp
using System.Collections.Generic;
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
}
public static List<Point> GetPointsOnLine(Point p1, Point p2)
{
List<Point> points = new List<Point>();
int dx = p2.X - p1.X;
int dy = p2.Y - p1.Y;
int steps = Math.Max(Math.Abs(dx), Math.Abs(dy));
if (steps == 0)
{
points.Add(p1);
return points;
}
float xIncrement = (float)dx / steps;
float yIncrement = (float)dy / steps;
float x = p1.X;
float y = p1.Y;
for (int i = 0; i <= steps; i++)
{
points.Add(new Point((int)Math.Round(x), (int)Math.Round(y)));
x += xIncrement;
y += yIncrement;
}
return points;
}
```
您可以传递两个Point对象作为参数,其中每个Point对象包含整数X和Y坐标。GetPointsOnLine方法返回连接两点之间的所有整数坐标点的List<Point>对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)