C语言用Bresenham算法画圆,哪位高手教教,主要是算法里的内容,谢谢!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言用Bresenham算法画圆,哪位高手教教,主要是算法里的内容,谢谢!相关的知识,希望对你有一定的参考价值。

主要是这个算法里头,坐标变换的意思,及如何变换,怎么用!请高手详细讲解,谢谢!

或者也可以看看下面我搜索到的Bresenham画圆算法,不过一点都不懂里面那一堆变量怎么用,只知道是画点组成的圆,不过为什么,原理是什么?

void BresenhemCircle(int centerx, int centery, int radius, int color, int type)

int x =type= 0;
int y = radius;
int delta = 2*(1-radius);
int direction;
while (y >= 0)
if (!type)
putpixel(centerx+x, centery+y, color);
putpixel(centerx-x, centery+y, color);
putpixel(centerx-x, centery-y, color);
putpixel(centerx+x, centery-y, color);

else
line(centerx+x, centery+y, centerx+x, centery-y);
line(centerx-x, centery+y, centerx-x, centery-y);

if (delta < 0)
if ((2*(delta+y)-1) < 0)
direction = 1;

else
direction = 2;


else if(delta > 0)
if ((2*(delta-x)-1) <= 0)
direction = 2;

else
direction = 3;


else
direction=2;


switch(direction)
case 1:
x++;
delta += (2*x+1);
break;
case 2:
x++;
y--;
delta += 2*(x-y+1);
break;
case 3:
y--;
delta += (-2*y+1);
break;




主要是那个八分法里坐标变换的依据,delta 为什么那样判断,那样计算?

的确哈,关键在于对delta的理解
可以看到,都是delta=2*(1-radius)这样的,起作用应该是判断要画的点x、y坐标的变化趋势,先把我注释了的代码贴下,加了getch();可以看到画的过程
-----------------------------------------------------------------
#include<graphics.h>
#include<stdio.h>

void BresenhemCircle(int centerx, int centery, int radius, int color, int type);

void main()

int drive=DETECT,mode;
int i,j;
initgraph(&drive,&mode,"");
BresenhemCircle(300,200,100,15,0);
getch();


void BresenhemCircle(int centerx, int centery, int radius, int color, int type)

int x =type = 0;/*初始横坐标为原点*/
int y = radius; /*初始纵坐标远离原点*/
int delta = 2*(1-radius);
int direction;
while (y >= 0)

getch();
if (!type)/*执行*/

/*在上半圆画两点*/
putpixel(centerx+x, centery+y, color);
putpixel(centerx-x, centery+y, color);
/*在下半圆画两点*/
putpixel(centerx-x, centery-y, color);
putpixel(centerx+x, centery-y, color);
getch();

else/*不执行*/

line(centerx+x, centery+y, centerx+x, centery-y);
line(centerx-x, centery+y, centerx-x, centery-y);
getch();

/*以下代码设置下次四点的位置,圆是对称的,且此方法相当于同时画四个圆弧
观察右上方圆弧可知,前一半是x增的要快些,后一半是y减的快些*/
if (delta < 0)

if ((2*(delta+y)-1) < 0)
direction = 1; /*选择横向加*/
else
direction = 2;

else if(delta > 0)

if ((2*(delta-x)-1) > 0)
direction = 3; /*选择纵向减*/
else
direction = 2;

else
direction=2;

switch(direction)

case 1:
x++;/*只横坐标远离原点*/
delta += (2*x+1); /*小执行到这,所以加*/
break;
case 2:
x++;
y--;/*横向远离,同时纵向靠近*/
delta += 2*(x-y+1); /*即(2*x+1)+(-2*y+1)*/
break;
case 3:
y--;/*只纵坐标靠近原点*/
delta += (-2*y+1); /*大执行到这,所以减*/
break;


参考技术A #include <math.h>
#include <graphics.h> /*预定义库函数*/
void circlePoint(int x,int y) /*八分法画圆程序*/

circle(320+x*20,240+y*20,3);
circle(320+y*20,240+x*20,3);
circle(320-y*20,240+x*20,3);
circle(320-x*20,240+y*20,3);
circle(320-x*20,240+y*20,3);
circle(320-x*20,240-y*20,3);
circle(320-y*20,240-x*20,3);
circle(320+y*20,240-x*20,3);
circle(320+x*20,240-y*20,3);

void MidBresenhamcircle(int r) /* 中点Bresenham算法画圆的程序 */

int x,y,d;
x=0;y=r;d=1-r; /* 计算初始值 */
while(x<y)
circlePoint(x,y); /* 绘制点(x,y)及其在八分圆中的另外7个对称点 */
if(d<0) d+=2*x+3; /* 根据误差项d的判断,决定非最大位移方向上是走还是不走 */
else
d+=2*(x-y)+5;
y--;

x++;
delay(900000);
/* while */

main()

int i,j,r,graphmode,graphdriver;
detectgraph(&graphdriver,&graphmode);
initgraph(&graphdriver,&graphmode," ");
printf("中点Bresenhamcircle算法画圆的程序"); /*提示信息*/
printf("注意 |r|<=11");
printf("输入半径值 r:");
scanf("%d",&r);
printf("按任意键显示图形...");
getch();
cleardevice();
setbkcolor(BLACK);
for(i=20;i<=620;i+=20) /*使用双循环画点函数画出表格中的纵坐标*/
for(j=20;j<=460;j++)
putpixel(i,j,2);
for(j=20;j<=460;j+=20) /*使用双循环画点函数画出表格中的横坐标*/
for(i=20;i<=620;i++)
putpixel(i,j,2);
outtextxy(320,245,"0"); /*原点坐标*/
outtextxy(320-5*20,245,"-5");circle(320-5*20,240,2); /*横坐标值*/
outtextxy(320+5*20,245,"5");circle(320+5*20,240,2);
outtextxy(320-10*20,245,"-10");circle(320-10*20,240,2);
outtextxy(320+10*20,245,"10");circle(320+10*20,240,2);
outtextxy(320-15*20,245,"-15");circle(320-15*20,240,2);
outtextxy(320+15*20,245,"15");circle(320+15*20,240,2);
outtextxy(320,240-5*20,"-5");circle(320,240-5*20,2); /*纵坐标值*/
outtextxy(320,240+5*20,"5");circle(320,240+5*20,2);
outtextxy(320,240-10*20,"-10");circle(320,240-10*20,2);
outtextxy(320,240+10*20,"10");circle(320,240+10*20,2);
outtextxy(20,10,"The center of the circle is (0,0) "); /*坐标轴左上角显示提示信息*/
setcolor(RED); /*标记坐标轴*/
line(20,240,620,240); outtextxy(320+15*20,230,"X");
line(320,20,320,460); outtextxy(330,20,"Y");
setcolor(YELLOW)

麻烦哪位高手帮忙解释一下c语言中gcd函数的用法,最好整点简单的例子,谢啦

参考技术A c语言中没有gcd函数,要自己编。本回答被提问者采纳

以上是关于C语言用Bresenham算法画圆,哪位高手教教,主要是算法里的内容,谢谢!的主要内容,如果未能解决你的问题,请参考以下文章

bresenham算法的Bresenham改进算法

计算机图形学中的中点画线,中点画圆,Bresenham画线与画圆算法

用C实现Bresenham算法生成直线和圆的程序(要求具体步骤有必要解述)

麻烦哪位高手解释一下这个C语言程序中,那个word 有啥用?

哪位高手帮我把汇编语言程序转换成c语言程序啊?(对了是51单片机程序)

哪位高手知道DDS的波形查找表怎么生成?c语言怎么写?用VHDL又怎么弄?还有怎么调用ROM