OpenGL学习之中点画圆算法

中点画圆算法

在每个布中以单位间隔取样并确定离指定圆最近的像素位置。

代码如下:

#include 
class screenpt
{
private:
	GLint x,y;
public:
	screenpt()
	{
		x = y = 0;
	}
	void setcoords(GLint xcoord,GLint ycoords)
	{
		x = xcoord;
		y = ycoords;
	}
	GLint getx() const
	{
		return x;
	}
	GLint gety() const
	{
		return y;
	}
	void incrementx()
	{
		x++;
	}
	void incrementy()
	{
		y++;
	}
	void decrementx()
	{
		x--;
	}
	void decreamenty()
	{
		y--;
	}
};


void  setpixel(GLint xcoord,GLint ycoord)
{
	glPointSize(5);
	glBegin(GL_POINTS);
	
	glVertex2i(xcoord,ycoord);
	glEnd();
}

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

void circleMIDpoint(GLint xc,GLint yc,GLint radius)
{
	screenpt cirpt;
	GLint p =1-radius;
	cirpt.setcoords(0,radius);
	circlePlotPoints(xc,yc,cirpt);
	while(cirpt.getx()

画出来的效果其实并不是特别的好

OpenGL学习之中点画圆算法_第1张图片

看完上面的中点画圆算法之后我们看看利用极坐标画圆的结果,

因为极坐标的坐标表示为x = cos(theta),y  = sin(theta)

那么很显然就可以使用下面的程序进行画图了,看程序

#include 
#include 
#include 
#include 
#define PI 3.14159f
#define R 5
GLfloat theta = 0.0f;
#define n 100
void init()
{
	glClearColor(0.0,0.0,0.0,0.0);
	glColor3f(1.0,1.0,0.0);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(-10,10,-10,10);
}

void display()
{
	//GLfloat x,y;
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,0.0,0.0);
	glPointSize(5);
	glBegin(GL_POINTS);
	for(int i=0;i

OpenGL学习之中点画圆算法_第2张图片

你可能感兴趣的:(计算机图形学,c++,OpenGL学习,算法,计算机编程,C语言)