1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| // 八对称性
inline void _draw_circle_8(IMAGE *img, int xc, int yc, int x, int y,unsigned long c) {
// 参数 c 为颜色值
putpixel(img, xc + x, yc + y, c);
putpixel(img, xc - x, yc + y, c);
putpixel(img, xc + x, yc - y, c);
putpixel(img, xc - x, yc - y, c);
putpixel(img, xc + y, yc + x, c);
putpixel(img, xc - y, yc + x, c);
putpixel(img, xc + y, yc - x, c);
putpixel(img, xc - y, yc - x, c);
}
//Bresenham's circle algorithm
void draw_circle(IMAGE *img, int xc, int yc, int r, int fill,unsigned long c) {
// (xc, yc) 为圆心,r 为半径
// fill 为是否填充
// c 为颜色值
// 如果圆在图片可见区域外,直接退出
if (xc + r < 0 || xc - r >= img->w ||
yc + r < 0 || yc - r >= img->h) return;
int x = 0, y = r, yi, d;
d = 3 - 2 * r;
if (fill) {
// 如果填充(画实心圆)
while (x <= y) {
for (yi = x; yi <= y; yi ++)
_draw_circle_8(img, xc, yc, x, yi, c);
if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y --;
}
x++;
}
} else {
// 如果不填充(画空心圆)
while (x <= y) {
_draw_circle_8(img, xc, yc, x, y, c);
if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y --;
}
x ++;
}
}
}
|