//
// mainfunc.cpp
// XcodeGlutDemo
//
// Created by Bertie on 12/1/15.
// Copyright (c) 2015 Bertie. All rights reserved.
//
#ifndef XcodeGlutDemo_graphicShow_h
#define XcodeGlutDemo_graphicShow_h
/* recursive subdivision of triangle to form Sierpinski gasket */
/* number of recursive steps given on command line */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic ignored "-Wunused-variable"
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <iostream>
#include <cmath>
#include <Carbon/Carbon.h>
#include "airplanee.h"
using namespace std;
/* initial triangle */
const int COL = 6;
const int ROW = 20;
GLfloat v[5][2]={{0.0,1.95},{-0.7, 1.7}, {-0.7, -1.9}, {0.7, -1.9},{0.7,1.7}};
GLfloat s[4][2];
GLfloat seatColor[numofR][numofC-1];
bool isstart = true;
bool isleftdown = false;
GLint n = 1;
GLint Shape = 1;
GLint xlable = 0;
GLint ylable = 0;
GLint xmove = 0;
GLint ymove = 0;
GLfloat xmovef = 0.0;
GLfloat ymovef = 0.0;
void triangle( GLfloat *a, GLfloat *b, GLfloat *c)
/* specify one triangle */
{
glColor3f(0.5, 0.5, 0.5);
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
}
void line_frame( GLfloat *a, GLfloat *b, GLfloat *c,GLfloat *d,GLfloat *e)
/* specify one triangle */
{
glLineWidth(3.5f);
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(b);
glVertex2fv(c);
glVertex2fv(c);
glVertex2fv(d);
glVertex2fv(d);
glVertex2fv(e);
glVertex2fv(e);
glVertex2fv(a);
glEnd();
}
void displaySeat(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d,GLfloat col)
{
glColor3f(col, 0.5,0.5 );
glBegin(GL_POLYGON);
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
glVertex2fv(d);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
//display框架
line_frame(v[0],v[1],v[2],v[3],v[4]);
//display左半边
float unit_row = (1.8 + 1.8)/(float)(ROW+2);
float unit_col = 0.15;
float width = 0.1;
float heith = 0.2;
float col = 0;
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL/2;j++)
{
s[0][0] = 0.0 - width - j*unit_col - 0.02;
s[1][0] = s[0][0];
s[2][0] = 0.0 - width - (j+1)*unit_col + 0.02;
s[3][0] = s[2][0];
s[0][1] = 1.7 - heith - i*unit_row - 0.02;
s[3][1] = s[0][1];
s[1][1] = 1.7 - heith - (i+1)*unit_row + 0.02;
s[2][1] = s[1][1];
col = seatColor[i][j];
displaySeat(s[0],s[1],s[2],s[3],col);
}
}
//display左半边
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL/2;j++)
{
s[0][0] = 0.0 + width + j*unit_col + 0.02;
s[1][0] = s[0][0];
s[2][0] = 0.0 + width + (j+1)*unit_col - 0.02;
s[3][0] = s[2][0];
s[0][1] = 1.7 - heith - i*unit_row - 0.02;
s[3][1] = s[0][1];
s[1][1] = 1.7 - heith - (i+1)*unit_row + 0.02;
s[2][1] = s[1][1];
col = seatColor[i][j+3];
displaySeat(s[0],s[1],s[2],s[3],col);
}
}
glFlush();
}
void winReshapeFun(int newWidth,int newHeight)
{
GLfloat aspectRatio;
isstart = true;
// 防止被0所除
if (0 == newHeight){
newHeight = 1;
}
// 设置视口为窗口的大小
glViewport(0, 0, (GLsizei)newWidth, (GLsizei)newHeight);
// 选择投影矩阵,并重置坐标系统
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(-2.0,2.0, -2.0, 2.0);
// 计算窗口的纵横比(像素比)
aspectRatio = (GLfloat) newWidth / (GLfloat) newHeight;
// 定义裁剪区域(根据窗口的纵横比,并使用正投影)
if (newWidth <=newHeight) {// 宽 < 高
glOrtho(-2.0, 2.0, -2.0 /aspectRatio, 2.0 / aspectRatio, 1.0, -1.0);
} else {// 宽 > 高
glOrtho(-2.0 * aspectRatio, 2.0 *aspectRatio, -2.0, 2.0, 1.0, -1.0);
}
// 选择模型视图矩阵,并重置坐标系统
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glClearColor (1.0, 1.0, 1.0, 1.0);
glColor3f(0.0,0.0,0.0);
}
int main(int argc, char **argv)
{
double ave_time = 0;
double sum_time = 0;
int method = 10;
cout<<"飞机结构: 6col 20row 120 满员"<<endl<<endl;;
ave_time = airplane(method);
cout<<"策略 "<<method<<" 用时:"<<ave_time<<endl;
for(method = 1;method<16;method++)
{
ave_time = 0;
sum_time = 0;
// ave_time += airplane(method);
for(int i=0;i<100;i++)
{
// sum_time += airplane(method);
}
// ave_time = sum_time/100;
cout<<" "<<" 用时:"<<ave_time<<endl;
}
/*
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("策略15");
myinit();
float max = airplane(15);
for(int i = 0;i<numofR;i++)
{
for(int j=0;j<numofC;j++)
{
if(j<3)
seatColor[i][j] = 1.0 - 1.0*((float)temp[j][i+numofH]/(float)max);
else
seatColor[i][j-1] = 1.0 - 1.0*(temp[j][i+numofH]/max);
}
}
//相应键盘
//glutKeyboardFunc(keyFunction);
//弹出菜单
// int shapeMenu=glutCreateMenu(theShape);
// glutAddMenuEntry("画三角形",1);
// glutAddMenuEntry("画线段",2);
// glutAttachMenu(GLUT_RIGHT_BUTTON);
//拖动图形
//glutMouseFunc(mouseFunction);
//glutPassiveMotionFunc(mouseMove);
//绘出图形
glutDisplayFunc(display);
//保证视图比例不变
glutReshapeFunc(winReshapeFun);
glutMainLoop();
*/
}
#pragma clang diagnostic pop
#endif