HOWTO draw circles, arcs and vector graphics in SDL?

前端 未结 4 2042
一个人的身影
一个人的身影 2021-02-02 15:43

(I\'m using SDL2)

SDL is a relatively small library for \"low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D\" It\'s us

4条回答
  •  礼貌的吻别
    2021-02-02 16:08

    This is an example of the Midpoint Circle Algorithm as referenced above. It doesn't require a math library and is very fast. (Renders in about 500 microseconds) This is what Windows uses/used to rasterize circles.

    void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
    {
       const int32_t diameter = (radius * 2);
    
       int32_t x = (radius - 1);
       int32_t y = 0;
       int32_t tx = 1;
       int32_t ty = 1;
       int32_t error = (tx - diameter);
    
       while (x >= y)
       {
          //  Each of the following renders an octant of the circle
          SDL_RenderDrawPoint(renderer, centreX + x, centreY - y);
          SDL_RenderDrawPoint(renderer, centreX + x, centreY + y);
          SDL_RenderDrawPoint(renderer, centreX - x, centreY - y);
          SDL_RenderDrawPoint(renderer, centreX - x, centreY + y);
          SDL_RenderDrawPoint(renderer, centreX + y, centreY - x);
          SDL_RenderDrawPoint(renderer, centreX + y, centreY + x);
          SDL_RenderDrawPoint(renderer, centreX - y, centreY - x);
          SDL_RenderDrawPoint(renderer, centreX - y, centreY + x);
    
          if (error <= 0)
          {
             ++y;
             error += ty;
             ty += 2;
          }
    
          if (error > 0)
          {
             --x;
             tx += 2;
             error += (tx - diameter);
          }
       }
    }
    

提交回复
热议问题