Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void DrawNaiveLine(const Point &p1, const Point &p2, unsigned char r, unsigned char g, unsigned char b)
- {
- //test if the line is vertical
- if (Round(p1.x) == Round(p2.x)) DrawVerticalLine(p1, p2, r, g, b);
- //test if the line is horizontal
- else if (Round(p1.y) == Round(p2.y)) DrawHorizontalLine(p1, p2, r, g, b);
- //test if the line is diagonal
- else if (abs(Round(p1.x) - Round(p2.x)) == abs(Round(p1.y) - Round(p2.y)))
- DrawDiagonalLine(p1, p2, r, g, b);
- //otherwise the line is not simple
- else
- {
- if (DEBUG) printf("Drawing non-trivial line from p1(%3.3f, %3.3f) to p2(%3.3f, %3.3f)\n", p1.x, p1.y, p2.x, p2.y);
- //calculate slope
- //(no danger of divison by zero due to vertical line test)
- float m = (p2.y - p1.y) / (p2.x - p1.x);
- //calculate intercept
- float b_ = p1.y - (m*p1.x);
- //step through the y
- if (abs(m) > 1)
- {
- //starting with p1
- if (p1.y < p2.y)
- {
- if (DEBUG) printf("stepping along the y axis starting with p1\n");
- //calculate the distance
- float distance = p2.y - p1.y;
- //draw the line
- for (int i = 0; i < distance + 1; ++i)
- {
- //calculate the x value
- float x = m*(p1.y + i) + b_;
- //draw the pixel
- if (DEBUG) printf("drawing pixel at (%d, %d)\n",
- Round(x), Round(p1.y + i));
- FrameBuffer::SetPixel(Round(x), Round(p1.y + i), r, g, b);
- }
- }
- //starting with p2
- else
- {
- if (DEBUG) printf("stepping along the y axis starting with p2\n");
- //calculate the distance
- float distance = p1.y - p2.y;
- //draw the line
- for (int i = 0; i < distance + 1; ++i)
- {
- //calculate the x value
- float x = m*(p2.y + i) + b_;
- //draw the pixel
- if (DEBUG) printf("drawing pxel at (%d, %d)\n",
- Round(x), Round(p2.y + i));
- FrameBuffer::SetPixel(Round(x), Round(p2.y + i), r, g, b);
- }
- }
- }
- //step through the x
- else if (abs(m) < 1)
- {
- //starting with p1
- if (p1.x < p2.x)
- {
- if (DEBUG) printf("stepping along the x axis starting with p1\n");
- //calculate the distance
- float distance = p2.x - p1.x;
- //draw the line
- for (int i = 0; i < distance + 1; ++i)
- {
- //calculate the y value
- float y = m*(p1.x + i) + b_;
- //draw the pixel
- if (DEBUG) printf("drawing pixel at (%d, %d)\n",
- Round(p1.x+i), Round(y));
- FrameBuffer::SetPixel(Round(p1.x + i), Round(y), r, g, b);
- }
- }
- //starting with p2
- else
- {
- if (DEBUG) printf("stepping along the x axis starting with p2\n");
- //calculate the distance
- float distance = p1.x - p2.x;
- //draw the line
- for (int i = 0; i < distance + 1; ++i)
- {
- //calculate the x value
- float y = m*(p2.x + i) + b_;
- //draw the pixel
- if (DEBUG) printf("drawing pxel at (%d, %d)\n",
- Round(p2.x+i), Round(y));
- FrameBuffer::SetPixel(Round(p2.x + i), Round(y), r, g, b);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement