# 程序画圆

Bresenham
//draw_pixel 中间的乘法应该可以简化，
//不过这个里主要是画圆，这里不考虑
int draw_pixel(unsigned char *data,  int width , int height, int x0, int y0, int x, int y, int color)
{

int y2 = y + y0;
int x2 = x + x0;
if(y2 < 0 || y2 > height){
return -1;
}
if(x2 < 0 || x2 > width){
return -1;
}
int offset = y2  * width;
unsigned char *p = data + offset + x2 ;
p[0] = color;
return 0;
}

int draw_circle(unsigned char *data,  int width, int height, int x0, int y0, int r, int color)
{
int x, y;
int r2 = r*r;

int prev = 0;
int y2 = r2;
for(x = 0, y = r; x <= r && y >= 0; x++){
int diff;
int diff2;
int x2 = prev;// = x^2
int x21 = x2 + x + x + 1;// = (x + 1)^2
prev = x21;
while(y >= 0){
diff = x2 + y2 - r2;
diff2 = x21 + y2 - r2;
draw_pixel(data,  width, height, x0, y0, x, y, color);
draw_pixel(data,  width, height, x0, y0, -x, y, color);
draw_pixel(data,  width, height, x0, y0, x, -y, color);
draw_pixel(data,  width, height, x0, y0, -x, -y, color);
if(abs(diff) <= abs(diff2)){
y2 = y2 - y - y + 1; // = (y - 1) ^ 2
y--;
}else{
break;
}
}
}
return 0;
}

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

03-17 5775

05-28 794
03-20 962
06-07
09-24
05-23
04-25 555
01-23 1384
07-13 9180
10-17 1万+
11-20 5391
09-24 1万+