Demo entry 6352858

dd

   

Submitted by anonymous on Mar 27, 2017 at 14:32
Language: C++. Code size: 1.8 kB.

#include "gl/glut.h"
#include <stdio.h>
#include <math.h>
const GLfloat PI = 3.1415926;

void draw_star(GLfloat cx, GLfloat cy, GLfloat mx, GLfloat my)
{
	int i;
	GLfloat rate1 = sin(0.1*PI) / sin(0.3*PI);	//内点到中心的距离与外点到中心的距离的比值
	GLfloat rate2 = 1.0f / (rate1);				//外点到中心的距离与内点到中心的距离之比
	 
	const GLfloat cos36 = cos(0.2*PI);	
	const GLfloat sin36 = sin(0.2*PI);

	GLfloat temp_dx = mx-cx;
	GLfloat temp_dy = my-cy;
	GLfloat temp,temp_r;

	GLfloat point[11][2];
	point[0][0] = mx;
	point[0][1] = my;
	point[10][0] = mx;
	point[10][1] = my;
	for (i = 1; i < 10; i++)	//循环计算每个顶点的坐标
	{
		if (i % 2 == 0)			//条件判断改顶点是内点还是外点
			temp_r = rate2;
		else
			temp_r = rate1;

		temp = temp_dx;
		temp_dx = (temp*cos36 - temp_dy*sin36)*temp_r;
		temp_dy = (temp*sin36 + temp_dy*cos36)*temp_r;
		point[i][0] = temp_dx + cx;
		point[i][1] = temp_dy + cy;
	}
	glColor3f(1, 1, 0);
	for (i = 0; i < 10; i++)
	{
		glBegin(GL_TRIANGLES);
		glVertex3f(cx, cy, 0.5);
		glVertex3f(point[i][0],point[i][1],0.5);
		glVertex3f(point[i+1][0], point[i+1][1], 0.5);
		glEnd();
	}
}
void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1, 0, 0);
	glBegin(GL_QUADS);
	glVertex3f(-0.9, 0.6, 0.5);
	glVertex3f(0.9, 0.6, 0.5);
	glVertex3f(0.9, -0.6, 0.5);
	glVertex3f(-0.9, -0.6, 0.5);
	glEnd();
	draw_star(-0.60, 0.30, -0.78, 0.36);	
	draw_star(-0.30, 0.48, -0.24, 0.48);
	draw_star(-0.18, 0.36, -0.24, 0.36);
	draw_star(-0.18, 0.18, -0.18, 0.24);
	draw_star(-0.30, 0.06, -0.24, 0.06);
	glutSwapBuffers();
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
	glutInitWindowPosition(10, 10);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Simple GLUT App");

	glutDisplayFunc(display);
	glutMainLoop();

	return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).