aboutsummaryrefslogtreecommitdiffstats
path: root/src/renderer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/renderer.c')
-rw-r--r--src/renderer.c42
1 files changed, 14 insertions, 28 deletions
diff --git a/src/renderer.c b/src/renderer.c
index 0ce3107..07d79d6 100644
--- a/src/renderer.c
+++ b/src/renderer.c
@@ -128,35 +128,21 @@ draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
128 } 128 }
129 } else { 129 } else {
130 // Diagonal line. 130 // Diagonal line.
131 int x_step = x0 > x1 ? -1 : 1; 131 int dx = x0 > x1 ? x0 - x1 : x1 - x0;
132 int y_step = y0 > y1 ? -1 : 1; 132 int dy = y0 > y1 ? y1 - y0 : y0 - y1;
133 size_t dx = x0 > x1 ? x0 - x1 : x1 - x0; 133 int x_step = x0 < x1 ? 1 : -1;
134 size_t dy = y0 > y1 ? y0 - y1 : y1 - y0; 134 int y_step = y0 < y1 ? 1 : -1;
135 size_t x = x0; 135 int err = dx + dy;
136 size_t y = y0; 136 while (!(x0 == x1 && y0 == y1)) {
137 if (dx >= dy) { 137 draw_pixel(x0, y0, clr);
138 // Positive slope. 138 int diff = 2 * err;
139 int diff = 2 * dy - dx; 139 if (diff >= dy) {
140 for (size_t i = 0; i <= dx; ++i) { 140 err += dy;
141 draw_pixel(x, y, clr); 141 x0 += x_step;
142 if (diff >= 0) {
143 y += y_step;
144 diff -= 2 * dx;
145 }
146 x += x_step;
147 diff += 2 * dy;
148 } 142 }
149 } else { 143 if (diff <= dx) {
150 // Negative slope. 144 err += dx;
151 int diff = 2 * dx - dy; 145 y0 += y_step;
152 for (size_t i = 0; i <= dy; ++i) {
153 draw_pixel(x, y, clr);
154 if (diff >= 0) {
155 x += x_step;
156 diff -= 2 * dy;
157 }
158 y -= y_step;
159 diff += 2 * dx;
160 } 146 }
161 } 147 }
162 } 148 }