2018-12-25 13:41:30 +00:00
|
|
|
|
#ifndef graphs_h
|
|
|
|
|
#define graphs_h
|
|
|
|
|
|
|
|
|
|
#include <type.hpp>
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
//计算函数执行时间间隔
|
2018-12-25 13:41:30 +00:00
|
|
|
|
#define DRAW_TIMER 1
|
2018-12-26 09:32:18 +00:00
|
|
|
|
//绘图函数执行时间间隔
|
2018-12-25 13:41:30 +00:00
|
|
|
|
#define FRESH_TIMER 20
|
|
|
|
|
|
|
|
|
|
typedef std::pair<double, double> Size;
|
|
|
|
|
typedef std::pair<double, double> Point;
|
|
|
|
|
typedef std::vector<double> Point3;
|
|
|
|
|
typedef std::vector<double> RawColor;
|
|
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::vector;
|
|
|
|
|
using std::list;
|
|
|
|
|
using std::pair;
|
|
|
|
|
using std::map;
|
|
|
|
|
|
|
|
|
|
class Field;
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
//绘图函数声明
|
2018-12-25 13:41:30 +00:00
|
|
|
|
unsigned long fresh_screen(int signal);
|
2018-12-26 09:32:18 +00:00
|
|
|
|
//计算函数声明
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void draw_fields(int signal);
|
|
|
|
|
|
2018-12-27 02:03:40 +00:00
|
|
|
|
int create_fields(int hw);
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/************************************************
|
|
|
|
|
颜色的抽象
|
|
|
|
|
***/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
class Color{
|
|
|
|
|
public:
|
2018-12-26 09:32:18 +00:00
|
|
|
|
// 颜色的RGB值,范围从0到1
|
2018-12-25 13:41:30 +00:00
|
|
|
|
double red, green, blue;
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
2018-12-25 13:41:30 +00:00
|
|
|
|
Color():red(1.0),blue(1.0),green(1.0){
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
2018-12-25 13:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
Color(RawColor color){
|
|
|
|
|
red = color[0]/255;
|
|
|
|
|
blue = color[1]/255;
|
|
|
|
|
green = color[2]/255;
|
|
|
|
|
}
|
|
|
|
|
Color(const Color &color){
|
|
|
|
|
red = color.red;
|
|
|
|
|
blue = color.blue;
|
|
|
|
|
green = color.green;
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
设置颜色对象的颜色
|
|
|
|
|
|
|
|
|
|
@param color RawColor颜色数据
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void setColor(RawColor color){
|
|
|
|
|
red = color[0]/255;
|
|
|
|
|
green = color[1]/255;
|
|
|
|
|
blue = color[2]/255;
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
设置颜色对象的颜色
|
|
|
|
|
|
|
|
|
|
@param color Color颜色数据
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void setColor(Color color){
|
|
|
|
|
red = color.red;
|
|
|
|
|
blue = color.blue;
|
|
|
|
|
green = color.green;
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
// 将颜色按照相关比例变亮
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void brighter(void){
|
|
|
|
|
red = sqrt(red);
|
|
|
|
|
blue = sqrt(blue);
|
|
|
|
|
green = sqrt(green);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/************************************************
|
|
|
|
|
图形的抽象
|
|
|
|
|
***/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
class Shape{
|
|
|
|
|
public:
|
2018-12-26 09:32:18 +00:00
|
|
|
|
// 图形形状
|
2018-12-25 13:41:30 +00:00
|
|
|
|
GLenum type;
|
2018-12-26 09:32:18 +00:00
|
|
|
|
// 点大小和画笔大小
|
2018-12-25 13:41:30 +00:00
|
|
|
|
GLfloat point_size, line_size;
|
2018-12-26 09:32:18 +00:00
|
|
|
|
// 图形顶点的坐标
|
2018-12-25 13:41:30 +00:00
|
|
|
|
vector<Point3> points;
|
2018-12-26 09:32:18 +00:00
|
|
|
|
// 图形颜色
|
2018-12-25 13:41:30 +00:00
|
|
|
|
Color color;
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
Shape():type(GL_POINTS),point_size(2.0f),line_size(2.0f),color({255,255,255}){
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
根据储存的信息调用glut函数库中的函数进行绘图前的初始化操作
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void flesh(void){
|
|
|
|
|
if(point_size != 1.0f)
|
|
|
|
|
glPointSize(point_size);
|
|
|
|
|
else if(line_size != 1.0f)
|
|
|
|
|
glLineWidth(line_size);
|
|
|
|
|
glColor3f(color.red, color.blue, color.green);
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
设置图形对象的颜色
|
|
|
|
|
|
|
|
|
|
@param color RawColor颜色数据
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void setColor(RawColor color){
|
|
|
|
|
this->color.setColor(color);
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
设置图形对象的颜色
|
|
|
|
|
|
|
|
|
|
@param color Color颜色数据
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void setColor(Color color){
|
|
|
|
|
this->color.setColor(color);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
将图形对象的形状设置为矩形
|
|
|
|
|
|
|
|
|
|
@param pos 对角线两端点坐标
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void setRetangle(const pair<Point3,Point3> pos){
|
|
|
|
|
double fx = pos.first[0],fy = pos.first[1];
|
|
|
|
|
double sx = pos.second[0], sy = pos.second[1];
|
|
|
|
|
setPolygon({{fx,fy,0},{sx,fy,0},{sx,sy,0},{fx,sy,0}});
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
将图形对象的形状设置为多边形
|
|
|
|
|
|
|
|
|
|
@param pos 多边形每个顶点的坐标
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void setPolygon(const vector<Point3> pos){
|
|
|
|
|
type = GL_POLYGON;
|
|
|
|
|
for(auto i = pos.begin(); i != pos.end(); i++){
|
|
|
|
|
points.push_back(*i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/************************************************
|
|
|
|
|
控件的抽象
|
|
|
|
|
***/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
class View2D {
|
|
|
|
|
public:
|
|
|
|
|
Point pos;
|
|
|
|
|
vector<Shape> shapes;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/************************************************
|
|
|
|
|
进度条控件的抽象
|
|
|
|
|
***/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
class Bar:public View2D {
|
|
|
|
|
public:
|
|
|
|
|
Bar(Point, GLint width){
|
2018-12-26 07:07:19 +00:00
|
|
|
|
//Shape *p_shape = new Shape();
|
2018-12-25 13:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/************************************************
|
|
|
|
|
窗体的抽象
|
|
|
|
|
***/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
class Window {
|
|
|
|
|
public:
|
2018-12-26 09:32:18 +00:00
|
|
|
|
// 储存指向窗体中要包含的图形对象的指针
|
2018-12-25 13:41:30 +00:00
|
|
|
|
static list<Shape *> shapes;
|
2018-12-26 09:32:18 +00:00
|
|
|
|
// 储存指向窗体中要包含的控件对象的指针
|
2018-12-25 13:41:30 +00:00
|
|
|
|
static map<string,View2D *> menus;
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
2018-12-25 13:41:30 +00:00
|
|
|
|
Window(int argc, char **argv){
|
|
|
|
|
glutInit(&argc, argv);
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
设置显示模式
|
|
|
|
|
|
|
|
|
|
@param Mode 参照GLUT函数库相关说明即可
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void playMode(int Mode){
|
|
|
|
|
glutInitDisplayMode(Mode);
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
设置窗体大小
|
|
|
|
|
|
|
|
|
|
@param sizexy 输入Size数据
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void size(Size sizexy){
|
|
|
|
|
glutInitWindowSize(static_cast<int>(sizexy.first), static_cast<int>(sizexy.second));
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
设置窗体的初始位置
|
|
|
|
|
|
|
|
|
|
@param positionxy 输入Point数据
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void position(Point positionxy){
|
|
|
|
|
glutPositionWindow(positionxy.first, positionxy.second);
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
创建窗体
|
|
|
|
|
|
|
|
|
|
@param name 窗体的标题名字
|
|
|
|
|
@param func 指向自定义绘制函数的函数指针
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void create(string name = "No Name",void (*func)() = Window::displayEngine){
|
|
|
|
|
glutCreateWindow(name.data());
|
|
|
|
|
glutDisplayFunc(func);
|
|
|
|
|
glutMouseFunc(mouseListener);
|
|
|
|
|
glutTimerFunc(DRAW_TIMER, draw_fields, 1);
|
|
|
|
|
glutTimerFunc(FRESH_TIMER, rendEngine, 1);
|
|
|
|
|
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
设置窗体的初始颜色
|
|
|
|
|
|
|
|
|
|
@param color 输入Color数据
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void setColor(Color color){
|
|
|
|
|
glClearColor(color.red, color.blue, color.green, 1.0f);
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
显示窗体
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void show(void){
|
|
|
|
|
glutMainLoop();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
进行图形对象的实际绘制操作
|
|
|
|
|
|
|
|
|
|
@param shape 指向目标图形对象的指针
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
static void rendShape(Shape *shape){
|
|
|
|
|
if(shape->point_size != 1.0f)
|
|
|
|
|
glPointSize(shape->point_size);
|
|
|
|
|
else if(shape->line_size != 1.0f)
|
|
|
|
|
glLineWidth(shape->line_size);
|
|
|
|
|
glColor3f(shape->color.red, shape->color.blue, shape->color.green);
|
|
|
|
|
glBegin(shape->type);
|
|
|
|
|
for(auto k = shape->points.begin(); k != shape->points.end();k++){
|
|
|
|
|
glVertex3f((*k)[0],(*k)[1],(*k)[2]);
|
|
|
|
|
}
|
|
|
|
|
glEnd();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
默认的绘制函数
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
static void displayEngine(void){
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
for(auto i = shapes.begin(); i != shapes.end(); i++)
|
|
|
|
|
rendShape(*i);
|
|
|
|
|
//for(auto i = menus.begin(); i != menus.end(); i++)
|
|
|
|
|
//rendShape((*i).second);
|
|
|
|
|
glutSwapBuffers();
|
|
|
|
|
}
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
默认的鼠标相关事件的监听函数
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
static void mouseListener(int button,int state,int x,int y){
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
窗口刷新注册函数
|
|
|
|
|
|
|
|
|
|
@param value 无用值
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
static void rendEngine(int value){
|
|
|
|
|
unsigned long ms = fresh_screen(0);
|
|
|
|
|
unsigned int now_fresh = (unsigned int)(FRESH_TIMER - ms);
|
|
|
|
|
if(now_fresh < 0 || now_fresh > FRESH_TIMER) now_fresh = 0;
|
|
|
|
|
glutTimerFunc(now_fresh, rendEngine, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
在窗体中直接绘制三角形并可以指定其填充颜色
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void draw_triangle(vector<Point3> pos, RawColor color={255, 255, 255}){
|
|
|
|
|
Shape *p_shape = new Shape;
|
|
|
|
|
p_shape->type = GL_TRIANGLES;
|
|
|
|
|
p_shape->setColor(color);
|
|
|
|
|
for(auto i = pos.begin(); i != pos.end(); i++){
|
|
|
|
|
p_shape->points.push_back(*i);
|
|
|
|
|
}
|
|
|
|
|
draw_shape(p_shape);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
直接在窗体中绘制点并可以指定其填充颜色
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void draw_point(Point3 pos, GLfloat size = 1.0f, RawColor color={255, 255, 255}){
|
|
|
|
|
Shape *p_shape = new Shape;
|
|
|
|
|
p_shape->type = GL_POINTS;
|
|
|
|
|
p_shape->point_size = size;
|
|
|
|
|
p_shape->setColor(color);
|
|
|
|
|
p_shape->points.push_back(pos);
|
|
|
|
|
draw_shape(p_shape);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
直接在窗体中绘制多边形并可以指定其填充颜色
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void draw_polygon(vector<Point3> pos, RawColor color={255,255,255}){
|
|
|
|
|
Shape *p_shape = new Shape;
|
|
|
|
|
p_shape->type = GL_POLYGON;
|
|
|
|
|
p_shape->setColor(color);
|
|
|
|
|
p_shape->setPolygon(pos);
|
|
|
|
|
draw_shape(p_shape);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
直接在窗体中绘制线并可以指定其填充颜色
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void draw_line(pair<Point3,Point3> pos, GLfloat size = 1.0f, RawColor color={255,255,255}){
|
|
|
|
|
Shape *p_shape = new Shape;
|
|
|
|
|
p_shape->type = GL_LINES;
|
|
|
|
|
p_shape->line_size = size;
|
|
|
|
|
p_shape->setColor(color);
|
|
|
|
|
p_shape->points.push_back(pos.first);
|
|
|
|
|
p_shape->points.push_back(pos.second);
|
|
|
|
|
draw_shape(p_shape);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
直接在窗体中绘制矩形并可以指定其填充颜色
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void draw_retangle(pair<Point3,Point3> pos, RawColor color={255,255,255}){
|
|
|
|
|
double fx = pos.first[0],fy = pos.first[1];
|
|
|
|
|
double sx = pos.second[0], sy = pos.second[1];
|
|
|
|
|
draw_polygon({{fx,fy},{sx,fy},{sx,sy},{fx,sy}},color);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
在窗体中添加图形对象
|
|
|
|
|
|
|
|
|
|
@param shape 指向目标图形对象的指针
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void draw_shape(Shape *shape){
|
|
|
|
|
shapes.insert(shapes.end(),shape);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 09:32:18 +00:00
|
|
|
|
/**
|
|
|
|
|
在窗体中添加控件对象
|
|
|
|
|
|
|
|
|
|
@param name 控件名
|
|
|
|
|
@param view 指向目标控件对象的指针
|
|
|
|
|
*/
|
2018-12-25 13:41:30 +00:00
|
|
|
|
void add_menu(string name, View2D *view){
|
|
|
|
|
menus.insert({name,view});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
#endif /* graphs_h */
|