u02: Implement DDA line tool
This commit is contained in:
parent
fab356c308
commit
708e501245
|
@ -5,6 +5,8 @@
|
|||
#include <catch2/generators/catch_generators_adapters.hpp>
|
||||
#include <catch2/generators/catch_generators_random.hpp>
|
||||
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue