一、利用OpenGL绘制形如下图的折线加柱状图,数据和颜色自己设定,要有文字的显示。
二、 利用OpenGL绘制形如下图的饼状图,数据和颜色自己设定,要有文字的显示。
一、直方图、折线图
#include
#include
using namespace std;
Glubyte label[18]={'J','a','n','F','e','b',
'M','a','r','A','p','r','M','a','y','J','u','n'};
GLint dataValue[6]={195,215,335,210,365,270};
void init (void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,600.0,0.0,500.0);
}
void lineGraph_barChart (void)
{
GLint month,k;
glClear(GL_COLOR_BUFFER_BIT); //清除clear display window
glLineWidth(1.3);
glColor3f(0.0,0.5,0.2);
//---------------------------------------------坐标轴
glBegin(GL_LINES);
glVertex2i(60,30); //y轴
glVertex2i(60,450);
glVertex2i(60,150); //100
glVertex2i(55,150);
glVertex2i(60,250); //200
glVertex2i(55,250);
glVertex2i(60,350); //300
glVertex2i(55,350);
glVertex2i(30,50); //x轴
glVertex2i(530,50);
glEnd();
//----------------------------------------
glPointSize(4.5);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POINTS);
for(k=0;k<6;k++)
{
glVertex2i(120+k*70,dataValue[k]);
}
glEnd();
//---------------------
glLineWidth(2.0);
glColor3f(0.0,0.3,0.7);
glBegin(GL_LINE_STRIP);
for(k=0;k<6;k++)
{
glVertex2i(120+k*70,dataValue[k]);
}
glEnd();
//---------------------
for(k=0;k<6;k++)
{
glColor3f(0.0,0.3,0.7);
glBegin(GL_POLYGON);
glVertex2i(110+k*70,50);
glVertex2i(130+k*70,50);
glVertex2i(130+k*70,dataValue[k]-40);
glVertex2i(110+k*70,dataValue[k]-40);
glEnd();
}
//----------------------------------
glColor3f(0.0,0.0,0.0);
GLint xRaster=110;
for(month=0;month<6;month++)
{
glRasterPos2i(xRaster,35);
for(k=3*month;k<3*month+3;k++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,label[k]);
}
xRaster+=70;
}
//--------------
glColor3f(0.0,0.0,0.0);
GLint yRaster=145;
GLubyte A[9]={'1','0','0','2','0','0','3','0','0'};
for(GLint i=0;i<3;i++)
{
glRasterPos2i(30,yRaster);
for(GLint j=i*3;j<3*i+3;j++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,A[j]);
yRaster+=100;
}
//---------------------------------------------
glRasterPos2i(45,35);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,'O');
for(k=0;k<6;k++)
{
glColor3f(0.0,0.0,0.0);
glLineWidth(2.0);
glBegin(GL_LINE_STRIP);
glVertex2i(110+k*70,50);
glVertex2i(110+k*70,dataValue[k]-40);
glVertex2i(130+k*70,dataValue[k]-40);
glVertex2i(130+k*70,50);
glEnd();
}
glFlush();
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(600,500);
glutCreateWindow("实验一:直方图、折线图");
init();
glutDisplayFunc(lineGraph_barChart);
glutMainLoop();
}
//------------------------------------------------------------------
#include
#include
#include
#include
using namespace std;
const GLdouble twoPi = 6.283185;
GLubyte label[24]={'J','a','n','F','e','b','M',
'a','r','A','p','r','M','a','y','J','u','n'};
GLint R[18]={'3','0','%','1','5','%','1',
'0','%','1','0','%','1','5','%','2','0','%'};
GLsizei winWidth = 600 , winHeight = 500;
GLfloat dataValue[6]={30.0,15.0,10.0,10.0,15.0,20.0};
class scrPt
{
public:
GLint x,y;
};
void init (void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,600.0,0.0,500.0);
}
void pieChart (void)
{
GLint k,n = 6;
scrPt circCtr;
scrPt piePt[6];
GLdouble A[6];
GLint r = winWidth/4;
GLdouble sliceAngle, previousSliceangle = 0.36;
GLfloat dataSum = 0.0;
circCtr.x = winWidth/2;
circCtr.y = winHeight/2;
GLdouble i;
//--------------------------------------------------
for(k=0;k<n;k++)
{
dataSum+=dataValue[k];
}
GLdouble B[7];
for (k=0;k<n;k++)
{
B[k]=sliceAngle = twoPi*dataValue[k]/dataSum + previousSliceangle;
piePt[k].x = circCtr.x - r * cos(sliceAngle);
piePt[k].y = circCtr.y + r * sin(sliceAngle);
A[k] = twoPi*dataValue[k]/dataSum;
// B[k] = sliceAngle;
B[6] = B[0];
previousSliceangle = sliceAngle;
}
//----------------------------------------------------------------扇形
glColor3f(0.482,0.227,0.69);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(circCtr.x,circCtr.y);
for(i=0;i<1000000;i++)
{
GLdouble t=i*B[0]/1000000;
glVertex2i(circCtr.x - r * cos(t),circCtr.y + r * sin(t));
}
glEnd();
for(k=1;k<6;k++)
{
GLfloat a,b,c;
if(k==1)
{
a=0.917;
b=0.0;
c=0.0;
}
if(k==2)
{
a=0.067;
b=0.627;
c=0.850;
}
if(k==3)
{
a=0.592;
b=0.592;
c=0.592;
}
if(k==4)
{
a=0.4;
b=0.929;
c=0.2549;
}
if(k==5)
{
a=0.968;
b=0.968;
c=0.215;
}
glColor3f(a,b,c);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(circCtr.x,circCtr.y);
for(i=0;i<1000000;i++)
{
GLdouble t=B[k-1]+i*A[k]/1000000;
glVertex2i(circCtr.x - r * cos(t),circCtr.y + r * sin(t));
}
glEnd();
}
//-------------------------------------------------园
glColor3f(0.0,0.0,0.0);
glLineWidth(3.0);
glBegin(GL_LINE_LOOP);
for(i=0;i<1000000;i++)
{
GLdouble t=i*twoPi/1000000;
glVertex2f(circCtr.x+r * cos(t),circCtr.y+r * sin(t));
}
glEnd();
//----------------------------------
for(k=0;k<n;k++)
{
glLineWidth(2.0);
glBegin(GL_LINES);
glVertex2i(circCtr.x,circCtr.y);
glVertex2i(piePt[k].x,piePt[k].y);
glEnd();
}
//---------------------------------------------
//----输出字符
scrPt piePt1[6];
piePt1[0].x = circCtr.x + (2*r/3)*cos((B[5]+B[0])/2);
piePt1[0].y = circCtr.y - (2*r/3)*sin((B[5]+B[0])/2);
for(k=1;k<6;k++)
{
piePt1[k].x = circCtr.x - (2*r/3)*cos((B[k-1]+B[k])/2);
piePt1[k].y = circCtr.y + (2*r/3)*sin((B[k-1]+B[k])/2);
}
glColor3f(0.0,0.0,0.0);
for(GLint t=0;t<6;t++)
{
glRasterPos2i(piePt1[t].x,piePt1[t].y);
for(GLint j=t*3;j<3*t+3;j++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,label[j]);
glRasterPos2i(piePt1[t].x,piePt1[t].y-20);
for(j=t*3;j<3*t+3;j++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,R[j]);
}
}
void displayFcn(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
pieChart();
glFlush();
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(winWidth,winHeight);
glutCreateWindow("实验一:扇形图");
init();
glutDisplayFunc(displayFcn);
glutMainLoop();
}
//-------------------------------------------------------