标签:
中点画圆算法或
其中,yk+1是yk或是yk-1,取决于pk的符号。
为了得到pk+1增量可能是2xk+1+1(如果pk为负)或是2xk+1+1-2yk+1。和2yk+1的求值也可以通过增量的方式进行,即
在起始位置(0, r)处,这两个项的值分别为0和2r 。2xk+1项的每个后续值可以通过对前一值加2或是对2yk+1的前一值减2而得到。
对圆函数在起始位置(x0,y0) = (0,r)处求值,就可以得到初始决策参数:
或
假如将半径r指定为整数,就可以对p0进行简单的取整:
假设以整数屏幕坐标指定圆参数,那么如同Bresenham画线算法中一样,中点方法使用整数加减来计算沿圆周的像素位置。我们可将中点画圆算法的步骤概括如下:
中点画圆算法的步骤2.计算决策参数的初始值:
3.在每个xk位置,从k = 0开始,完成下列测试:假如pk < 0,圆心在(0, 0)的圆的下一个点为(xk+1,yk)并且
否则,圆的下一个点是(xk+1,yk-1),并且
其中2xk+1= 2xk+2且2yk+1= 2yk-2。
4.确定在其他七个八分圆中的对称点。
5.将每个计算出的像素位置移动到圆心在(xc,yc)的圆路径上,并画坐标值:
给定圆半径r = 10,我们将演示中点画圆算法,确定在第一象限从x = 0到x = y沿八分圆的像素位置。决策参数的初始值为
使用中点画圆算法计算的后继决策参数值和沿圆路径的位置为
#include <GL/glut.h>
class screenPt{
private:Glint x,y;
public:
/*Default Constructor :initializes coordinate position to (0,0) */
screenPt(){
x = y = 0;
}
void setCoords(GLint xCoordValue,GLint yCorrdValue){
x = xCoordValue;
y = yCorrdValue;
}
Glint getx() const{
return x;
}
GLint gety() const{
return y;
}
void incrementx(){
x++;
}
void incrementy(){
y--;
}
};
void setPixel(GLint xCoord,GLint yCoord){
glBegin(GL_POINTS);
glVertex2i(xCoord,yCoord);
glEnd();
}
void circleMidpoint(GLint xc,GLint yc,GLint radius){
screenPt circPt;
GLInt p = 1 - radius;//Initial value for midpoint parameter
circPt.setCoords(0,radius);//Set coords for top point of circle
void circlePlotPoints(GLint,GLint,screenPt);
/*Plot the initial point in each circle quadrant*/
circlePlotPoints(xc,yc,circPt);
/*Calculate next point and plot in each octant*/
while(circPt.getx() < circPt.gety()){
circPt.incrementx ();
if (p < 0)
p += 2 * circPt.getx() + 1;
else{
circPt.decrementy ();
p += 2 * (circPt.getx() - circPt.gety()) + 1;
}
circlePlotPoints(xc, xy, circPt);
}
}
void circlePlotPoints(GLint xc,GLint yc,screenPt circPt)
{
setPixel(xc + circPt.getx(), yc + circPt.gety());
setPixel(xc - circPt.getx(), yc + circPt.gety());
setPixel(xc + circPt.getx(), yc - circPt.gety());
setPixel(xc - circPt.getx(), yc - circPt.gety());
setPixel(xc + circPt.gety(), yc + circPt.getx());
setPixel(xc - circPt.gety(), yc + circPt.getx());
setPixel(xc + circPt.gety(), yc - circPt.getx());
setPixel(xc - circPt.gety(), yc - circPt.getx());
}
计算机图形学(二)输出图元_6_OpenGL曲线函数_2_中点画圆算法
标签:
原文地址:http://blog.csdn.net/heyuchang666/article/details/51200357