⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lab1.cpp

📁 a source code for banker s algorithm
💻 CPP
字号:
#include<stdio.h>
#include<math.h>
#include<glut.h>
#include<windows.h>

typedef float point[3];
point v[4] ={{0.0,0.0,0.0},{0.0,0.942809,-0.333333},{-0.816497,-0.471405,-0.333333},{0.816497,-0.471405,-0.333333}};
//static GLfloat theta[]={0.0,0.0,0.0};
int n=3;

void triangle(point a,point b,point c)
{
	glBegin(GL_POLYGON);
		glNormal3fv(a);
		glVertex3fv(a);
	    glVertex3fv(b);
		glVertex3fv(c);
	glEnd();
}

void divide_triangle(point a,point b,point c,int m)
{
  point v1,v2,v3;
  int j;
  if(m>0)
  {
	for(j=0;j<3;j++)
		v1[j]=(a[j]+b[j])/2.0;
    for(j=0;j<3;j++)
        v2[j]=(a[j]+c[j])/2.0;
    for(j=0;j<3;j++)
        v3[j]=(b[j]+c[j])/2.0;

	divide_triangle(a,v1,v2,m-1);
	divide_triangle(c,v2,v3,m-1);
	divide_triangle(b,v3,v1,m-1);

  }
  else
	  triangle (a,b,c);
}

void tetrahedron(int m)
{
	glColor3f(1,0,0);
	divide_triangle(v[0],v[1],v[2],m);
	glColor3f(0,1,0);
	divide_triangle(v[3],v[2],v[1],m);
	glColor3f(1,0,0);
	divide_triangle(v[0],v[3],v[1],m);
	glColor3f(1,0,0);
	divide_triangle(v[0],v[2],v[3],m);
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	tetrahedron(n);
	glFlush();
}

void myReshape(int w,int h)
{
	glViewport(0,0,w,h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if(w<=h)
		glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
	else
		glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,-10.0,10.0);
	glMatrixMode(GL_MODELVIEW);
	glutPostRedisplay();
}

void main()
{
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
	glutInitWindowSize(500.0,500.0);
	glutCreateWindow("A");
	glutReshapeFunc(myReshape);
	glutDisplayFunc(display);
	glEnable(GL_DEPTH_TEST);
	glClearColor(1,1,1,0);
	glutMainLoop();
}
		
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -