From 708e501245fbca9f973a6e4d7ec1d172ca0470a3 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sat, 6 May 2023 17:39:18 +0200 Subject: [PATCH] u02: Implement DDA line tool --- u02/src/tests.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/u02/src/tests.cpp b/u02/src/tests.cpp index a2f7209..dd2eeee 100644 --- a/u02/src/tests.cpp +++ b/u02/src/tests.cpp @@ -5,6 +5,8 @@ #include #include +#include "canvas_buffer.h" +#include "dda_line_tool.h" #include "util.h" TEST_CASE("Transform Mirror") { @@ -102,3 +104,59 @@ TEST_CASE("Transformation to standard case (prop)") { REQUIRE(x0 <= x1); REQUIRE(y0 >= y1); } + +TEST_CASE("DDA line tool (prop: for every row/column, only one pixel is set)") { + const int size = 100; + canvas_buffer *canvas = new canvas_buffer(size, size); + dda_line_tool *tool = new dda_line_tool(*canvas); + + int x0 = GENERATE(take(10, random(0, size - 1))); + int y0 = GENERATE(take(10, random(0, size - 1))); + int x1 = GENERATE(take(10, random(0, size - 1))); + int y1 = GENERATE(take(10, random(0, size - 1))); + + tool->draw(x0, y0, x1, y1); + + int x_min = std::min(x0, x1); + int x_max = std::max(x0, x1); + int y_min = std::min(y0, y1); + int y_max = std::max(y0, y1); + + // Depending on what the direction of the line (rounded to the next 90°) is, + // either every row or column has only one pixel set. + bool vertical = false; + int draw_direction_min; + int draw_direction_max; + int unique_direction_min; + int unique_direction_max; + if (abs(y1 - y0) > abs(x1 - x0)) { + vertical = true; + draw_direction_min = y_min; + draw_direction_max = y_max; + unique_direction_min = x_min; + unique_direction_max = x_max; + } else { + draw_direction_min = x_min; + draw_direction_max = x_max; + unique_direction_min = y_min; + unique_direction_max = y_max; + } + + int sum; + for (int dd = draw_direction_min; dd <= draw_direction_max; dd++) { + sum = 0; + for (int ud = unique_direction_min; ud <= unique_direction_max; ud++) { + int x, y; + if (vertical) { + x = ud; + y = dd; + } else { + x = dd; + y = ud; + } + if (canvas->get_pixel(x, y)) + sum++; + } + REQUIRE(sum == 1); + } +}