用C语言代码实现布雷森汉姆直线插补
时间: 2023-03-01 17:35:50 浏览: 49
布雷森汉姆直线插补是计算机图形学中常见的直线插补算法。以下是用C语言实现布雷森汉姆直线插补的代码示例:
```
#include <stdio.h>
#include <math.h>
void bresenham_line(int x1, int y1, int x2, int y2)
{
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int p = 2 * dy - dx;
int twoDy = 2 * dy;
int twoDyMinusDx = 2 * (dy - dx);
int x, y;
/* Determine which endpoint to use as start position */
if (x1 > x2) {
x = x2;
y = y2;
x2 = x1;
} else {
x = x1;
y = y1;
}
printf("(%d, %d)\n", x, y);
while (x < x2) {
x++;
if (p < 0) {
p += twoDy;
} else {
y++;
p += twoDyMinusDx;
}
printf("(%d, %d)\n", x, y);
}
}
int main(void)
{
int x1 = 2, y1 = 3, x2 = 15, y2 = 10;
bresenham_line(x1, y1, x2, y2);
return 0;
}
```
该代码实现了从点(x1, y1)到点(x2, y2)的布雷森汉姆直线插补。
相关推荐












