-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.cpp
More file actions
48 lines (43 loc) · 1.71 KB
/
graphics.cpp
File metadata and controls
48 lines (43 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "graphics.h"
#include <cstdlib>
void drawLine(Device& device, Pixel& pixel, const unsigned int& startX, const unsigned int& startY, const unsigned int& endX, const unsigned int& endY) {
unsigned int dx = endX - startX;
unsigned int dy = endY - startY;
unsigned int x = startX, y = startY;
unsigned int steps;
float xIncrement, yIncrement;
if (abs(dx) > abs(dy)) {
steps = abs(dx);
} else {
steps = abs(dy);
}
xIncrement = dx / (float) steps;
yIncrement = dy / (float) steps;
device.setBufferPixel(x, y, pixel);
for (int v = 0; v < steps; v++) {
x += xIncrement;
y += yIncrement;
device.setBufferPixel(x, y, pixel);
}
}
void drawVerticalLine(Device& device, Pixel& pixel, const unsigned int& x, const unsigned int& startY, const unsigned int& endY) {
for (unsigned int y = startY; y <= endY; y++) {
device.setBufferPixel(x, y, pixel);
}
}
void drawHorizontalLine(Device& device, Pixel& pixel, const unsigned int& y, const unsigned int& startX, const unsigned int& endX) {
for (unsigned int x = startX; x <= endX; x++) {
device.setBufferPixel(x, y, pixel);
}
}
void drawRectangle(Device& device, Pixel& pixel, const unsigned int& startX, const unsigned int& startY, const unsigned int& endX, const unsigned int& endY) {
drawHorizontalLine(device, pixel, startY, startX, endX);
drawHorizontalLine(device, pixel, endY, startX, endX);
drawVerticalLine(device, pixel, startX, startY, endY);
drawVerticalLine(device, pixel, endX, startY, endY);
}
void fillRectangle(Device& device, Pixel& pixel, const unsigned int& startX, const unsigned int& startY, const unsigned int& endX, const unsigned int& endY) {
for (unsigned int y = startY; y <= endY; y++) {
drawHorizontalLine(device, pixel, y, startX, endX);
}
}