计算机图形学(⼆)中点画圆算法讲解与源代码
近些天写了⼀些关于计算机图形学的算法和源代码!
源代码:
关于中点画圆,⼤家都知道是根据圆的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
0. x=0;
1. y=r;
2. d=1.25-r;//避免取反⽆法显⽰</span>
3. 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));
4. 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));
5. 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));
6. 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));
7. while(x<y)
8. {
9. if(d<0)
0. d=d+2*x+3;
1. else
2. {
3. d=d+2*(x-y)+5;
4. y--;
5. }
6. x++;
7. CirclePoint(cx,cy,x,y);
8. }
9. dc.SetROP2(oldmode);
0. }
1. void 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.SetPixel(x+cx,y+cy,RGB(255,255,0));
8. dc.SetPixel(x+cx,-y+cy,RGB(255,255,0);
9. dc.SetPixel(-x+cx,-y+cy,RGB(255,255,0));
0. dc.SetPixel(-x+cx,y+cy,RGB(255,255,0));
1. }
2. else if(x!=y+gaps)
3. {
4. dc.SetPixel(x+cx,y+cy,RGB(255,255,0));
5. dc.SetPixel(-x+cx,y+cy,RGB(255,255,0));
6. dc.SetPixel(x+cx,-y+cy,RGB(255,255,0));
7. dc.SetPixel(y+cx,x+cy,RGB(255,255,0));
8. dc.SetPixel(-y+cx,x+cy,RGB(255,255,0));
9. dc.SetPixel(-y+cx,-x+cy,RGB(255,255,0));
0. dc.SetPixel(-x+cx,-y+cy,RGB(255,255,0));
1. dc.SetPixel(y+cx,-x+cy,RGB(255,255,0));
2. }
3. dc.SetROP2(oldmode);
源代码电影讲解4.  }
以上是画圆算法的⼀般情况其中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;
0. x=0;
1. y=r;
2. d=1.25-r;
3. 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));
4. 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));
5. 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));
6. 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));
7. while(x<y)
8. {
19.
0. if(d<0)
1. d=d+2*x+3;
2. else
3. {
4. d=d+2*(x-y)+5;
5. y-=gaps;
6. }
7. x+=gaps;
8. CirclePoint(cx,cy,x,y);
9. }
0. dc.SetROP2(oldmode);
1. }
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));
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));
1. }
2. else if(x!=y+gaps)
3. {
4. 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));
5. 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));
6. 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));
7. 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));
8. 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));
9. 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));
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));
1. 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.
3. }
4. //*/
5. dc.SetROP2(oldmode);
6. }
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. }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。