计算机图形学(二)中点画圆算法讲解与源代码

发布时间:2024-11-21 05:07

虚拟现实技术的核心是计算机图形学和人工智能算法。 #生活知识# #科技生活# #虚拟现实技术#

计算机图形学(二)中点画圆算法讲解与源代码

最新推荐文章于 2024-09-21 13:57:27 发布

林小竹 于 2018-08-15 11:24:50 发布

近些天写了一些关于计算机图形学的算法和源代码!

如果喜欢转载请标明出处:并非菜鸟的博客http://blog.csdn.net/syx1065001748

源代码:源代码在这里!

关于中点画圆,大家都知道是根据圆的8分对称性质,然后画出1/8圆之后再进行对称画点,就可以得到完整的圆了。首先给出圆的一般算法,是使用中点画圆的统法,在原点画圆!然后进行平移来得到!

下边是使用橡皮筋的方法实现的画圆方法!

 

void MidPointCircle(int x0,int y0,int x1,int y1)  //(x0,y0)鼠标左键落下的点和(x1,y1)MouseMove的点

{

CClientDC dc(this);

int oldmode=dc.SetROP2(R2_NOTXORPEN);

int r=(max(x1,x0)-min(x1,x0))/2;//求的圆心

int x,y,cx,cy;

cx=(x1+x0)/2;//圆心X坐标

cy=(y0+y1)/2;//圆心Y坐标

float d; //判别D

x=0;

y=r;

d=1.25-r;//避免取反无法显示</span>

dc.Ellipse(x+cx-int(gaps/2.0),y+cy+int(gaps/2.0),x+cx+int(gaps/2.0),y+cy-int(gaps/2.0));

dc.Ellipse(y+cx-int(gaps/2.0),x+cy+int(gaps/2.0),y+cx+int(gaps/2.0),x+cy-int(gaps/2.0));

dc.Ellipse(x+cx-int(gaps/2.0),-y+cy+int(gaps/2.0),x+cx+int(gaps/2.0),-y+cy-int(gaps/2.0));

dc.Ellipse(-y+cx-int(gaps/2.0),x+cy+int(gaps/2.0),-y+cx+int(gaps/2.0),x+cy-int(gaps/2.0));

while(x<y)

{

if(d<0)

d=d+2*x+3;

else

{

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

y--;

}

x++;

CirclePoint(cx,cy,x,y);

}

dc.SetROP2(oldmode);

}

void CirclePoint(int cx,int cy,int x, int y)//辅助画点

{

CClientDC dc(this);

int oldmode=dc.SetROP2(R2_NOTXORPEN);

if(y==x)

{

dc.SetPixel(x+cx,y+cy,RGB(255,255,0));

dc.SetPixel(x+cx,-y+cy,RGB(255,255,0);

dc.SetPixel(-x+cx,-y+cy,RGB(255,255,0));

dc.SetPixel(-x+cx,y+cy,RGB(255,255,0));

}

else if(x!=y+gaps)

{

dc.SetPixel(x+cx,y+cy,RGB(255,255,0));

dc.SetPixel(-x+cx,y+cy,RGB(255,255,0));

dc.SetPixel(x+cx,-y+cy,RGB(255,255,0));

dc.SetPixel(y+cx,x+cy,RGB(255,255,0));

dc.SetPixel(-y+cx,x+cy,RGB(255,255,0));

dc.SetPixel(-y+cx,-x+cy,RGB(255,255,0));

dc.SetPixel(-x+cx,-y+cy,RGB(255,255,0));

dc.SetPixel(y+cx,-x+cy,RGB(255,255,0));

}

dc.SetROP2(oldmode);

 }

 

以上是画圆算法的一般情况其中CX和CY分别是经过圆心(0,0),平移过后的圆心也就是偏移量!然后给一段关于如何将画圆显示在自己设置的像素坐标系中

以上是效果图

 

MidPointCircle(int x0,int y0,int x1,int y1) //(x0,y0)和(x1,y1)两个点数遍落下两个点

{

CClientDC dc(this);

int oldmode=dc.SetROP2(R2_NOTXORPEN);

int r=Ajust((maxn(x1,x0)-mine(x1,x0))/2,gaps);

int x,y,cx,cy;

cx=Ajust((x1+x0)/2,gaps);

cy=Ajust((y0+y1)/2,gaps);

float d;

x=0;

y=r;

d=1.25-r;

dc.Ellipse(x+cx-int(gaps/2.0),y+cy+int(gaps/2.0),x+cx+int(gaps/2.0),y+cy-int(gaps/2.0));

dc.Ellipse(y+cx-int(gaps/2.0),x+cy+int(gaps/2.0),y+cx+int(gaps/2.0),x+cy-int(gaps/2.0));

dc.Ellipse(x+cx-int(gaps/2.0),-y+cy+int(gaps/2.0),x+cx+int(gaps/2.0),-y+cy-int(gaps/2.0));

dc.Ellipse(-y+cx-int(gaps/2.0),x+cy+int(gaps/2.0),-y+cx+int(gaps/2.0),x+cy-int(gaps/2.0));

while(x<y)

{

 

if(d<0)

d=d+2*x+3;

else

{

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

y-=gaps;

}

x+=gaps;

CirclePoint(cx,cy,x,y);

}

dc.SetROP2(oldmode);

}

 

CirclePoint(int cx,int cy,int x, int y)

{

CClientDC dc(this);

int oldmode=dc.SetROP2(R2_NOTXORPEN);

if(y==x)

{

dc.Ellipse(x+cx-int(gaps/2.0),y+cy+int(gaps/2.0),x+cx+int(gaps/2.0),y+cy-int(gaps/2.0));

dc.Ellipse(x+cx-int(gaps/2.0),-y+cy+int(gaps/2.0),x+cx+int(gaps/2.0),-y+cy-int(gaps/2.0));

dc.Ellipse(-x+cx-int(gaps/2.0),-y+cy+int(gaps/2.0),-x+cx+int(gaps/2.0),-y+cy-int(gaps/2.0));

dc.Ellipse(-x+cx-int(gaps/2.0),y+cy+int(gaps/2.0),-x+cx+int(gaps/2.0),y+cy-int(gaps/2.0));

}

else if(x!=y+gaps)

{

dc.Ellipse(x+cx-int(gaps/2.0),y+cy+int(gaps/2.0),x+cx+int(gaps/2.0),y+cy-int(gaps/2.0));

dc.Ellipse(-x+cx-int(gaps/2.0),y+cy+int(gaps/2.0),-x+cx+int(gaps/2.0),y+cy-int(gaps/2.0));

dc.Ellipse(x+cx-int(gaps/2.0),-y+cy+int(gaps/2.0),x+cx+int(gaps/2.0),-y+cy-int(gaps/2.0));

dc.Ellipse(y+cx-int(gaps/2.0),x+cy+int(gaps/2.0),y+cx+int(gaps/2.0),x+cy-int(gaps/2.0));

dc.Ellipse(-y+cx-int(gaps/2.0),x+cy+int(gaps/2.0),-y+cx+int(gaps/2.0),x+cy-int(gaps/2.0));

dc.Ellipse(-y+cx-int(gaps/2.0),-x+cy+int(gaps/2.0),-y+cx+int(gaps/2.0),-x+cy-int(gaps/2.0));

dc.Ellipse(-x+cx-int(gaps/2.0),-y+cy+int(gaps/2.0),-x+cx+int(gaps/2.0),-y+cy-int(gaps/2.0));

dc.Ellipse(y+cx-int(gaps/2.0),-x+cy+int(gaps/2.0),y+cx+int(gaps/2.0),-x+cy-int(gaps/2.0));

 

}

//*/

dc.SetROP2(oldmode);

}

 

Ajust(int x,int gaps) //调整x,y到自己的方格点上! gaps是方格大小!

{

if(x%gaps>gaps/2)

x=int(x/gaps+1)*gaps;

else

x=int(x/gaps)*gaps;

return x;

}

网址:计算机图形学(二)中点画圆算法讲解与源代码 https://www.yuejiaxmz.com/news/view/168138

相关内容

Java计算机毕业设计基于的二手图书交易系统设计与实现(开题报告+源码+论文)
turtle画图代码大全
计算机组装与维护心得体会(通用10篇)
计算机与数码设备维修
【计算机毕设论文】基于SpringBoot图片热量估计系统
计算机组装与维护实训总结范文(精选16篇)
基于微信小程序的校园二手图书交易小程序设计与实现(源码+lw+部署+讲解)
中职计算机与数码设备维修专业
计算机学习心得体会(精选9篇)
JAVA计算机毕业设计基于Springboot的在线教育平台的设计与实现(附源码+springboot+开题+论文)

随便看看