Add bresenham circle and rectangle
This commit is contained in:
parent
0f847a08ab
commit
e2e64daf10
3 changed files with 76 additions and 24 deletions
|
@ -26,4 +26,7 @@ public:
|
|||
// Put debug output into the stream "stream" to be displayed in the
|
||||
// main window
|
||||
void set_text(std::stringstream &stream);
|
||||
|
||||
private:
|
||||
void draw_circle(int xc, int yc, int x, int y);
|
||||
};
|
|
@ -14,7 +14,17 @@ bresenham_circle_tool::bresenham_circle_tool(canvas_buffer& canvas): tool_base(c
|
|||
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)
|
||||
|
@ -31,9 +41,35 @@ 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
|
||||
|
|
|
@ -6,15 +6,12 @@
|
|||
#include "rectangle_tool.h"
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
// Initialize the tool and store a reference of a canvas_buffer
|
||||
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,9 +21,25 @@ 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
|
||||
|
|
Loading…
Reference in a new issue