它仅在斜率介于 0 和 1 之间时有效。我不知道从这里去哪里。感谢您的帮助!此方法读取两个点并使用 bresenhams 算法在它们之间画线。我不知道去哪里为所有线路优化它。谢谢你的帮助。
void Bresenhams(int x1, int y1, int xk, int yk)
{
int deltaX = xk - x1;
int deltaY = yk - y1;
int error = 0;
int y = y1;
int x = x1;
int doubleDeltaX = 2 * deltaX;
bool steep = Math.Abs(yk - y1) > Math.Abs(xk - x1);
canvas.SetPixel(x1, y1, Color.Black);
if (!steep)
{
for (int i = x1 + 1; i <= xk; i++)
{
if (x1 > xk)
{
int temp = x1;
x1 = xk;
xk = temp;
temp = y1;
y1 = yk;
yk = temp;
}
error = error + deltaY;
if (error > deltaX)
{
y++;
error -= doubleDeltaX;
}
canvas.SetPixel(i, y, Color.Black);
}
}
else
{
for (int i = y1 + 1; i <= yk; i++)
{
if (y1 > yk)
{
int temp = x1;
x1 = xk;
xk = temp;
temp = y1;
y1 = yk;
yk = temp;
}
error = error + deltaY;
if (error > deltaY)
{
y++;
error -= doubleDeltaX;
}
canvas.SetPixel(x, i, Color.Black);
}
}
pictureBox1.Image = canvas;
}