#include "Dependencies\glew\glew.h"
#include "Dependencies\freeglut\freeglut.h"
#include <stdio.h>
GLint array[1000][2]; //保存要显示的坐标点
int length=0;
class screenPt {
private:
GLint x, y;
public:
/* Default Constructor:initializes coordinate position to (0,0). */
screenPt() {
x = y = 0;
}
void setCoords(GLint xCoordValue, GLint yCoordValue) {
x = xCoordValue;
y = yCoordValue;
}
GLint getx() const {
return x;
}
GLint gety() const {
return y;
}
void incrementx() {
x++;
}
void decrementy() {
y--;
}
};
void init(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);//窗口颜色(红:[0.0~1.0]R, 绿:[0.0~1.0]G,蓝:[0.0~1.0]B, 透明度:[0.0~1.0]A)
/*使用正投影将世界坐标系二位矩形区域的内容映射到屏幕上,区域的x坐标值从0.0到200.0,y坐标值从0.0到150.0*/
glMatrixMode(GL_PROGRAM);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void setPixel(void)
{
glClear(GL_COLOR_BUFFER_BIT);//GL_COLOR_BUFFER_BIT是一个OpenGL符号常量,用来指定它的颜色缓存(刷新缓存)中的位值,该缓存将使用glClearColor函数中指定的值来设定。
glColor3f(1.0f, 0.0f, 0.0f);//设置线段颜色为红色
glPointSize(5.f); //设置点的大小
glBegin(GL_POINTS);
for (size_t i = 0; i <=length; i++)
{
glVertex2i(array[i][1], array[i][2]);
}
glEnd();
glFlush();//执行所有的OpenGL程序
}
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());
printf("(%d,%d)\n", xc + circPt.getx(), yc + circPt.gety());
//要显示的坐标点保存至array[1000][2]
array[length][1] = xc + circPt.getx(), array[length][2] = yc + circPt.gety();
}
void circleMidpoint(void)
{
screenPt circPt;
GLint xc = 0;
GLint yc = 0;
GLint radius = 50;
GLint p = 1 - radius; // Initial value for midpiont 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()) {
length++;
circPt.incrementx();
if (p<0)
p += 2 * circPt.getx() + 1;
else {
circPt.decrementy();
p += 2 * (circPt.getx() - circPt.gety()) + 1;
}
circlePlotPoints(xc, yc, circPt);
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);//初始化GULT
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);//设置显示模式
glutInitWindowPosition(300,500);//设置左上角窗口显示位置
glutInitWindowSize(400, 300);//设置窗口显示的宽与高
glutCreateWindow("中点画圆算法");//创建一个窗口
init();//执行初始化程序
circleMidpoint();
glutDisplayFunc(setPixel);//把图形显示在窗口
glutMainLoop();//显示所有并进入等待状态
}