Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- /******************************************
- * COLORAรรO DE CARACTERES */
- const std::string RED("\033[0;31m");
- const std::string GREEN("\033[1;32m");
- const std::string YELLOW("\033[1;33m");
- const std::string CYAN("\033[0;36m");
- const std::string MAGENTA("\033[0;35m");
- const std::string RESET("\033[0m");
- /******************************************/
- inline int abs(int n) { return n > 0 ? n : -n; }
- inline int round(float n) { return (int) (n + 0.5); }
- int size;
- bool** buffer;
- void drawLineDDA(int xa, int ya, int xb, int yb)
- {
- int dx = xb - xa,
- dy = yb - ya;
- int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
- float xIncrement = dx / (float) steps,
- yIncrement = dy / (float) steps,
- x = xa,
- y = ya;
- buffer[round(y)][round(x)] = true;;
- for (int k = 0; k < steps; k++)
- {
- x += xIncrement;
- y += yIncrement;
- buffer[round(y)][round(x)] = true;
- }
- }
- void drawLineBres_(int xa, int ya, int xb, int yb, bool inverted)
- {
- int dx = xb - xa,
- dy = yb - ya,
- slope;
- if (xa > xb)
- return drawLineBres_(xb, yb, xa, ya, inverted);
- if (abs(dy) > abs(dx))
- return drawLineBres_(ya, xa, yb, xb, ! inverted);
- if (dy < 0)
- {
- slope = -1;
- dy = -dy;
- }
- else slope = 1;
- int incE = 2 * dy,
- incNE = 2 * (dy - dx),
- p = 2 * dy - dx;
- for (; xa <= xb; xa++)
- {
- if (inverted)
- buffer[xa][ya] = true;
- else
- buffer[ya][xa] = true;
- if (p < 0)
- p += incE;
- else {
- p += incNE;
- ya += slope;
- }
- }
- }
- void drawLineBres(int xa, int ya, int xb, int yb) {
- drawLineBres_(xa, ya, xb, yb, false);
- }
- void clear()
- {
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- buffer[i][j] = false;
- }
- void display()
- {
- for (int i = size - 1; i >= 0; i--) {
- cout << RED << (i < 10 ? " " : "") << i << RESET;
- for (int j = 0; j < size; j++)
- if (buffer[i][j])
- cout << " o";
- else
- cout << MAGENTA << " ." << RESET;
- cout << endl;
- }
- cout << " " << RED;
- for (int i = 0; i < size; i++)
- cout << (i < 10 ? " " : "") << i;
- cout << RESET << endl << endl;
- }
- /* Exemplo de uso:
- * Tamanho: 30 # Cria um buffer de tamanho 30x30
- * Pontos: 4 19 9 13 # Mostra grรกfico da reta que vai do ponto (4, 19) ao (9, 13)
- * # Pontos no formato (x, y) */
- int main()
- {
- cout << "Tamanho (largura/altura): ";
- cin >> size;
- buffer = new bool*[size];
- for (int i = 0; i < size; i++)
- buffer[i] = new bool[size];
- int xa, ya, xb, yb;
- cout << "Pontos (xa xb ya yb): ";
- cin >> xa >> ya >> xb >> yb;
- cout << endl;
- cout << "DDA:" << endl;
- clear();
- drawLineDDA(xa, ya, xb, yb);
- display();
- cout << "Bresenham:" << endl;
- clear();
- drawLineBres(xa, ya, xb, yb);
- display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement