[分享]递归绘制 谢宾斯基(Sierpinski)分形三角形
void divide(GLfloat* a, GLfloat* b, GLfloat* c, int k)
{
GLfloat ab[2], ac[2], bc[2];
GLint i;
if (k > 0)
{
glColor3ub(100, 100, 100);
for (i = 0; i < 2; i++)
{
ab[i] = (a[i] + b[i]) / 2;
ac[i] = (a[i] + c[i]) / 2;
bc[i] = (b[i] + c[i]) / 2;
}
divide(a, ab, ac, k-1);
divide(c, ac, bc, k-1);
divide(b, bc, ab, k-1);
}
else
{
glBegin(GL_TRIANGLES);
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
glEnd();
}
}