[SDL] "Perfect" circles

xEsk PiV xeskuu.sdl at gmail.com
Tue Oct 31 13:01:38 PST 2006


Hello!

Somebody knows why this code does not make "perfect" circles? How I
can improve the precision? I have tested this same code in Delphi and
works perfectly. Can it be problem of the SDL_Rect? I'm using C++.

I have seen some programs done in SDL that make this perfectly!

Sorry, I know isn't a specific SDL question, but I'm desperate.

Code:

float pre_cos[360];
float pre_sin[360];
bool is_pre_cos_sin_init = false;

const float PI = 3.14159265358979323846;

float RadToDeg(float rad) { return rad * 180 / PI; }
float DegToRad(float deg) { return deg * PI / 180; }

void init_pre_math()
{
  for(int i = 0; i <= 360; i++)
  {
    pre_cos[i] = sin(DegToRad(i));
    pre_sin[i] = cos(DegToRad(i));
  }

  is_pre_cos_sin_init = true;
}

SDL_Rect advance(SDL_Rect pos, float angle, int units)
{
  if (!is_pre_cos_sin_init) init_pre_math();

  int ang = static_cast<int>(-angle);
  ang = (ang < 0) ? (ang % 360) + 360: ang % 360;

  pos.x = pos.x + static_cast<Sint16>(units * pre_cos[ang]);
  pos.y = pos.y + static_cast<Sint16>(units * pre_sin[ang]);

  return pos;
}




More information about the SDL mailing list