Add bresenham circle and rectangle
This commit is contained in:
parent
0f847a08ab
commit
e2e64daf10
3 changed files with 76 additions and 24 deletions
|
@ -14,7 +14,7 @@ Read the information in "tool_base.h" for more information.
|
||||||
// provides the declaration of the super class tool_base
|
// provides the declaration of the super class tool_base
|
||||||
#include "tool_base.h"
|
#include "tool_base.h"
|
||||||
|
|
||||||
class bresenham_circle_tool: public tool_base
|
class bresenham_circle_tool : public tool_base
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// Initialize the tool and store a reference of a canvas_buffer
|
// 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
|
// Put debug output into the stream "stream" to be displayed in the
|
||||||
// main window
|
// 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);
|
||||||
};
|
};
|
|
@ -8,19 +8,29 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
// Initialize the tool and store a reference of a canvas_buffer
|
// 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!
|
// This tool draws circles!
|
||||||
shape = TS_CIRCLE;
|
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
|
// 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)
|
void bresenham_circle_tool::draw(int x0, int y0, int x1, int y1)
|
||||||
{
|
{
|
||||||
// Calculate the radius
|
// Calculate the radius
|
||||||
int r = static_cast<int>(sqrt(static_cast<double>((x0-x1)*(x0-x1) + (y0-y1)*(y0-y1))));
|
int r = static_cast<int>(sqrt(static_cast<double>((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1))));
|
||||||
|
|
||||||
/************
|
/************
|
||||||
Additional task: Implement circle rasterization using the bresenham algorithm.
|
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.
|
(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
|
// Put debug output into the stream "stream" to be displayed in the
|
||||||
// main window
|
// 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)";
|
||||||
}
|
}
|
|
@ -6,15 +6,12 @@
|
||||||
#include "rectangle_tool.h"
|
#include "rectangle_tool.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
// Initialize the tool and store a reference of a canvas_buffer
|
// 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;
|
shape = TS_BOX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Draw a box from (x0, y0) to (x1, y1)
|
// Draw a box from (x0, y0) to (x1, y1)
|
||||||
void rectangle_tool::draw(int x0, int y0, int x1, int 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
|
Zusatzaufgabe: Implementieren Sie die Rasterisierung eines
|
||||||
Rechtecks, das von (x0, y0) nach (x1, y1) geht.
|
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
|
// Put debug output into the stream "stream" to be displayed in the
|
||||||
// main window
|
// 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)";
|
||||||
}
|
}
|
Loading…
Reference in a new issue