Pen circle drawing revisited

This commit is contained in:
James Turk 2005-08-23 21:55:03 +00:00
parent 2d8ba79bf0
commit ab008b050f
3 changed files with 28 additions and 59 deletions

View File

@ -1,10 +1,12 @@
Changelog for Photon Changelog for Photon
$Id: CHANGELOG.txt,v 1.11 2005/08/18 07:05:34 cozman Exp $ $Id: CHANGELOG.txt,v 1.12 2005/08/23 21:55:03 cozman Exp $
! : Major Changes (potentially breaking existing code) ! : Major Changes (potentially breaking existing code)
+ : New Features + : New Features
* : Minor Changes (bugfixes and behind-the-scenes changes) * : Minor Changes (bugfixes and behind-the-scenes changes)
0.1.0
* Rewrote circle drawing code in Pen, major speed up.
0.0.2 - Released 2005-18-08 0.0.2 - Released 2005-18-08
! Removed InputListener, opting to move features into State class. ! Removed InputListener, opting to move features into State class.

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Pen.hpp,v 1.3 2005/07/20 03:58:54 cozman Exp $ // $Id: Pen.hpp,v 1.4 2005/08/23 21:55:03 cozman Exp $
#ifndef PHOTON_VIDEO_PEN_HPP #ifndef PHOTON_VIDEO_PEN_HPP
#define PHOTON_VIDEO_PEN_HPP #define PHOTON_VIDEO_PEN_HPP
@ -134,6 +134,9 @@ public:
// data members // data members
private: private:
Color color_; Color color_;
// 30 seems to be an even tradeoff between speed and roundness
static const int CIRCLE_RESOLUTION = 30;
}; };
} }

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Pen.cpp,v 1.5 2005/07/20 03:58:54 cozman Exp $ // $Id: Pen.cpp,v 1.6 2005/08/23 21:55:03 cozman Exp $
#include "video/Pen.hpp" #include "video/Pen.hpp"
@ -125,39 +125,21 @@ void Pen::fillRect(const math::Rect &rect) const
void Pen::drawCircle(const math::Circle &circle) const void Pen::drawCircle(const math::Circle &circle) const
{ {
//written from Bresenham's circle algorithm scalar radius(circle.getRadius());
double cx(circle.getCenter().x); scalar cx(circle.getCenter().x);
double cy(circle.getCenter().y); scalar cy(circle.getCenter().y);
double d(3-(2*circle.getRadius())); scalar angle(0);
double x(0); const scalar angleStep(2*math::PI/CIRCLE_RESOLUTION);
double y(circle.getRadius());
glBindTexture(GL_TEXTURE_2D,0); glBindTexture(GL_TEXTURE_2D,0);
glPushAttrib(GL_CURRENT_BIT); glPushAttrib(GL_CURRENT_BIT);
color_.makeGLColor(); color_.makeGLColor();
glBegin(GL_POINTS); // draw points, creating a circle glBegin(GL_LINE_LOOP);
while(y > x) for(int i=0; i<CIRCLE_RESOLUTION; ++i)
{ {
glVertex2d(cx+x,cy+y); glVertex2f(cx+radius*std::cos(angle), cy+radius*std::sin(angle));
glVertex2d(cx+x,cy-y); angle += angleStep;
glVertex2d(cx-x,cy+y);
glVertex2d(cx-x,cy-y);
glVertex2d(cx+y,cy+x);
glVertex2d(cx+y,cy-x);
glVertex2d(cx-y,cy+x);
glVertex2d(cx-y,cy-x);
if(d < 0)
{
d += x*4 + 6;
}
else
{
d += (x-y)*4 + 10;
--y;
}
++x;
} }
glEnd(); glEnd();
glPopAttrib(); glPopAttrib();
@ -165,39 +147,21 @@ void Pen::drawCircle(const math::Circle &circle) const
void Pen::fillCircle(const math::Circle &circle) const void Pen::fillCircle(const math::Circle &circle) const
{ {
//written from Bresenham's circle algorithm scalar radius(circle.getRadius());
double cx(circle.getCenter().x); scalar cx(circle.getCenter().x);
double cy(circle.getCenter().y); scalar cy(circle.getCenter().y);
double d(3-(2*circle.getRadius())); scalar angle(0);
double x(0); const scalar angleStep(2*math::PI/CIRCLE_RESOLUTION);
double y(circle.getRadius());
glBindTexture(GL_TEXTURE_2D,0); glBindTexture(GL_TEXTURE_2D,0);
glPushAttrib(GL_CURRENT_BIT); glPushAttrib(GL_CURRENT_BIT);
color_.makeGLColor(); color_.makeGLColor();
glBegin(GL_LINES); // draw lines instead of points, filling the circle glBegin(GL_POLYGON);
while(y > x) for(int i=0; i<CIRCLE_RESOLUTION; ++i)
{ {
glVertex2d(cx+x,cy+y); glVertex2f(cx+radius*std::cos(angle), cy+radius*std::sin(angle));
glVertex2d(cx+x,cy-y); angle += angleStep;
glVertex2d(cx-x,cy-y);
glVertex2d(cx-x,cy+y);
glVertex2d(cx+y,cy+x);
glVertex2d(cx-y,cy+x);
glVertex2d(cx-y,cy-x);
glVertex2d(cx+y,cy-x);
if(d < 0)
{
d += x*4 + 6;
}
else
{
d += (x-y)*4 + 10;
--y;
}
++x;
} }
glEnd(); glEnd();
glPopAttrib(); glPopAttrib();