用opengl画五角星_opengl画一个三角形「建议收藏」

用opengl画五角星_opengl画一个三角形「建议收藏」用OPenGL原有的画三角形,正方形,点的函数来作图

不开心,想玩,当家的不许我玩,还说你要玩就玩,那我不管你了,哭哭TAT


用OPenGL原有的画三角形,正方形,点的函数来作图。

画圆是用化曲为直的思想,把圆分为很多分,越分的多越细,越像圆。

画菱形和圆的思想一样,来确定菱形顶点坐标。只是把圆周分的份数少一些


#include "stdafx.h"
#include<gl/glut.h>
#include<math.h>
#include <stdlib.h>
const double PI = 3.14159265357f;
const double R = 0.5f;
const int n = 150;

void myDisplay(void)
{

	glClearColor(1.0, 1.0, 1.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	//画倒三角
	glBegin(GL_TRIANGLES);
	glColor3f(1.0f, 0.0f, 0.0f);  glVertex2f(0.0f, -1.0f);
	glColor3f(0.0f, 1.0f, 0.0f);  glVertex2f(-1.0f, 1.0f);
	glColor3f(1.0f, 1.0f, 0.0f);  glVertex2f(1.0f, 1.0f);
	glEnd();
	//画圆,思想:化曲为直
	glBegin(GL_POLYGON);
	for (int i = 1; i <= n; i++){
		glVertex2f((R*cos(2 * PI / n*i)), R*sin(2 * PI / n*i));
		glColor3f(1.0f, 0.0f, 1.0f);
	}
	glEnd();
	//画五角星
	GLfloat a = 1 / (2 - 2 * cos(72 * PI / 180));
	GLfloat bx = a*cos(18 * PI / 180);
	GLfloat by = a*sin(18 * PI / 180);
	GLfloat cy = -a*cos(18 * PI / 180);
	GLfloat pointB[2] = { bx, by },
		pointC[2] = { 0.5, cy },
		pointD[2] = { -0.5, cy },
		pointE[2] = { -bx, by },
		pointA[2] = { 0, a };

	//按照A->C->E->B->D->A的顺序一笔画出五角星
	glBegin(GL_LINE_LOOP);
	glVertex2fv(pointA);
	glVertex2fv(pointC);
	glVertex2fv(pointE);
	glVertex2fv(pointB);
	glVertex2fv(pointD);
	glVertex2fv(pointA);
	glEnd();
	//画两个小三角
	glBegin(GL_TRIANGLES);
	glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.8f, -0.8f);
	glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(-0.7f, -0.8f);
	glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.75f, -0.7f);
	glEnd();

	glBegin(GL_TRIANGLES);
	glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.8f, -0.8f);
	glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.7f, -0.8f);
	glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.75f, -0.7f);
	glEnd();
	glFlush();

}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(200, 200);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Hellow World!");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}

下图为程序1 2 的运行结果

用opengl画五角星_opengl画一个三角形「建议收藏」用opengl画五角星_opengl画一个三角形「建议收藏」


#include "stdafx.h"
#include<gl/glut.h>
#include<math.h>
#include <stdlib.h>
void myDisplay(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);

	//画白色正方形
	glColor3f(1.0f, 1.0f, 1.0f);
	glRectf(-0.5f, -0.5f, 0.5f, 0.5f);//这个函数只能画正的正方形

	//画红色正方形
	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f); 
	glVertex2f(0.0f, 0.5f);glVertex2f(0.5f, 0.0f);
	glVertex2f(0.0f, -0.5f);
	
	glVertex2f(-0.5f, 0.0f);
	glEnd();

	//画三角形
	glBegin(GL_TRIANGLES);
	glColor3f(0.2f, 0.4f, 0.8f);  glVertex2f(0.5f, 0.5f);
	glColor3f(0.2f, 0.4f, 0.8f);  glVertex2f(-0.5f, 0.5f);
	glColor3f(0.2f, 0.4f, 0.8f);  glVertex2f(0.0f, 1.0f);
	glEnd();

	glBegin(GL_TRIANGLES);
	glColor3f(0.9f, 0.9f, 0.0f);  glVertex2f(0.5f, 0.5f);
	glColor3f(0.9f, 0.9f, 0.0f);  glVertex2f(0.5f, -0.5f);
	glColor3f(0.9f, 0.9f, 0.0f);  glVertex2f(1.0f, 0.0f);
	glEnd();

	glBegin(GL_TRIANGLES);
	glColor3f(0.0f, 1.0f, 0.0f);  glVertex2f(0.5f, -0.5f);
	glColor3f(0.0f, 1.0f, 0.0f);  glVertex2f(-0.5f, -0.5f);
	glColor3f(0.0f, 1.0f, 0.0f);  glVertex2f(0.0f, -1.0f);
	glEnd();

	glBegin(GL_TRIANGLES);
	glColor3f(0.9f, 0.5f, 0.9f);  glVertex2f(-0.5f, 0.5f);
	glColor3f(0.9f, 0.5f, 0.9f);  glVertex2f(-0.5f, -0.5f);
	glColor3f(0.9f, 0.5f, 0.9f);  glVertex2f(-1.0f, 0.0f);
	glEnd();

	glFlush();

}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(200, 200);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Hellow World!");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}


#include "stdafx.h"
#include<gl/glut.h>
#include<math.h>
#include <stdlib.h>
const double PI = 3.14159265357f;


void myDisplay(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	//红色菱形
	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5f, 0.5 *(tan(22.5*PI/180)));
	glVertex2f(1.0f, 0.0f);
	glVertex2f(0.5f, -0.5 *(tan(22.5*PI/180)));
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(-0.5f, 0.5 *(tan(22.5*PI / 180)));
	glVertex2f(-1.0f, 0.0f);
	glVertex2f(-0.5f, -0.5 *(tan(22.5*PI / 180)));
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5*(tan(22.5*PI / 180)),0.5f);
	glVertex2f(0.0f, 1.0f);
	glVertex2f(-0.5*(tan(22.5*PI / 180)),0.5f);
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5*(tan(22.5*PI / 180)), -0.5f);
	glVertex2f(0.0f, -1.0f);
	glVertex2f(-0.5*(tan(22.5*PI / 180)), -0.5f);
	glEnd(); 

	//绿色菱形
	glBegin(GL_POLYGON);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5f, 0.5 *(tan(22.5*PI / 180)));
	glVertex2f(cos(45 * PI / 180), cos(45 * PI / 180));
	glVertex2f(0.5*(tan(22.5*PI / 180)), 0.5f);
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5f, -0.5 *(tan(22.5*PI / 180)));
	glVertex2f(cos(45 * PI / 180), -cos(45 * PI / 180));
	glVertex2f(0.5*(tan(22.5*PI / 180)), -0.5f);
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(-0.5f, 0.5 *(tan(22.5*PI / 180)));
	glVertex2f(-cos(45 * PI / 180), cos(45 * PI / 180));
	glVertex2f(-0.5*(tan(22.5*PI / 180)), 0.5f);
	glEnd();

	glBegin(GL_POLYGON);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(-0.5f, -0.5 *(tan(22.5*PI / 180)));
	glVertex2f(-cos(45 * PI / 180), -cos(45 * PI / 180));
	glVertex2f(-0.5*(tan(22.5*PI / 180)), -0.5f);
	glEnd();

	glFlush();

}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(200, 200);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Hellow World!");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}

运行结果:

用opengl画五角星_opengl画一个三角形「建议收藏」

今天的文章用opengl画五角星_opengl画一个三角形「建议收藏」分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/62559.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注