• 圆的角度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毫秒,没法看。

  • 相关阅读:
    技术债务墙:一种让技术债务可见并可协商的方法
    墙裂推荐
    shell 脚本通过Webhook 发送消息到微信群
    关于中医的一段对话 [ZZ] -- 思维训练故事
    应用深度神经网络预测学生期末成绩
    Python中的模块引用机制
    批量修改含空格的文件名「Linux」
    Markdown数学公式语法
    批处理修改IP
    FTD团队目录
  • 原文地址:https://www.cnblogs.com/weiweishuo/p/2955936.html
Copyright © 2020-2023  润新知