Advertisement
NightFox

Dibuja una linea entre dos puntos (Implementacion del algoritmo de Bresenham)

Nov 1st, 2020
1,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. /*** Dibuja una linea entre dos puntos (Implementacion del algoritmo de Bresenham) ***/
  2. void NGN_ProDraw::Line(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color) {
  3.  
  4.     // Parametros iniciales
  5.     int32_t right = (surface_width - 1);        // Limites del buffer
  6.     int32_t bottom = (surface_height - 1);
  7.     int32_t x = x1;                             // Coordenadas de dibujado
  8.     int32_t y = y1;
  9.     int32_t dx = (x2 - x1);                     // Distancias
  10.     int32_t dy = (y2 - y1);
  11.     int32_t ix = (dx >= 0) ? 1:-1;              // Sentido del dibujado
  12.     int32_t iy = (dy >= 0) ? 1:-1;
  13.     int32_t pk = 0;                             // Precision de la pendiente
  14.  
  15.     // Valor absoluto de las distancias
  16.     dx = std::abs(dx);
  17.     dy = std::abs(dy);
  18.  
  19.     // Bloquea el surface
  20.     SDL_LockSurface(surface);
  21.  
  22.     // Acceso al array de pixeles
  23.     uint32_t* p = (uint32_t*)surface->pixels;
  24.  
  25.    
  26.     // Segun la pendiente (mandan la X o mandan las Y)
  27.     if (dx >= dy) {
  28.  
  29.         // Dibuja el primer pixel
  30.         if (!((x < 0) || (y < 0) || (x > right) || (y > bottom))) {
  31.             p[((y * surface_width) + x)] = color;
  32.         }
  33.         // Calculo de la precision
  34.         pk = ((dy << 1) - dx);
  35.         // Bucle de dibujado
  36.         for (int32_t i = 0; i < dx; i ++) {
  37.             // Incremento de la X
  38.             x += ix;
  39.             // Segun el estado de la precision, recalculala
  40.             if (pk < 0) {
  41.                 pk += (dy << 1);
  42.             } else {
  43.                 y += iy;
  44.                 pk += (dy << 1) - (dx << 1);
  45.             }
  46.             // Dibuja el pixel
  47.             if (!((x < 0) || (y < 0) || (x > right) || (y > bottom))) {
  48.                 p[((y * surface_width) + x)] = color;
  49.             }
  50.         }
  51.  
  52.     } else {
  53.  
  54.         // Dibuja el primer pixel
  55.         if (!((x < 0) || (y < 0) || (x > right) || (y > bottom))) {
  56.             p[((y * surface_width) + x)] = color;
  57.         }
  58.         // Calculo de la precision
  59.         pk = ((dx << 1) - dy);
  60.         // Bucle de dibujado
  61.         for (int32_t i = 0; i < dy; i ++) {
  62.             // Incremento de la Y
  63.             y += iy;
  64.             // Segun el estado de la precision, recalculala
  65.             if (pk < 0) {
  66.                 pk += (dx << 1);
  67.             } else {
  68.                 x += ix;
  69.                 pk += (dx << 1) - (dy << 1);
  70.             }
  71.             // Dibuja el pixel
  72.             if (!((x < 0) || (y < 0) || (x > right) || (y > bottom))) {
  73.                 p[((y * surface_width) + x)] = color;
  74.             }
  75.         }
  76.  
  77.     }
  78.  
  79.     // Desbloquea el surface
  80.     SDL_UnlockSurface(surface);
  81.  
  82.     // Indica el blit
  83.     blit = true;
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement