Add bresenham circle and rectangle

This commit is contained in:
hodasemi 2019-05-20 12:49:29 +02:00
parent 0f847a08ab
commit e2e64daf10
3 changed files with 76 additions and 24 deletions

View file

@ -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);
};

View file

@ -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 <math.h>
// 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<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.
@ -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)";
}

View file

@ -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 <algorithm>
// 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)";
}