1. 程式人生 > >OpenGL(十七) 繪製折線圖、柱狀圖、餅圖

OpenGL(十七) 繪製折線圖、柱狀圖、餅圖

一、繪製折線圖

glutBitmapCharacter(GLUT_BITMAP_8_BY_13,label[j])函式可以繪製GLUT點陣圖字元,第一個引數是GLUT中指定的特定字形集,第二個引數是要寫入的單個字元;

#include <glut.h> 

GLsizei windowWidth=600,windowHeight=600;
GLubyte label[36]={'O','n','e',  'T','w','o', 'T','h','r',  'F','o','u',
	'F','i','v',  'S','i','x',  'S','e','v',  'E','i','g',
	'N','i','e',  'T','e','n',  'E','l','e',  'T','w','e',};
GLint dataValue[12]={452,368,214,543,328,193,322,436,257,268,473,467};

void Init()
{
	glClearColor(1,1,1,1);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0,windowWidth,0,windowHeight);
}

void LineImage()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0,0,1);
	glLineWidth(2);
	//繪製折線圖
	glBegin(GL_LINE_STRIP);
	{
		for(int i=0;i<12;i++)
		{
			glVertex2i(20+i*50,dataValue[i]);
		}
	}
	glEnd();

	//繪製標記點
	glColor3f(1,0,0);
	for(int i=0;i<12;i++)
	{
		glRasterPos2i(15+i*50,dataValue[i]-5);
		glutBitmapCharacter(GLUT_BITMAP_8_BY_13,'*');
	}

	glColor3f(0,0,0);
	//繪製序列
	for(int i=0;i<12;i++)
	{
		glRasterPos2i(15+i*50,150);
		for(int j=i*3;j<i*3+3;j++)
		{
			glutBitmapCharacter(GLUT_BITMAP_8_BY_13,label[j]);
		}
	}
	glFlush();
}

void ReshapFunction(GLint x,GLint y)
{
	glClear(GL_COLOR_BUFFER_BIT);
}


int main(int argc, char *argv[])    
{    
	glutInit(&argc, argv);   //初始化GLUT
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
	glutInitWindowPosition(500, 200);    
	glutInitWindowSize(windowWidth, windowHeight);    
	glutCreateWindow("OpenGL");
	Init();
	glutDisplayFunc(&LineImage);   //回撥函式 
	glutReshapeFunc(ReshapFunction);
	glutMainLoop();    //持續顯示,當視窗改變會重新繪製圖形
	return 0;    
}  

折線圖:

二、柱狀圖

使用函式glRecti (GLint x1, GLint y1, GLint x2, GLint y2)可以方便的繪製一個填充的矩形區域,第一二個引數是矩形區域的左下角起點,第二三個引數是右上角頂點。

#include <glut.h> 

GLsizei windowWidth=600,windowHeight=600;
GLubyte label[36]={'O','n','e',  'T','w','o', 'T','h','r',  'F','o','u',
	'F','i','v',  'S','i','x',  'S','e','v',  'E','i','g',
	'N','i','e',  'T','e','n',  'E','l','e',  'T','w','e',};
GLint dataValue[12]={452,368,214,543,328,193,322,436,257,268,473,467};

void Init()
{
	glClearColor(1,1,1,1);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0,windowWidth,0,windowHeight);
}

void LineImage()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0,0,1);

	for(int i=0;i<12;i++)
	{
		glRecti(20+i*50,170,30+i*50,dataValue[i]);
	}

	glColor3f(0,0,0);
	//繪製序列
	for(int i=0;i<12;i++)
	{
		glRasterPos2i(15+i*50,150);
		for(int j=i*3;j<i*3+3;j++)
		{
			glutBitmapCharacter(GLUT_BITMAP_8_BY_13,label[j]);
		}
	}
	glFlush();
}

int main(int argc, char *argv[])    
{    
	glutInit(&argc, argv);   //初始化GLUT
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
	glutInitWindowPosition(500, 200);    
	glutInitWindowSize(windowWidth, windowHeight);    
	glutCreateWindow("OpenGL");
	Init();
	glutDisplayFunc(&LineImage);   //回撥函式 	
	glutMainLoop();    
	return 0;    
}  

柱狀圖:


三、餅圖

#include <glut.h> 
#include <stdlib.h>
#include <math.h>

GLsizei windowWidth=600,windowHeight=600;
GLint dataValue[12]={452,368,214,543,328,193,322,436,257,268,473,467};

void Init()
{
	glClearColor(1,1,1,1);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0,windowWidth,0,windowHeight);
}

void CircleImage()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0,0,1);	
	glPointSize(2);
	glBegin(GL_POINTS);
	{
		for(float i=0.0f;i<2*3.1415926f;i+=0.001)
		{
			glVertex2f((windowWidth/3)*cos(i)+windowWidth/2,(windowHeight/3)*sin(i)+windowHeight/2);
		}
	}
	glEnd();	
	GLint totalNum=0;
	for(int i=0;i<12;i++)
	{
		totalNum+=dataValue[i];
	}

	GLfloat angleData[12];
	for(int i=0;i<12;i++)
	{
		angleData[i]=2*3.1415926f*(GLfloat)dataValue[i]/totalNum;

	}
	for(int j=1;j<12;j++)
	{
		angleData[j]+=angleData[j-1];
	}

	glColor3f(1,0,0);
	glLineWidth(2);

	glBegin(GL_LINES);
	{
		for(int i=0;i<12;i++)
		{
			glVertex2f(windowWidth/2,windowHeight/2);
			glVertex2f((windowWidth/3)*cos(angleData[i])+windowWidth/2,(windowHeight/3)*sin(angleData[i])+windowHeight/2);			
		}
		glEnd();
		glFlush();
	}
}

int main(int argc, char *argv[])    
{    
	glutInit(&argc, argv);   //初始化GLUT
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
	glutInitWindowPosition(500, 200);    
	glutInitWindowSize(windowWidth, windowHeight);    
	glutCreateWindow("OpenGL");
	Init();
	glutDisplayFunc(&CircleImage);   //回撥函式 	
	glutMainLoop();    
	return 0;    
}  

餅圖: