zoukankan      html  css  js  c++  java
  • 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;    
    }  

    饼图:




  • 相关阅读:
    C#---将数据库数据转换为json格式
    ASP.NET ---根据值让树中某一节点选中
    SQL---查询树中某个节点及其所有子节点
    CSS---相对定位笔记
    CSS---绝对定位笔记
    滑雪
    Self Numbers
    Lotto
    Parencodings
    Robot Motion
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9411910.html
Copyright © 2011-2022 走看看