Browse Source

Fix the line bug offset

rexim 2 years ago
parent
commit
e71315f4c7
3 changed files with 19 additions and 18 deletions
  1. 2 18
      olive.c
  2. 17 0
      test.c
  3. BIN
      test/line_bug_offset_expected.png

+ 2 - 18
olive.c

@@ -566,18 +566,10 @@ OLIVECDEF void olivec_line(Olivec_Canvas oc, int x1, int y1, int x2, int y2, uin
             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 <= y && y < (int) oc.height) {
+            if (0 <= x && x < (int) oc.width && 0 <= y && y < (int) oc.height) {
                 olivec_blend_color(&OLIVEC_PIXEL(oc, x, y), color);
             }
         }
@@ -587,18 +579,10 @@ OLIVECDEF void olivec_line(Olivec_Canvas oc, int x1, int y1, int x2, int y2, uin
             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;
             // TODO: move boundary checks out side of the loops in olivec_draw_line
-            if (0 <= x && x < (int) oc.width) {
+            if (0 <= x && x < (int) oc.width && 0 <= y && y < (int) oc.height) {
                 olivec_blend_color(&OLIVEC_PIXEL(oc, x, y), color);
             }
         }

+ 17 - 0
test.c

@@ -612,6 +612,22 @@ Olivec_Canvas test_fill_ellipse(void)
     return dst;
 }
 
+Olivec_Canvas test_line_bug_offset(void)
+{
+    size_t factor = 3;
+    size_t width = 100*factor;
+    size_t height = 50*factor;
+    Olivec_Canvas dst = canvas_alloc(width, height);
+    olivec_fill(dst, BACKGROUND_COLOR);
+    int x1 = 50;
+    int y1 = 100;
+    int x2 = 0;
+    int y2 = -100;
+    olivec_line(dst, x1, y1, x2, y2, GREEN_COLOR);
+    olivec_circle(dst, x1, y1, 5, RED_COLOR);
+    return dst;
+}
+
 Test_Case test_cases[] = {
     DEFINE_TEST_CASE(fill_rect),
     DEFINE_TEST_CASE(fill_circle),
@@ -637,6 +653,7 @@ Test_Case test_cases[] = {
     DEFINE_TEST_CASE(barycentric_overflow),
     DEFINE_TEST_CASE(bilinear_interpolation),
     DEFINE_TEST_CASE(fill_ellipse),
+    DEFINE_TEST_CASE(line_bug_offset),
 };
 #define TEST_CASES_COUNT (sizeof(test_cases)/sizeof(test_cases[0]))
 

BIN
test/line_bug_offset_expected.png