From e2e64daf1070d61b6e8d6618bb0a9d723a26d654 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Mon, 20 May 2019 12:49:29 +0200 Subject: [PATCH] Add bresenham circle and rectangle --- exercise2/include/bresenham_circle_tool.h | 13 +++--- exercise2/src/bresenham_circle_tool.cpp | 54 +++++++++++++++++++---- exercise2/src/rectangle_tool.cpp | 33 +++++++++----- 3 files changed, 76 insertions(+), 24 deletions(-) diff --git a/exercise2/include/bresenham_circle_tool.h b/exercise2/include/bresenham_circle_tool.h index 321e193..8035639 100644 --- a/exercise2/include/bresenham_circle_tool.h +++ b/exercise2/include/bresenham_circle_tool.h @@ -1,12 +1,12 @@ // -// 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 // /************************************************************************* Bresenham-Circle tool. This class is inherited from "tool_base" to define a tool for drawing -circles using the bresenham algorithm on a canvas_buffer. +circles using the bresenham algorithm on a canvas_buffer. Read the information in "tool_base.h" for more information. *************************************************************************/ #pragma once @@ -14,7 +14,7 @@ Read the information in "tool_base.h" for more information. // provides the declaration of the super class tool_base #include "tool_base.h" -class bresenham_circle_tool: public tool_base +class bresenham_circle_tool : public tool_base { public: // Initialize the tool and store a reference of a canvas_buffer @@ -25,5 +25,8 @@ public: // Put debug output into the stream "stream" to be displayed in the // main window - void set_text(std::stringstream& stream); + void set_text(std::stringstream &stream); + +private: + void draw_circle(int xc, int yc, int x, int y); }; \ No newline at end of file diff --git a/exercise2/src/bresenham_circle_tool.cpp b/exercise2/src/bresenham_circle_tool.cpp index f4265ea..7a8aaf6 100644 --- a/exercise2/src/bresenham_circle_tool.cpp +++ b/exercise2/src/bresenham_circle_tool.cpp @@ -1,6 +1,6 @@ // -// 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_circle_tool.h" @@ -8,19 +8,29 @@ #include // Initialize the tool and store a reference of a canvas_buffer -bresenham_circle_tool::bresenham_circle_tool(canvas_buffer& canvas): tool_base(canvas) +bresenham_circle_tool::bresenham_circle_tool(canvas_buffer &canvas) : tool_base(canvas) { // This tool draws circles! shape = TS_CIRCLE; } - +void bresenham_circle_tool::draw_circle(int xc, int yc, int x, int y) +{ + canvas.set_pixel(xc + x, yc + y); + canvas.set_pixel(xc - x, yc + y); + canvas.set_pixel(xc + x, yc - y); + canvas.set_pixel(xc - x, yc - y); + canvas.set_pixel(xc + y, yc + x); + canvas.set_pixel(xc - y, yc + x); + canvas.set_pixel(xc + y, yc - x); + canvas.set_pixel(xc - y, yc - x); +} // Draw a circle with center (x0, y0) and (x1, y1) on the circle void bresenham_circle_tool::draw(int x0, int y0, int x1, int y1) { // Calculate the radius - int r = static_cast(sqrt(static_cast((x0-x1)*(x0-x1) + (y0-y1)*(y0-y1)))); + int r = static_cast(sqrt(static_cast((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1)))); /************ Additional task: Implement circle rasterization using the bresenham algorithm. @@ -31,13 +41,39 @@ void bresenham_circle_tool::draw(int x0, int y0, int x1, int y1) (x0, y0) und einen Radius von "r" haben. *************/ + int x = 0; + int y = r; + int d = 3 - 2 * r; + + draw_circle(x0, y0, x, y); + + while (y >= x) + { + // for each pixel we will + // draw all eight pixels + + x++; + + // check for decision parameter + // and correspondingly + // update d, x, y + if (d > 0) + { + y--; + d = d + 4 * (x - y) + 10; + } + else + { + d = d + 4 * x + 6; + } + + draw_circle(x0, y0, x, y); + } } - - // Put debug output into the stream "stream" to be displayed in the // main window -void bresenham_circle_tool::set_text(std::stringstream& stream) +void bresenham_circle_tool::set_text(std::stringstream &stream) { - stream<<"Tool: Bresenham-Circle (click and drag mouse to draw)"; + stream << "Tool: Bresenham-Circle (click and drag mouse to draw)"; } \ No newline at end of file diff --git a/exercise2/src/rectangle_tool.cpp b/exercise2/src/rectangle_tool.cpp index 64ef68c..d01bf93 100644 --- a/exercise2/src/rectangle_tool.cpp +++ b/exercise2/src/rectangle_tool.cpp @@ -1,20 +1,17 @@ // -// 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 "rectangle_tool.h" #include - // Initialize the tool and store a reference of a canvas_buffer -rectangle_tool::rectangle_tool(canvas_buffer& canvas): tool_base(canvas) +rectangle_tool::rectangle_tool(canvas_buffer &canvas) : tool_base(canvas) { shape = TS_BOX; } - - // Draw a box from (x0, y0) to (x1, y1) void rectangle_tool::draw(int x0, int y0, int x1, int y1) { @@ -24,13 +21,29 @@ void rectangle_tool::draw(int x0, int y0, int x1, int y1) Zusatzaufgabe: Implementieren Sie die Rasterisierung eines Rechtecks, das von (x0, y0) nach (x1, y1) geht. *************/ + + if (x0 > x1) + { + std::swap(x0, x1); + } + + if (y0 > y1) + { + std::swap(y0, y1); + } + + for (int x = x0; x <= x1; x++) + { + for (int y = y0; y <= y1; y++) + { + canvas.set_pixel(x, y); + } + } } - - // Put debug output into the stream "stream" to be displayed in the // main window -void rectangle_tool::set_text(std::stringstream& stream) +void rectangle_tool::set_text(std::stringstream &stream) { - stream<<"Tool: Rectangle (click and drag mouse to draw)"; + stream << "Tool: Rectangle (click and drag mouse to draw)"; } \ No newline at end of file