Advertisement
mateuspl

CG: DDA e Bresenham

Mar 6th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. /******************************************
  6.  *        COLORAร‡ร‚O DE CARACTERES       */
  7. const std::string RED("\033[0;31m");
  8. const std::string GREEN("\033[1;32m");
  9. const std::string YELLOW("\033[1;33m");
  10. const std::string CYAN("\033[0;36m");
  11. const std::string MAGENTA("\033[0;35m");
  12. const std::string RESET("\033[0m");
  13. /******************************************/
  14.  
  15. inline int abs(int n) { return n > 0 ? n : -n; }
  16. inline int round(float n) { return (int) (n + 0.5); }
  17.  
  18. int size;
  19. bool** buffer;
  20.  
  21. void drawLineDDA(int xa, int ya, int xb, int yb)
  22. {
  23.     int dx = xb - xa,
  24.         dy = yb - ya;
  25.  
  26.     int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
  27.  
  28.     float   xIncrement = dx / (float) steps,
  29.             yIncrement = dy / (float) steps,
  30.             x = xa,
  31.             y = ya;
  32.  
  33.     buffer[round(y)][round(x)] = true;;
  34.  
  35.     for (int k = 0; k < steps; k++)
  36.     {
  37.         x += xIncrement;
  38.         y += yIncrement;
  39.  
  40.         buffer[round(y)][round(x)] = true;
  41.     }
  42. }
  43.  
  44. void drawLineBres_(int xa, int ya, int xb, int yb, bool inverted)
  45. {
  46.     int dx = xb - xa,
  47.         dy = yb - ya,
  48.         slope;
  49.    
  50.     if (xa > xb)
  51.         return drawLineBres_(xb, yb, xa, ya, inverted);
  52.     if (abs(dy) > abs(dx))
  53.         return drawLineBres_(ya, xa, yb, xb, ! inverted);
  54.  
  55.     if (dy < 0)
  56.     {
  57.         slope = -1;
  58.         dy = -dy;
  59.     }
  60.     else slope = 1;
  61.  
  62.     int incE = 2 * dy,
  63.         incNE = 2 * (dy - dx),
  64.         p = 2 * dy - dx;
  65.  
  66.     for (; xa <= xb; xa++)
  67.     {
  68.         if (inverted)
  69.             buffer[xa][ya] = true;
  70.         else
  71.             buffer[ya][xa] = true;
  72.  
  73.         if (p < 0)
  74.             p += incE;
  75.         else {
  76.             p += incNE;
  77.             ya += slope;
  78.         }
  79.     }
  80. }
  81.  
  82. void drawLineBres(int xa, int ya, int xb, int yb) {
  83.     drawLineBres_(xa, ya, xb, yb, false);
  84. }
  85.  
  86. void clear()
  87. {
  88.     for (int i = 0; i < size; i++)
  89.         for (int j = 0; j < size; j++)
  90.             buffer[i][j] = false;
  91. }
  92.  
  93. void display()
  94. {
  95.     for (int i = size - 1; i >= 0; i--) {
  96.         cout << RED << (i < 10 ? " " : "") << i << RESET;
  97.  
  98.         for (int j = 0; j < size; j++)
  99.             if (buffer[i][j])
  100.                 cout << " o";
  101.             else
  102.                 cout << MAGENTA << " ." << RESET;
  103.         cout << endl;
  104.     }
  105.  
  106.     cout << "  " << RED;
  107.     for (int i = 0; i < size; i++)
  108.         cout << (i < 10 ? " " : "") << i;
  109.     cout << RESET << endl << endl;
  110. }
  111.  
  112. /* Exemplo de uso:
  113.  * Tamanho: 30          # Cria um buffer de tamanho 30x30
  114.  * Pontos: 4 19 9 13    # Mostra grรกfico da reta que vai do ponto (4, 19) ao (9, 13)
  115.  *                      # Pontos no formato (x, y) */
  116. int main()
  117. {
  118.     cout << "Tamanho (largura/altura): ";
  119.     cin >> size;
  120.  
  121.     buffer = new bool*[size];
  122.     for (int i = 0; i < size; i++)
  123.         buffer[i] = new bool[size];
  124.  
  125.     int xa, ya, xb, yb;
  126.     cout << "Pontos (xa xb ya yb): ";
  127.     cin >> xa >> ya >> xb >> yb;
  128.     cout << endl;
  129.  
  130.     cout << "DDA:" << endl;
  131.     clear();
  132.     drawLineDDA(xa, ya, xb, yb);
  133.     display();
  134.  
  135.     cout << "Bresenham:" << endl;
  136.     clear();
  137.     drawLineBres(xa, ya, xb, yb);
  138.     display();
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement