有圆有方

#include<gl/glut.h>
#include<math.h>
#include <stdlib.h>
const double PI = 3.14159265357f;
const double R = 0.57f;
double red[] = { 1,0,0 };
double blue[] = { 0,0,1 };
double green[] = { 0,1,0 };
void DrawCircle(double radius,double* color) {
	//画圆,凸多边形化曲为直
	int n = 150;
	glBegin(GL_LINE_LOOP);
	glColor3f(color[0], color[1], color[2]);
	for (int i = 1; i <= n; i++) {
		glVertex2f((radius * cos(2 * PI / n * i)), radius * sin(2 * PI / n * i));
	}
	glEnd();
}
void DrawDiamond(double length, double* color) {
	glBegin(GL_LINE_LOOP);
	glColor3f(color[0], color[1], color[2]);
	glVertex2f(0, length); glVertex2f(length, 0);
	glVertex2f(0, -length); glVertex2f(-length, 0);
	glEnd();
}
void DrawRectangle(double length, double* color) {
	glBegin(GL_LINE_LOOP);
	glColor3f(color[0], color[1], color[2]);
	glVertex2f(length, length); glVertex2f(length, -length);
	glVertex2f(-length, -length); glVertex2f(-length, length);
	glEnd();
}
void myDisplay(){
	glClearColor(1.0, 1.0, 1.0, 1.0);//白色背景,前3个是RGB,最后是Alpha值,用来控制透明,1.0表示完全不透明
	glClear(GL_COLOR_BUFFER_BIT);//使用glClearColorz中指定的值设定颜色缓存区的值,即将窗口中的每一个像素设置为背景色
	double l = 0.5;
	for (int i = 0; i < 10; i++) {
		DrawDiamond(l*2, red);
		DrawCircle(l*sqrt(2), blue);
		DrawRectangle(l, green);
		l /= 2;
	}
	glFlush();//清空OpenGL命令缓冲区,强制执行命令缓冲区中所有OpenGL函数
}

int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(200, 200);//设定窗口的初始位置,屏幕左上角为原点,单位为像素
	glutInitWindowSize(500, 500);//设定窗口的大小
	glutCreateWindow("Hellow World!");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值