Advertisement
NightFox

Dibuja un circulo (Implementacion del algoritmo de Bresenham)

Nov 1st, 2020
1,500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | None | 0 0
  1. /*** Dibuja un circulo (Implementacion del algoritmo de Bresenham)***/
  2. void NGN_ProDraw::Circle(int32_t cx, int32_t cy, int32_t r, uint32_t color, int32_t ry, double in_angle, double out_angle) {
  3.  
  4.     // Asignacion de los radios y proteccion de 0
  5.     int32_t _rx = (r < 1) ? 1:r;
  6.     int32_t _ry = (ry == NGN_DEFAULT_VALUE) ? _rx:ry;
  7.     _ry = (_ry < 1) ? 1:_ry;
  8.  
  9.     // Calculos
  10.     int32_t right = (surface_width - 1);                // Limites del buffer
  11.     int32_t bottom = (surface_height - 1);
  12.     int32_t x = _rx;                                    // Coordenadas
  13.     int32_t y = 0;
  14.     int32_t ix = ((_ry * _ry) * (1 - (_rx << 1)));      // Cambios de coordenada
  15.     int32_t iy = (_rx * _rx);
  16.     int32_t square_x = ((_rx * _rx) << 1);              // Cuadrados
  17.     int32_t square_y = ((_ry * _ry) << 1);
  18.     int32_t stop_x = (square_y * _rx);                  // Puntos de detencion
  19.     int32_t stop_y = 0;
  20.     int32_t ellip_error = 0;                            // Error en el dibujado de la elipse
  21.  
  22.     // Variables adicionales
  23.     int32_t _x = 0, _y = 0;
  24.  
  25.     // Bloquea el surface
  26.     SDL_LockSurface(surface);
  27.     // Acceso al array de pixeles
  28.     uint32_t* p = (uint32_t*)surface->pixels;
  29.  
  30.     // Primera area de dibujado (izquierda y derecha)
  31.     while (stop_x >= stop_y) {
  32.         // Dibuja el pixel del cuadrante 1
  33.         _x = cx + x;
  34.         _y = cy + y;
  35.         if (!((_x < 0) || (_y < 0) || (_x > right) || (_y > bottom))) {
  36.             p[((_y * surface_width) + _x)] = color;
  37.         }
  38.         // Dibuja el pixel del cuadrante 2
  39.         _x = cx - x;
  40.         _y = cy + y;
  41.         if (!((_x < 0) || (_y < 0) || (_x > right) || (_y > bottom))) {
  42.             p[((_y * surface_width) + _x)] = color;
  43.         }
  44.         // Dibuja el pixel del cuadrante 3
  45.         _x = cx - x;
  46.         _y = cy - y;
  47.         if (!((_x < 0) || (_y < 0) || (_x > right) || (_y > bottom))) {
  48.             p[((_y * surface_width) + _x)] = color;
  49.         }
  50.         // Dibuja el pixel del cuadrante 4
  51.         _x = cx + x;
  52.         _y = cy - y;
  53.         if (!((_x < 0) || (_y < 0) || (_x > right) || (_y > bottom))) {
  54.             p[((_y * surface_width) + _x)] = color;
  55.         }
  56.         // Incrementa la Y
  57.         y ++;
  58.         // Incrementa stop_y
  59.         stop_y += square_x;
  60.         // Incrementa el error de elipse
  61.         ellip_error += iy;
  62.         // Incrementa el incremento de Y
  63.         iy += square_x;
  64.         // Correccion de valores
  65.         if (((ellip_error << 1) + ix) > 0) {
  66.             // Reduce X
  67.             x --;
  68.             // Reduce stop_x
  69.             stop_x -= square_y;
  70.             // Incrementa el error de elipse
  71.             ellip_error += ix;
  72.             // Incremente el incremento de X
  73.             ix += square_y;
  74.         }
  75.     }
  76.  
  77.     // Prepara la segunda area (arriba y abajo)
  78.     x = 0;
  79.     y = _ry;
  80.     ix = (_ry * _ry);
  81.     iy = ((_rx * _rx) * (1 - (_ry << 1)));
  82.     ellip_error = 0;
  83.     stop_x = 0;
  84.     stop_y = (square_x * _ry);
  85.  
  86.     // Segunda area de dibujado (arriba y abajo)
  87.     while (stop_x <= stop_y) {
  88.         // Dibuja el pixel del cuadrante 1
  89.         _x = cx + x;
  90.         _y = cy + y;
  91.         if (!((_x < 0) || (_y < 0) || (_x > right) || (_y > bottom))) {
  92.             p[((_y * surface_width) + _x)] = color;
  93.         }
  94.         // Dibuja el pixel del cuadrante 2
  95.         _x = cx - x;
  96.         _y = cy + y;
  97.         if (!((_x < 0) || (_y < 0) || (_x > right) || (_y > bottom))) {
  98.             p[((_y * surface_width) + _x)] = color;
  99.         }
  100.         // Dibuja el pixel del cuadrante 3
  101.         _x = cx - x;
  102.         _y = cy - y;
  103.         if (!((_x < 0) || (_y < 0) || (_x > right) || (_y > bottom))) {
  104.             p[((_y * surface_width) + _x)] = color;
  105.         }
  106.         // Dibuja el pixel del cuadrante 4
  107.         _x = cx + x;
  108.         _y = cy - y;
  109.         if (!((_x < 0) || (_y < 0) || (_x > right) || (_y > bottom))) {
  110.             p[((_y * surface_width) + _x)] = color;
  111.         }
  112.         // Incrementa la X
  113.         x ++;
  114.         // Incrementa stop_x
  115.         stop_x += square_y;
  116.         // Incrementa el error de elipse
  117.         ellip_error += ix;
  118.         // Incrementa el incremento de X
  119.         ix += square_y;
  120.         // Correccion de valores
  121.         if (((ellip_error << 1) + iy) > 0) {
  122.             // Reduce Y
  123.             y --;
  124.             // Reduce stop_y
  125.             stop_y -= square_x;
  126.             // Incrementa el error de elipse
  127.             ellip_error += iy;
  128.             // Incremente el incremento de Y
  129.             iy += square_x;
  130.         }
  131.     }
  132.  
  133.     // Desbloquea el surface
  134.     SDL_UnlockSurface(surface);
  135.  
  136.     // Indica el blit
  137.     blit = true;
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement