【发布时间】:2018-03-02 01:09:09
【问题描述】:
我想使用Bresenham's algorihm 栅格化一行。我的插值顶点不应包含对角线步骤。我在 StackOverflow 上做了一些搜索,this topic 似乎正是我需要的东西。
我遇到的唯一问题是,如果我改变输入的顺序,我需要得到相同的结果,我的意思是如果我交换 startPoint 和 endPoint我需要获得相同的插值顶点集。
//the Method definition
List<Point> plotPoints(Point startPoint, Point endPoint);
//The thing I'm looking for
plotPoints(startPoint, endPoint)==plotPoints(endPoint, startPoint)
代码与The answer 几乎相同。但是我为我的目的做了一些定制:
private float step=0.5;
public static List<Vector3> plotPoints(float x0, float y0, float x1, float y1) {
List<Vector3> plottedPoints = new List<Vector3>();
float dx = Mathf.Abs(x1 - x0), sx = x0 < x1 ? step : -step;
float dy = -Mathf.Abs(y1 - y0), sy = y0 < y1 ? step: -step;
float err = dx + dy, e2; /* error value e_xy */
for (; ; ) { /* loop */
if (x0 == x1 && y0 == y1) break;
plottedPoints.Add(new Vector3(x0,0, y0));
e2 = 2 * err;
if (e2 >= dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
else if (e2 <= dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
}
return plottedPoints;
}
【问题讨论】:
-
你卡在哪里了?你能显示一些代码吗?
-
我添加了方法。如果我交换起点和终点,我需要得到相同的结果。
-
链接主题的
ants280方法是否产生相同的序列? (是的,这不是 Bresenham 算法) -
@MBo 老实说,我没有尝试过这种方法,但我会试一试,然后告诉你结果。
-
一种方法是交换 ypur 代码中的端点,以便您始终增加 x(或者如果 x 等于增加 y)/。这样,如果您在调用中交换参数,内部代码会将它们交换回来。
标签: c# math computational-geometry