[OpenGL]课后案例03:三维Sierpinski镂垫的递归程序

//A.3  三维Sierpinski镂垫的递归程序
/* Recursive subdivision of a tetrahedron to form 3D Sierpinski 
gasket */
/* number of recursive steps given on command line */
#include <stdlib.h>
#include <GL/glut.h>
/* initial tetrahedron */
GLfloat v[4][3]={{0.0, 0.0, 1.0},{0.0, 0.942809, -0.33333},
{-0.816497, -0.471405, -0.333333},{0.816497, -0.471405, 
-0.333333}};
GLfloat colors[4][3]={{1.0,0.0,0.0},{0.0,1.0,0.0},
{0.0,0.0,1.0},{0.0,0.0,0.0}};
int n;
void triangle( GLfloat *va, GLfloat *vb, GLfloat *vc)
{
	glVertex3fv(va);
	glVertex3fv(vb);
	glVertex3fv(vc);
}
void tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d)
{
	glColor3fv(colors[0]);
	triangle(a,b,c);
	glColor3fv(colors[1]);
	triangle(a,c,d);
	glColor3fv(colors[2]);
	triangle(a,d,b);
	glColor3fv(colors[3]);
	triangle(b,d,c);

}
void divide_tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d, int 
	m)
{
	GLfloat mid[6][3];
	int j;
	if(m>0)
	{
		/* compute six midpoints */
		for(j=0; j<3; j++) mid[0][j]=(a[j]+b[j])/2;
		for(j=0; j<3; j++) mid[1][j]=(a[j]+c[j])/2;
		for(j=0; j<3; j++) mid[2][j]=(a[j]+d[j])/2;
		for(j=0; j<3; j++) mid[3][j]=(b[j]+c[j])/2;
		for(j=0; j<3; j++) mid[4][j]=(c[j]+d[j])/2;
		for(j=0; j<3; j++) mid[5][j]=(b[j]+d[j])/2;
		/* create 4 tetrahedrons  by subdivision */
		divide_tetra(a,mid[0],mid[1],mid[2], m-1);
		divide_tetra(mid[0],b,mid[3],mid[5], m-1);
		divide_tetra(mid[1],mid[3],c,mid[4], m-1);
		divide_tetra(mid[2],mid[4],d,mid[5], m-1);
	}
	else  tetra(a,b,c,d); /* draw tetrahedron at end of recursion */
}
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBegin(GL_TRIANGLES);
	divide_tetra(v[0],v[1],v[2],v[3],n);
	glEnd();
	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(int argc, char **argv)
{
	n=10;
	//n=atoi(argv[1]); /* or set number of subdivision steps here */
	//注意,上句若是在VS或VC++中直接使用会报错,因为需要输入一个参数。
	//可以考虑使用命令提示行,在windows下打开项目的debug文件夹,按住shift键右击
	//选择:在当前位置打开命令提示行。
	//输入:Main.exe 10
	//其中,Main.exe根据自己情况确定,后面的10是递归次数,可以自定义
	//实际检测,当数字大于15会出现卡死状态,数字大于20直接死机
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(500, 500);
	glutCreateWindow("3D Gasket");
	glutReshapeFunc(myReshape);
	glutDisplayFunc(display);
	glEnable(GL_DEPTH_TEST); 
	glClearColor (1.0, 1.0, 1.0, 1.0);
	glutMainLoop();
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个基于C#语言的绘制Sierpinski片的程序。以下是代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace SierpinskiTriangle { public partial class Form1 : Form { private int depth = 0; private int size = 0; private Graphics g; public Form1() { InitializeComponent(); g = CreateGraphics(); } private void button1_Click(object sender, EventArgs e) { depth = int.Parse(textBox1.Text); size = int.Parse(textBox2.Text); DrawSierpinski(new PointF(ClientSize.Width / 2, 50), size, depth); } private void DrawSierpinski(PointF p, int size, int depth) { if (depth == 0) { DrawTriangle(p, size); } else { DrawSierpinski(new PointF(p.X - size / 2, p.Y + size / 2), size / 2, depth - 1); DrawSierpinski(new PointF(p.X + size / 2, p.Y + size / 2), size / 2, depth - 1); DrawSierpinski(new PointF(p.X, p.Y - size / 2), size / 2, depth - 1); } } private void DrawTriangle(PointF p, int size) { PointF[] points = { new PointF(p.X, p.Y - size / 2), new PointF(p.X - size / 2, p.Y + size / 2), new PointF(p.X + size / 2, p.Y + size / 2) }; g.FillPolygon(Brushes.Blue, points); } } } ``` 这个程序使用了Windows Form,所以您需要将它保存为一个名为Form1.cs的文件。在窗体上添加两个文本框和一个按钮,用于输入Sierpinski片的深度和大小,以及开始绘制的命令。您需要在程序中引用System.Drawing和System.Windows.Forms。当您单击按钮时,程序将根据您输入的深度和大小来绘制Sierpinski片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值