近些天写了一些关于计算机图形学的算法和源代码!
如果喜欢转载请标明出处:并非菜鸟的博客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; -
}