📄 seripinski.cpp
字号:
#include <windows.h>
#include <gl/glut.h>
#include <math.h>
const double PI=3.1415926;
const float MIN_LEN = 0.0001;
const float LEN = 0.6;
const float g_x1=-LEN, g_y1=-LEN, g_x2=0, g_y2=LEN, g_x3=LEN, g_y3=-LEN;
/// 窗口宽度
int g_width=400;
/// 窗口高度
int g_height=300;
///鼠标器左键是否按下
bool mouse_button_pressed=false;
///记录鼠标器位置
int mouse_x;
///记录鼠标器位置
int mouse_y;
///旋转的纬度
float theta=0;
///旋转的经度
float phi=0;
/**
鼠标按钮回调函数
*/
void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON)
{
if(state == GLUT_DOWN)
//如果鼠标器左键按下,mouse_button_pressed置位
//并记录光标位置
{
mouse_button_pressed = true;
mouse_x = x;
mouse_y = y;
}
else
{
mouse_button_pressed = false;
}
}
}
/**
鼠标运动处理函数
*/
void motion(int x, int y)
{
if(mouse_button_pressed)
{
theta -= y-mouse_y; //根据鼠标器的移动改变旋转的纬度
if(theta<0) theta = 0; //限制纬度在0到180度之间
if(theta>180) theta = 180;
phi += x-mouse_x; //根据鼠标器的移动改变旋转的经度
if(phi<0) phi+=360; //限制经度在0到360度之间
if(phi>360) phi-=360;
mouse_x = x; //更新记录的鼠标器位置
mouse_y = y;
glutPostRedisplay();
//通知系统:窗口需要刷新
}
}
/**
点类
*/
class point {
public:
float x;
float y;
public:
point(float _x, float _y) {x=_x;y=_y;}
point(point& p) {x=p.x; y=p.y;}
point middle(point& p2) {return point((x+p2.x)/2, (y+p2.y)/2);}
float between(point& p2) {return (x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y);}
};
/**
绘制Seripinski垫票
三点坐标为a(x1,y1), b(x2,y2), c(x3,y3)
*/
void seripinski(point& a, point& b, point& c)
{
if (a.between(b) < MIN_LEN && b.between(c) < MIN_LEN) return;
//绘制三角形
glBegin(GL_LINE_LOOP);
glVertex2f(a.x, a.y);
glVertex2f(b.x, b.y);
glVertex2f(c.x, c.y);
glEnd();
//递归绘制三个角
seripinski(a, a.middle(b), a.middle(c));
seripinski(a.middle(b), b, b.middle(c));
seripinski(a.middle(c), b.middle(c), c);
}
/**
\b 主处理回调函数,每当需要重画时由OpenGL库调用
\b 该函数功能是调用seripinski,绘制seripinski垫票
\b 三点坐标分别为a(10, g_height-10),b(g_width/2,g_height-10),c(g_width-10, 10)
*/
void main_display_loop(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //刷新背景
glMatrixMode (GL_PROJECTION); //设置矩阵模式为投影矩阵
glLoadIdentity(); //初始化投影矩阵
glOrtho(-1,1,-1,1,-1,1); //设置平行投影的投影矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(phi,0,1,0); //绕y轴旋转OCS,旋转的角度为phi
glRotatef(theta,1,0,0); //绕x轴旋转OCS,旋转的角度为theta
glColor3f(1, 0, 0);
glPushMatrix();
glTranslatef(0, 0, -0.4);
seripinski(point(g_x1, g_y1), point(g_x2, g_y2), point(g_x3, g_y3));
glPopMatrix();
glColor3f(0, 1, 0);
glPushMatrix();
glTranslatef(0, 0, 0.4);
seripinski(point(g_x1, g_y1), point(g_x2, g_y2), point(g_x3, g_y3));
glPopMatrix();
glColor3f(0, 0, 1);
glPushMatrix();
glTranslatef(0.4, 0, 0);
glRotatef(90, 0, 1, 0);
seripinski(point(g_x1, g_y1), point(g_x2, g_y2), point(g_x3, g_y3));
glPopMatrix();
glColor3f(0, 0.3, 0.5);
glPushMatrix();
glTranslatef(-0.4, 0, 0);
glRotatef(90, 0, 1, 0);
seripinski(point(g_x1, g_y1), point(g_x2, g_y2), point(g_x3, g_y3));
glPopMatrix();
glFlush(); //更新窗口
glutSwapBuffers();
}
/**
OPENGL 特性初始化函数
*/
init()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearColor(0,0,0,1);
glClearDepth(1);
glColor3f(0, 0, 1);
}
/// 主函数
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGBA);
glutInitWindowSize(g_width, g_height);
glutCreateWindow("OpenGL");
init();
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(main_display_loop);
glutMainLoop();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -