Advertisement
NightFox

Dibuja un arco

Nov 3rd, 2020
2,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. /*** Dibuja un arco ***/
  2. void NGN_ProDraw::Arc(int32_t cx, int32_t cy, int32_t r, double start_angle, double end_angle, uint32_t color, int32_t ry, uint8_t close) {
  3.  
  4.     // Asignacion de los radios y proteccion de 0
  5.     int32_t _rx = r;
  6.     int32_t _ry = (ry == NGN_DEFAULT_VALUE) ? _rx:ry;
  7.  
  8.     // Ordena los angulos de inicio y fin
  9.     double st_angle = 0.0f, ed_angle = 0.0f;
  10.     if (start_angle == end_angle) {
  11.         // Si son identicos, sal
  12.         return;
  13.     } else if (start_angle > end_angle) {
  14.         // Si el angulo inicial es mayor que el final
  15.         st_angle = start_angle;
  16.         ed_angle = end_angle += (PI * 2.0f);
  17.     } else {
  18.         st_angle = start_angle;
  19.         ed_angle = end_angle;
  20.     }
  21.  
  22.     // Algun radio es 0...
  23.     if ((_rx == 0) && (_ry == 0)) {
  24.         return;
  25.     }
  26.  
  27.     // Calculos
  28.     int32_t x = 0, y = 0;                               // Coordenadas
  29.     int32_t _x = 0, _y = 0;
  30.     int32_t _fx = 0, _fy = 0;
  31.  
  32.     // Precision
  33.     double p = 0.0f;
  34.     if (_rx >= _ry) {
  35.         p = (PI * 4.0f) / (double)_rx;
  36.     } else {
  37.         p = (PI * 4.0f) / (double)_ry;
  38.     }
  39.  
  40.     // Primer punto del angulo
  41.     _fx = _x = (std::round((std::cos(st_angle) * (float)_rx)) + cx);
  42.     _fy = _y = (std::round((std::sin(st_angle) * (float)_ry)) + cy);
  43.     // Dibuja los segmentos del arco
  44.     for (double angle = st_angle; angle <= ed_angle; angle += p) {
  45.         // Calculos de las coordenadas
  46.         x = (std::round((std::cos(angle) * (float)_rx)) + cx);
  47.         y = (std::round((std::sin(angle) * (float)_ry)) + cy);
  48.         if ((_x != x) || (_y != y)) Line(x, y, _x, _y, color);
  49.         _x = x;
  50.         _y = y;
  51.     }
  52.     // Ultimo punto del arco
  53.     x = (std::round((std::cos(ed_angle) * (float)_rx)) + cx);
  54.     y = (std::round((std::sin(ed_angle) * (float)_ry)) + cy);
  55.     if ((_x != x) || (_y != y)) Line(x, y, _x, _y, color);
  56.  
  57.     // Has de cerrar el arco (0 = no, 1 = entre los puntos, 2 = con el centro)
  58.     switch (close) {
  59.         // Entre ellos
  60.         case 1:
  61.             Line(x, y, _fx, _fy, color);
  62.             break;
  63.         // Con el centro
  64.         case 2:
  65.             Line(x, y, cx, cy, color);
  66.             Line(_fx, _fy, cx, cy, color);
  67.             break;
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement