我编写了以下代码来使用OpenGL打印Koch曲线。但我得到一个倒置的输出。在OpenGL中反转输出需要哪些修改?另外,还有其他公式可以找到Koch曲线的等边三角形的顶点坐标吗?
#include<iostream>
#include<GL/glut.h>
#define COS 0.5
#define SIN 0.866
int x1=10,x2=550,y1=200,y2=200;
void myinit()
{
glClearColor(0.0,1.0,0.0,1.0);
glColor3f(0.0,0.0,1.0);
glPointSize(3.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,640,0,480);
}
void koch(int x1, int y1, int x2, int y2, int m)
{
int x[5],y[5],xx,yy,lx,ly;
lx=(x2-x1)/3;
ly=(y2-y1)/3;
x[0]=x1;
y[0]=y1;
x[4]=x2;
y[4]=y2;
x[1]=x[0]+lx;
y[1]=y[0]+ly;
x[3]=x[0]+2*lx;
y[3]=y[0]+2*ly;
xx=x[3]-x[1];
yy=y[3]-y[1];
x[2]=xx*(COS)+yy*(SIN);
y[2]=-xx*(SIN)+yy*(COS);
x[2]=x[1]+x[2];
y[2]=y[1]+y[2];
if(m>0)
{
koch(x[0],y[0],x[1],y[1],m-1);
koch(x[1],y[1],x[2],y[2],m-1);
koch(x[2],y[2],x[3],y[3],m-1);
koch(x[3],y[3],x[4],y[4],m-1);
}
else
{
glBegin(GL_LINES);
glVertex2d(x[0],y[0]);
glVertex2d(x[1],y[1]);
glEnd();
glBegin(GL_LINES);
glVertex2d(x[1],y[1]);
glVertex2d(x[2],y[2]);
glEnd();
glBegin(GL_LINES);
glVertex2d(x[2],y[2]);
glVertex2d(x[3],y[3]);
glEnd();
glBegin(GL_LINES);
glVertex2d(x[3],y[3]);
glVertex2d(x[4],y[4]);
glEnd();
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
koch(x1,y1,x2,y2,2);
glEnd();
glFlush();
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutCreateWindow("Grllp");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}