计算机图形学中点圆生成代码,计算机图形中点圆算法

它基于以下功能, 用于测试任意点(x, y)与以原点为中心的半径r的圆之间的空间关系:

0a10e500d8201fb262ff6c0c27f6746e.png

b4e882ce43dbb3ff427ed6e737b31583.png

现在, 考虑像素T和像素S之间的中间点的坐标

这称为中点(xi + 1, yi-), 我们用它来定义决策参数:

Pi=f (xi+1, yi-) = (xi+1)2+(yi-)2-r2 ……………equation 2

如果Pi是-ve⟹中点在圆内, 我们选择像素T

如果Pi是+ ve⟹中点在圆之外(或在圆上), 我们选择像素S。

下一步的决策参数是:

Pi+1=(xi+1+1)2+(yi+1-)2- r2…………equation 3

由于xi + 1 = xi + 1, 我们有

28d4821394cd563c4e57afc364fd4a9a.png

如果选择像素T⟹Pi<0

我们有yi + 1 = yi

如果选择像素S⟹Pi≥0

我们有yi + 1 = yi-1

d9834d1c14c5bf5980cdaa2068f73d4a.png

我们可以继续用(xi, yi)的n来简化它, 得到

5207caf0da7aa9414d53155326f9a92e.png

现在, 由等式2得出的Pi(0, r)的初始值

3bb3b9248eb7f2e33acaae227b2a7f8d.png

我们可以把≅1∴r是一个整数所以, P1 = 1-r

算法

步骤1:将x = 0, y = r代入等式2中, 我们有p = 1-r

步骤2:在x≤y的情况下重复步骤x(y, y)如果(p <0)然后设置p = p + 2x + 3其他p = p + 2(xy)+5 y = y-1(如果结束)x = x + 1(结束循环)

第三步:结束

程序使用中点算法绘制圆:

#include

#include

#include

#include

#include

#include

class bresen

{

float x, y, a, b, r, p;

public:

void get ();

void cal ();

};

void main ()

{

bresen b;

b.get ();

b.cal ();

getch ();

}

Void bresen :: get ()

{

cout<

cout<< "ENTER (a, b)";

cin>>a>>b;

cout<

cin>>r;

}

void bresen ::cal ()

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

int midx, midy, i;

/* initialize graphics and local variables */

initgraph (&gdriver, &gmode, " ");

/* read result of initialization */

errorcode = graphresult ();

if (errorcode ! = grOK) /*an error occurred */

{

printf("Graphics error: %s \n", grapherrormsg (errorcode);

printf ("Press any key to halt:");

getch ();

exit (1); /* terminate with an error code */

}

x=0;

y=r;

putpixel (a, b+r, RED);

putpixel (a, b-r, RED);

putpixel (a-r, b, RED);

putpixel (a+r, b, RED);

p=5/4)-r;

while (x<=y)

{

If (p<0)

p+= (4*x)+6;

else

{

p+=(2*(x-y))+5;

y--;

}

x++;

putpixel (a+x, b+y, RED);

putpixel (a-x, b+y, RED);

putpixel (a+x, b-y, RED);

putpixel (a+x, b-y, RED);

putpixel (a+x, b+y, RED);

putpixel (a+x, b-y, RED);

putpixel (a-x, b+y, RED);

putpixel (a-x, b-y, RED);

}

}

输出:

e6ff76e8d1d36346d999c3dba00f8846.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值