📄 lab3.cpp
字号:
// Lab3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "GL/glut.h"
#include "Polygon.h"
#include "Pie.h"
#include "Rect.h"
CPolygon poly; //多边形的类
CPie pie;
CRect rect;
int mainmenu = -1;
bool m_bRect =0;//绘制条形图标志
bool m_bPie =0;//绘制饼状图标志
void RenderScene()
{
glClearColor(255,255,255,255); //设置背景色为全白色
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,0);
glEnd();
glFlush();
if(m_bRect) //m_bRect为1时调用画条形图的函数
rect.Draw();
if(m_bPie)
pie.Draw(); //m_bPie为1时调用画条形图的函数
}
void MouseFunc(int button,int state,int x,int y) //x,y为实际获得的坐标
{
int w = glutGet(GLUT_WINDOW_WIDTH); //获得窗口的宽度
int h = glutGet(GLUT_WINDOW_HEIGHT); //获得窗口的高度
x=x-w/2; //将实际获得的点坐标转化为所定的坐标轴上的点
y=-(y-h/2);
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN) //相应左键点下
{
poly.point[poly.num++].SetPoint(x,y); //保存点到数组poly.point中
glBegin(GL_POINTS);
glVertex2f(x,y); //画点,多边形的定点
glEnd();
glFlush();
}
if(button==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN)
poly.Fill(); //调用填充所画多边形区域的函数
}
void menu( int entry )
{
switch ( entry )
{
case 1: //画条形图
m_bPie = 0; //m_bPie为0时,不画饼状图,为1时画饼状图
m_bRect = 1; //m_bRect为0时,不画条形图,为1时画条形图
break;
case 2: //画饼状图
m_bRect = 0;
m_bPie = 1;
break;
}
RenderScene();
}
int main(int argc, char* argv[])
{
glutInitWindowSize(500,400); //确定所创建窗口的大小,500位宽,400为高
glutInitWindowPosition(100,100); //确定窗口出现的位置
glutCreateWindow("点左键画点,点中键填充,点右键选其它图形");
gluOrtho2D(-250,250,-200,200); //坐标原点的位置
glutDisplayFunc(RenderScene);
mainmenu = glutCreateMenu(menu); //创建菜单
glutAddMenuEntry("条形图", 1); // 1 表示画条状图
glutAddMenuEntry("饼状图", 2); // 2 表示画饼状图
glutAttachMenu( GLUT_RIGHT_BUTTON ); // 定义菜单动作方式:点击右键弹出
glutMouseFunc(MouseFunc); //调用鼠标的操作函数
glutMainLoop();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -