让我说我有我的第一个Point结构:
Point start = new Point(1, 9);
Run Code Online (Sandbox Code Playgroud)
而我的第二个:
Point end = new Point(4, 9);
Run Code Online (Sandbox Code Playgroud)
我想得到开始和结束之间的所有要点.所以例如我想要一个数组中的2,9和3,9..NET有内置的东西吗?
这就是我最终做的事情.正如@Cody Gray在评论中提到的那样,在一条线上有无限的点.因此,您需要指定要检索的点数.
我的班级课程:
public class Line {
public Point p1, p2;
public Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
public Point[] getPoints(int quantity) {
var points = new Point[quantity];
int ydiff = p2.Y - p1.Y, xdiff = p2.X - p1.X;
double slope = (double)(p2.Y - p1.Y) / (p2.X - p1.X);
double x, y;
--quantity;
for (double i = 0; i < quantity; i++) {
y = slope == 0 ? 0 : ydiff * (i / quantity);
x = slope == 0 ? xdiff * (i / quantity) : y / slope;
points[(int)i] = new Point((int)Math.Round(x) + p1.X, (int)Math.Round(y) + p1.Y);
}
points[quantity] = p2;
return points;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
var line = new Line(new Point(10, 15), new Point(297, 316));
var points = line.getPoints(20);
Run Code Online (Sandbox Code Playgroud)
这将返回两个端点(包括两端)之间均匀间隔的20点的Point数组.希望有所帮助!
由于点之间没有点,因此没有为此构建函数.Mathematicaly在两点之间有一条线.在计算机图形学方面,线路可以是抗锯齿的,因此不能四舍五入到完整的整数.
如果您正在寻找一种在中间创建所有整数的快速方法,我猜Bresenhams-Line-Algorithm将是您的选择.但这不是构建到.NET中,您必须自己编写代码(或者采用Matthew Watson的实现):
http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
甚至还有更快的算法,但我会选择Bresenham.
| 归档时间: |
|
| 查看次数: |
6668 次 |
| 最近记录: |