|
@@ -339,8 +339,11 @@ OLIVECDEF void olivec_line(Olivec_Canvas oc, int x1, int y1, int x2, int y2, uin
|
|
|
int dx = x2 - x1;
|
|
|
int dy = y2 - y1;
|
|
|
|
|
|
+ // If both of the differences are 0 there will be a division by 0 below.
|
|
|
if (dx == 0 && dy == 0) {
|
|
|
- olivec_blend_color(&OLIVEC_PIXEL(oc, x1, y1), color);
|
|
|
+ if (0 <= x1 && x1 < (int) oc.width && 0 <= y1 && y1 < (int) oc.height) {
|
|
|
+ olivec_blend_color(&OLIVEC_PIXEL(oc, x1, y1), color);
|
|
|
+ }
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -349,10 +352,19 @@ OLIVECDEF void olivec_line(Olivec_Canvas oc, int x1, int y1, int x2, int y2, uin
|
|
|
OLIVEC_SWAP(int, x1, x2);
|
|
|
OLIVEC_SWAP(int, y1, y2);
|
|
|
}
|
|
|
+
|
|
|
+ // Cull out invisible line
|
|
|
+ if (x1 > (int) oc.width) return;
|
|
|
+ if (x2 < 0) return;
|
|
|
+
|
|
|
+ // Clamp the line to the boundaries
|
|
|
+ if (x1 < 0) x1 = 0;
|
|
|
+ if (x2 >= (int) oc.width) x2 = (int) oc.width - 1;
|
|
|
+
|
|
|
for (int x = x1; x <= x2; ++x) {
|
|
|
int y = dy*(x - x1)/dx + y1;
|
|
|
// TODO: move boundary checks out side of the loops in olivec_draw_line
|
|
|
- if (0 <= x && x < (int) oc.width && 0 <= y && y < (int) oc.height) {
|
|
|
+ if (0 <= y && y < (int) oc.height) {
|
|
|
olivec_blend_color(&OLIVEC_PIXEL(oc, x, y), color);
|
|
|
}
|
|
|
}
|
|
@@ -361,9 +373,19 @@ OLIVECDEF void olivec_line(Olivec_Canvas oc, int x1, int y1, int x2, int y2, uin
|
|
|
OLIVEC_SWAP(int, x1, x2);
|
|
|
OLIVEC_SWAP(int, y1, y2);
|
|
|
}
|
|
|
+
|
|
|
+ // Cull out invisible line
|
|
|
+ if (y1 > (int) oc.height) return;
|
|
|
+ if (y2 < 0) return;
|
|
|
+
|
|
|
+ // Clamp the line to the boundaries
|
|
|
+ if (y1 < 0) y1 = 0;
|
|
|
+ if (y2 >= (int) oc.height) y2 = (int) oc.height - 1;
|
|
|
+
|
|
|
for (int y = y1; y <= y2; ++y) {
|
|
|
int x = dx*(y - y1)/dy + x1;
|
|
|
- if (0 <= x && x < (int) oc.width && 0 <= y && y < (int) oc.height) {
|
|
|
+ // TODO: move boundary checks out side of the loops in olivec_draw_line
|
|
|
+ if (0 <= x && x < (int) oc.width) {
|
|
|
olivec_blend_color(&OLIVEC_PIXEL(oc, x, y), color);
|
|
|
}
|
|
|
}
|