diff --git a/.vscode/settings.json b/.vscode/settings.json index 2d27a79..1cb26e2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,6 +35,8 @@ "typeinfo": "cpp", "utility": "cpp", "__config": "cpp", - "__nullptr": "cpp" + "__nullptr": "cpp", + "deque": "cpp", + "algorithm": "cpp" } } \ No newline at end of file diff --git a/exercise2/src/bresenham_line_tool.cpp b/exercise2/src/bresenham_line_tool.cpp index 38c18ed..daeac46 100644 --- a/exercise2/src/bresenham_line_tool.cpp +++ b/exercise2/src/bresenham_line_tool.cpp @@ -16,11 +16,6 @@ bresenham_line_tool::bresenham_line_tool(canvas_buffer &canvas) : tool_base(canv shape = TS_LINE; } -void print_point(int x, int y) -{ - std::cout << "point (" << x << ", " << y << ")" << std::endl; -} - // Draw a line from (x0, y0) to (x1, y1) void bresenham_line_tool::draw(int x0, int y0, int x1, int y1) { @@ -40,21 +35,12 @@ void bresenham_line_tool::draw(int x0, int y0, int x1, int y1) muessen koennen Sie dafür die Methode "std::swap(a, b)" verwenden. *************/ - std::cout << "first: " << std::endl; - print_point(x0, y0); - print_point(x1, y1); - if (x0 > x1) { std::swap(x0, x1); std::swap(y0, y1); } - std::cout << "second: " << std::endl; - print_point(x0, y0); - print_point(x1, y1); - std::cout << std::endl; - bool swap_y = false; if (y0 > y1) diff --git a/exercise2/src/non_recursive_fill_tool.cpp b/exercise2/src/non_recursive_fill_tool.cpp index 0f5b4c6..16aa359 100644 --- a/exercise2/src/non_recursive_fill_tool.cpp +++ b/exercise2/src/non_recursive_fill_tool.cpp @@ -1,44 +1,52 @@ // -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute in modified or unmodified form! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute in modified or unmodified form! // Copyright (C) 2016 CGV TU Dresden - All Rights Reserved // #include "non_recursive_fill_tool.h" #include +#include + // Initialize the tool and store a reference of a canvas_buffer -non_recursive_fill_tool::non_recursive_fill_tool(canvas_buffer& canvas): tool_base(canvas) +non_recursive_fill_tool::non_recursive_fill_tool(canvas_buffer &canvas) : tool_base(canvas) { // This tool has no shape and is not draggable shape = TS_NONE; is_draggable = false; } - // A pixel to be put into the queue of pixels to process -struct waiting_pixel { - int x, y; +struct waiting_pixel +{ + waiting_pixel(int x, int y) : x(x), y(y) {} + waiting_pixel() : x(0), y(0) {} + + int x, y; }; - +void print_point(int x, int y) +{ + std::cout << "point (" << x << ", " << y << ")" << std::endl; +} void non_recursive_fill_tool::draw(int x, int y) { waiting_pixel p; - std::deque stack; + std::deque stack; /************ Task 3.2.2. Implement a filling algothim without recursion. Use a stack instead. - The source code below inserts the first element into the stack and + The source code below inserts the first element into the stack and starts a loop that reads the front element from the stack and removes it. Your task is to fill the functionality in the loop. Before you begin - think about when to set the pixel (at the beginning of the loop or after + think about when to set the pixel (at the beginning of the loop or after the stack was updated with neighbours). Use "canvas.set_pixel" to set a pixel and "canvas.get_pixel" to read the state of a pixel. - Aufgabe 3.2.2. Implementieren Sie einen Füllalgorithmus ohne Rekursion zu verwenden. + Aufgabe 3.2.2. Implementieren Sie einen F�llalgorithmus ohne Rekursion zu verwenden. Nutzen Sie stattdessen einen Stack. Der vorgegebene Quelltext unten fuegt - das erste Element in den Stack ein und startet eine Schleife, die das - vorderste Element aus dem Stack liest und es anschließend entfernt. + das erste Element in den Stack ein und startet eine Schleife, die das + vorderste Element aus dem Stack liest und es anschlie�end entfernt. Ihre Aufgabe ist es die Funktionalitaet im Inneren der Schleife zu ergaenzen. Bevor Sie beginnen machen Sie sich Gedanken an welcher Stelle das tatsaechliche Setzen eines Pixels erfolgt (am Beginn der Schleife oder @@ -47,34 +55,54 @@ void non_recursive_fill_tool::draw(int x, int y) den Zustand eines Pixels zu erhalten. *************/ - - // Put the initial pixel into the stack - if (!canvas.get_pixel(x, y)) { - p.x = x; - p.y = y; - canvas.set_pixel(x, y); + if (!canvas.get_pixel(x, y)) + { + p.x = x; + p.y = y; stack.push_back(p); } - - // While there are pixels to be checked - while (!stack.empty()) { - - int cur_x = stack.front().x; - int cur_y = stack.front().y; + while (!stack.empty()) + { + int x = stack.front().x; + int y = stack.front().y; // Complete the algorithm here - stack.pop_front(); + if (!canvas.get_pixel(x, y)) + { + canvas.set_pixel(x, y); + + // add neighbours with bounds check + if (x > 0) + { + stack.emplace_back(x - 1, y); + } + + if (y > 0) + { + stack.emplace_back(x, y - 1); + } + + if (y < canvas.get_height() - 1) + { + stack.emplace_back(x, y + 1); + } + + if (x < canvas.get_width() - 1) + { + stack.emplace_back(x + 1, y); + } + } + + stack.pop_front(); } } - - // Put debug output into the stream "stream" to be displayed in the // main window -void non_recursive_fill_tool::set_text(std::stringstream& stream) +void non_recursive_fill_tool::set_text(std::stringstream &stream) { - stream<<"Tool: Non-Recursive Fill (click to fill)"; + stream << "Tool: Non-Recursive Fill (click to fill)"; } \ No newline at end of file diff --git a/exercise2/src/recursive_fill_tool.cpp b/exercise2/src/recursive_fill_tool.cpp index f6ca50d..6bac8d9 100644 --- a/exercise2/src/recursive_fill_tool.cpp +++ b/exercise2/src/recursive_fill_tool.cpp @@ -1,20 +1,18 @@ // -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute in modified or unmodified form! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute in modified or unmodified form! // Copyright (C) 2016 CGV TU Dresden - All Rights Reserved // #include "recursive_fill_tool.h" // Initialize the tool and store a reference of a canvas_buffer -recursive_fill_tool::recursive_fill_tool(canvas_buffer& canvas): tool_base(canvas) +recursive_fill_tool::recursive_fill_tool(canvas_buffer &canvas) : tool_base(canvas) { // This tool has no shape and is not draggable shape = TS_NONE; is_draggable = false; } - - // Fill the shape that contains the point (x, y) void recursive_fill_tool::draw(int x, int y) { @@ -24,22 +22,48 @@ void recursive_fill_tool::draw(int x, int y) where "x" and "y" defines the position to check) and recursively call "draw" if the pixel is not set. A pixel can be set with "canvas.set_pixel(x, y)". - This method will make the application crash if the area which shall + This method will make the application crash if the area which shall be filled is too big. - Aufgabe 3.2.1. Implementieren Sie den rekursiven Füllalgorithmus, indem Sie den - Zustand der Nachbarpixel überprüfen (mittels "canvas.get_pixel(x, y)" - wobei "x" und "y" die Position des zu überprüfenden Pixels definieren) - und rufen Sie rekursiv die "draw"-Methode auf, wenn die Pixel nicht + Aufgabe 3.2.1. Implementieren Sie den rekursiven F�llalgorithmus, indem Sie den + Zustand der Nachbarpixel �berpr�fen (mittels "canvas.get_pixel(x, y)" + wobei "x" und "y" die Position des zu �berpr�fenden Pixels definieren) + und rufen Sie rekursiv die "draw"-Methode auf, wenn die Pixel nicht gesetzt sind. Ein Pixel kann durch "canvas.set_pixel(x, y)" gesetzt werden. *************/ + + if (canvas.get_pixel(x, y)) + { + return; + } + + canvas.set_pixel(x, y); + + // call neighbours with bounds check + if (x > 0) + { + draw(x - 1, y); + } + + if (y > 0) + { + draw(x, y - 1); + } + + if (y < canvas.get_height() - 1) + { + draw(x, y + 1); + } + + if (x < canvas.get_width() - 1) + { + draw(x + 1, y); + } } - - // Put debug output into the stream "stream" to be displayed in the // main window -void recursive_fill_tool::set_text(std::stringstream& stream) +void recursive_fill_tool::set_text(std::stringstream &stream) { - stream<<"Tool: Recursive Fill (click to fill)"; + stream << "Tool: Recursive Fill (click to fill)"; } \ No newline at end of file