【发布时间】:2017-12-06 17:48:19
【问题描述】:
我有以下可用的 Bresenham 算法,它找到两个顶点之间的中间点。但这是针对大小为 1 的像素。
void Bresenham(int x1,
int y1,
int const x2,
int const y2)
{
int delta_x(x2 - x1);
// if x1 == x2, then it does not matter what we set here
signed char const ix((delta_x > 0) - (delta_x < 0));
delta_x = std::abs(delta_x) << 1;
int delta_y(y2 - y1);
// if y1 == y2, then it does not matter what we set here
signed char const iy((delta_y > 0) - (delta_y < 0));
delta_y = std::abs(delta_y) << 1;
cout << "(" << x1 << "," << y1 << ")\n";
//plot(x1, y1);
if (delta_x >= delta_y)
{
// error may go below zero
int error(delta_y - (delta_x >> 1));
while (x1 != x2)
{
// reduce error, while taking into account the corner case of error == 0
if ((error > 0) || (!error && (ix > 0)))
{
error -= delta_x;
y1 += iy;
}
// else do nothing
error += delta_y;
x1 += ix;
cout << "(" << x1 << "," << y1 << ")\n";
//plot(x1, y1);
}
}
else
{
// error may go below zero
int error(delta_x - (delta_y >> 1));
while (y1 != y2)
{
// reduce error, while taking into account the corner case of error == 0
if ((error > 0) || (!error && (iy > 0)))
{
error -= delta_y;
x1 += ix;
}
// else do nothing
error += delta_x;
y1 += iy;
cout << "(" << x1 << "," << y1 << ")\n";
//plot(x1, y1);
}
}
}
我想控制两个给定顶点之间形成的中间点的数量。在上面的代码中,我目前无法控制它。
例如:如果我有顶点 (0,0) 和 (3,3);如果我想要介于两者之间的 2 个点,那就是 (1,1) 和 (2,2)。
您能否建议在我的代码中进行更改,以便我可以控制给定两个顶点之间的中间点的数量,并且请告诉我确定每个像素长度的方法(目前,像素大小固定为1)
我希望我的整体函数看起来像:void Bresenham(int x1, 整数 y1, int 常量 x2, int const y2, int TotalIntermediatePoints)
提前致谢!
【问题讨论】: