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

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

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

源代码:源代码在这里!

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

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

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

  2. {

  3. CClientDC dc(this);

  4. int oldmode=dc.SetROP2(R2_NOTXORPEN);

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

  6. int x,y,cx,cy;

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

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

  9. float d; //判别D

  10. x=0;

  11. y=r;

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

  13. 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));

  14. 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));

  15. 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));

  16. 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));

  17. while(x<y)

  18. {

  19. if(d<0)

  20. d=d+2*x+3;

  21. else

  22. {

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

  24. y--;

  25. }

  26. x++;

  27. CirclePoint(cx,cy,x,y);

  28. }

  29. dc.SetROP2(oldmode);

  30. }

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

  32. {

  33. CClientDC dc(this);

  34. int oldmode=dc.SetROP2(R2_NOTXORPEN);

  35. if(y==x)

  36. {

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

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

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

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

  41. }

  42. else if(x!=y+gaps)

  43. {

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

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

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

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

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

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

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

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

  52. }

  53. dc.SetROP2(oldmode);

  54.  }

 

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

 

以上是效果图

 

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

  2. {

  3. CClientDC dc(this);

  4. int oldmode=dc.SetROP2(R2_NOTXORPEN);

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

  6. int x,y,cx,cy;

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

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

  9. float d;

  10. x=0;

  11. y=r;

  12. d=1.25-r;

  13. 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));

  14. 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));

  15. 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));

  16. 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));

  17. while(x<y)

  18. {

  19.  
  20. if(d<0)

  21. d=d+2*x+3;

  22. else

  23. {

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

  25. y-=gaps;

  26. }

  27. x+=gaps;

  28. CirclePoint(cx,cy,x,y);

  29. }

  30. dc.SetROP2(oldmode);

  31. }

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

  2. {

  3. CClientDC dc(this);

  4. int oldmode=dc.SetROP2(R2_NOTXORPEN);

  5. if(y==x)

  6. {

  7. 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));

  8. 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));

  9. 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));

  10. 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));

  11. }

  12. else if(x!=y+gaps)

  13. {

  14. 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));

  15. 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));

  16. 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));

  17. 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));

  18. 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));

  19. 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));

  20. 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));

  21. 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));

  22.  
  23. }

  24. //*/

  25. dc.SetROP2(oldmode);

  26. }

 

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

  2. {

  3. if(x%gaps>gaps/2)

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

  5. else

  6. x=int(x/gaps)*gaps;

  7. return x;

  8. }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值