diff --git a/exercise2/src/bresenham_line_tool.cpp b/exercise2/src/bresenham_line_tool.cpp index f6ff98c..38c18ed 100644 --- a/exercise2/src/bresenham_line_tool.cpp +++ b/exercise2/src/bresenham_line_tool.cpp @@ -1,21 +1,25 @@ // -// 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 "bresenham_line_tool.h" #include #include +#include + // Initialize the tool and store a reference of a canvas_buffer -bresenham_line_tool::bresenham_line_tool(canvas_buffer& canvas): tool_base(canvas) +bresenham_line_tool::bresenham_line_tool(canvas_buffer &canvas) : tool_base(canvas) { // This tool draws lines! 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) @@ -23,7 +27,7 @@ void bresenham_line_tool::draw(int x0, int y0, int x1, int y1) /************ Task 3.1.2. Implement the Bresenham algorithm to raster a line from (x0, y0) to (x1, y1). To set a pixel use "canvas.set_pixel(x, y)" where - "x" and "y" is the desired pixel position. This method + "x" and "y" is the desired pixel position. This method handles border violations. Establish the standard case in the first step. If you need to swap the value of two variables you can use the method "std::swap(a, b)". @@ -34,15 +38,94 @@ void bresenham_line_tool::draw(int x0, int y0, int x1, int y1) Diese Methode behandelt auch Randverletzungen. Stellen Sie zunaechst den Standardfall her. Falls Sie den Wert zweier Variablen vertauschen 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) + { + swap_y = true; + std::swap(y0, y1); + } + + int x = x0; + int y = y0; + + float dx = static_cast(abs(x1 - x0)); + float dy = static_cast(abs(y1 - y0)); + + int sR; + float f; + + if (dx > dy) + { + sR = 0; + f = dx / 2.0f; + } + else + { + sR = 1; + f = dy / 2.0f; + } + + int p = (2 * dy) - dx; + + while (x < x1 || y < y1) + { + if (sR == 0) + { + x++; + f -= dy; + } + else + { + y++; + f -= dx; + } + + if (f < 0.0f) + { + if (sR == 0) + { + y++; + f += dx; + } + else + { + x++; + f += dy; + } + } + + int x_res = x; + + if (swap_y) + { + x_res = x1 - (x - x0); + } + + canvas.set_pixel(x_res, y); + } } - - - // Put debug output into the stream "stream" to be displayed in the // main window -void bresenham_line_tool::set_text(std::stringstream& stream) +void bresenham_line_tool::set_text(std::stringstream &stream) { - stream<<"Tool: Bresenham-Line (click and drag mouse to draw)"; + stream << "Tool: Bresenham-Line (click and drag mouse to draw)"; }