圆的角度DDA算法初试

  听老师讲到dx=-r*sini*di;dy=r*cosi*di(i表示角度),就关掉视频干别的了。早起猜了猜后续算法,核心思想应该是:不同半径的圆周长不同,为保证长为C的圆周描绘连续,保证有C个像素点,这样di=2PI/C=r,即圆心角每增加di,圆周长增加1像素.从公式看出,r越大,圆心角细分的越厉害,i的累加误差越小。我测了下r=10情形下,累加误差达到0.3,但r=100时,累加误差就只0.03了。

  按照如下坐标系:

  只需算出区域1的像素点,再对称出2区域,其余区域就可以由区域1,2做x/y对称得到。

  流程图画的很丑:

下面是代码:

void drawCircle_dda(int x0,int y0,int r,char color){
    double di=1/(double)r;
    double x=r;
    double y=0;
    *(pt_memBuffer+((int)(x+0.5)<<2)+(int)(y+0.5)*bytes_w)=color;
    int x_round,y_round;
    for(double i=0;i<=PI/4;i+=di){//the logical-starting radian should be di.
        x_round=(int)(x+0.5);
        y_round=(int)(y+0.5);
        *(pt_memBuffer+(x_round+x0<<2)+(y_round+y0)*bytes_w)=color;//1  即drawPixel(x_round+x0,y_round+y0)
        *(pt_memBuffer+(LOWPOINT_X+x0<<2)+(LOWPOINT_Y+y0)*bytes_w)=color;//2<-1 即drawPixel(LOWPOINT_X+x0,LOWpOINT_Y+y0),下面类似
        *(pt_memBuffer+(-LOWPOINT_X+x0<<2)+(LOWPOINT_Y+y0)*bytes_w)=color;//3<-2
        *(pt_memBuffer+(-x_round+x0<<2)+(y_round+y0)*bytes_w)=color;//4<-1
        *(pt_memBuffer+(-x_round+x0<<2)+(-y_round+y0)*bytes_w)=color;//5<-1
        *(pt_memBuffer+(-LOWPOINT_X+x0<<2)+(-LOWPOINT_Y+y0)*bytes_w)=color;//6<-2
        *(pt_memBuffer+(LOWPOINT_X+x0<<2)+(-LOWPOINT_Y+y0)*bytes_w)=color;//7<-2
        *(pt_memBuffer+(x_round+x0<<2)+(-y_round+y0)*bytes_w)=color;//8<-1
        x+=-sin(i);//di=1/r,dx=-sin(i);
        y+=cos(i);//di=1/r,dy=cos(i);
    }
}

调用50000次,耗时220毫秒,没法看。

 

posted on 2013-03-12 16:20  weiweishuo  阅读(1091)  评论(0编辑  收藏  举报

导航