标签:
一些用作练习的非常简单的图元。
1 #pragma once 2 #include <gl/glut.h> 3 4 class ScreenPt { 5 private: 6 GLint x, y; 7 8 public: 9 ScreenPt() { 10 x = y = 0; 11 } 12 void setCoords(GLint xCoordValue, GLint yCoordValue) { 13 x = xCoordValue; 14 y = yCoordValue; 15 } 16 GLint getx() const { 17 return x; 18 } 19 GLint gety() const { 20 return y; 21 } 22 void incrementx() { 23 ++x; 24 } 25 void decrementy() { 26 --y; 27 } 28 }; 29 30 void setPixel(GLint xCoord, GLint yCoord) 31 { 32 glBegin(GL_POINTS); 33 glVertex2i(xCoord, yCoord); 34 glEnd(); 35 } 36 37 void circlePlotPoints(GLint xc, GLint yc, ScreenPt circPt) 38 { 39 setPixel(xc + circPt.getx(), yc + circPt.gety()); 40 setPixel(xc - circPt.getx(), yc + circPt.gety()); 41 setPixel(xc + circPt.getx(), yc - circPt.gety()); 42 setPixel(xc - circPt.getx(), yc - circPt.gety()); 43 setPixel(xc + circPt.gety(), yc + circPt.getx()); 44 setPixel(xc - circPt.gety(), yc + circPt.getx()); 45 setPixel(xc + circPt.gety(), yc - circPt.getx()); 46 setPixel(xc - circPt.gety(), yc - circPt.getx()); 47 } 48 49 void circleMidpoint(GLint xc, GLint yc, GLint radius) 50 { 51 ScreenPt circPt; 52 53 GLint p = 1 - radius; //Initial value for midpoint parameter 54 55 circPt.setCoords(0, radius); //Set coords for top point of circle. 56 // Plot the initial point in each circle quadrant 57 circlePlotPoints(xc, yc, circPt); 58 // Calculate next point and polt in each octant 59 while (circPt.getx() < circPt.gety()) { 60 circPt.incrementx(); 61 if (p < 0) { 62 p += 2 * circPt.getx() + 1; 63 } 64 else { 65 circPt.decrementy(); 66 p += 2 * (circPt.getx() - circPt.gety()) + 1; 67 } 68 circlePlotPoints(xc, yc, circPt); 69 } 70 }
1 #include <GL/glut.h> 2 #include <cstdlib> 3 #include <cmath> 4 #include "circle.h" 5 #define TWO_PI 6.2831853 6 7 class screenPt { 8 public: 9 GLint x, y; 10 }; 11 12 GLsizei winWidth = 600, winHeight = 500; 13 GLint xRaster = 25, yRaster = 150; 14 15 GLubyte label[36] = {‘J‘,‘a‘,‘n‘, ‘F‘,‘e‘,‘b‘, ‘M‘,‘a‘,‘r‘, 16 ‘A‘,‘p‘,‘r‘, ‘M‘,‘a‘,‘y‘, ‘J‘,‘u‘,‘n‘, 17 ‘J‘,‘u‘,‘l‘, ‘A‘,‘u‘,‘g‘, ‘S‘,‘e‘,‘p‘, 18 ‘O‘,‘c‘,‘t‘, ‘N‘,‘o‘,‘v‘, ‘D‘,‘e‘,‘c‘}; 19 GLint dataValue[12] = {420, 342, 324, 310, 262, 185, 20 190, 196, 217, 240, 312, 438}; 21 GLint myData[36] = {52, 50, 48, 51, 52, 50, 51, 50, 52, 51, 49, 48, 22 50, 54, 50, 49, 56, 53, 49, 57, 48, 49, 57, 54, 23 50, 49, 55, 50, 52, 48, 51, 49, 50, 52, 51, 56}; 24 25 void init(void) 26 { 27 glClearColor(0.5, 0.5, 0.5, 0.0); 28 glMatrixMode(GL_PROJECTION); 29 gluOrtho2D(0.0, 600.0, 0.0, 500.0); 30 } 31 32 void lineGraph(void) //折线图 33 { 34 GLint month, k; 35 GLint x = 30; 36 37 glClear(GL_COLOR_BUFFER_BIT); 38 glColor3f(0.0, 0.0, 0.0); 39 glBegin(GL_LINE_STRIP); 40 for (k = 0; k < 12; ++k) { 41 glVertex2i(x + k * 50, dataValue[k]); 42 } 43 glEnd(); 44 xRaster = 20; 45 glColor3f(1.0, 0.0, 0.0); 46 for (k = 0; k < 12; ++k) { 47 glRasterPos2i(xRaster + k * 50, dataValue[k] - 4); 48 glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ‘*‘); 49 } 50 51 glColor3f(0.0, 0.0, 0.0); 52 xRaster = 20; 53 k = 0; 54 for (month = 0; month < 12; ++month) { 55 glRasterPos2i(xRaster, dataValue[month]+5); 56 for (k = 3 * month; k < 3 * month + 3; ++k) { 57 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, myData[k]); 58 } 59 xRaster += 50; 60 } 61 62 glColor3f(0.0, 0.0, 0.0); 63 xRaster = 20; 64 k = 0; 65 for (month = 0; month < 12; ++month) { 66 glRasterPos2i(xRaster, yRaster); 67 for (k = 3 * month; k < 3 * month+3; ++k) { 68 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, label[k]); 69 } 70 xRaster += 50; 71 } 72 glFlush(); 73 } 74 75 void barChart(void) // 柱状图 76 { 77 GLint month, k; 78 79 glClear(GL_COLOR_BUFFER_BIT); 80 glColor3f(1.0, 0.0, 0.0); 81 for (k = 0; k < 12; ++k) { 82 glRecti(20 + k * 50, 165, 40 + k * 50, dataValue[k]); 83 } 84 glColor3f(0.0, 0.0, 0.0); 85 xRaster = 20; 86 for (month = 0; month < 12; ++month) { 87 glRasterPos2i(xRaster, yRaster); 88 for (k = 3 * month; k < 3 * month + 3; ++k) { 89 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, label[k]); 90 } 91 xRaster += 50; 92 } 93 94 glColor3f(0.0, 0.0, 0.0); 95 xRaster = 20; 96 k = 0; 97 for (month = 0; month < 12; ++month) { 98 glRasterPos2i(xRaster, dataValue[month] + 5); 99 for (k = 3 * month; k < 3 * month + 3; ++k) { 100 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, myData[k]); 101 } 102 xRaster += 50; 103 } 104 105 glFlush(); 106 } 107 108 void pieChart(void) //饼状图 109 { 110 screenPt circCtr, piePt; 111 GLint radius = winWidth / 4; 112 113 GLdouble sliceAngle, previousSlicenAngle = 0.0; 114 115 GLint k, nSlicens = 12; 116 GLfloat dataValues[12] = { 10.0, 7.0, 13.0, 5.0, 13.0, 14.0, 3.0, 16.0, 5.0, 3.0, 17.0, 8.0 }; 117 118 GLfloat dataSum = 0.0; 119 120 circCtr.x = winWidth / 2; 121 circCtr.y = winHeight / 2; 122 circleMidpoint(circCtr.x, circCtr.y, radius); 123 124 for (k = 0; k < nSlicens; ++k) { 125 dataSum += dataValues[k]; 126 } 127 128 for (k = 0; k < nSlicens; ++k) { 129 sliceAngle = TWO_PI * dataValues[k] / dataSum + previousSlicenAngle; 130 piePt.x = circCtr.x + radius * cos(sliceAngle); 131 piePt.y = circCtr.y + radius * sin(sliceAngle); 132 glBegin(GL_LINES); 133 glVertex2i(circCtr.x, circCtr.y); 134 glVertex2i(piePt.x, piePt.y); 135 glEnd(); 136 previousSlicenAngle = sliceAngle; 137 } 138 glFlush(); 139 } 140 141 void winReshapeFcn(GLint newWidth, GLint newHeight) 142 { 143 glMatrixMode(GL_PROJECTION); 144 glLoadIdentity(); 145 gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight)); 146 147 glClear(GL_COLOR_BUFFER_BIT); 148 149 winWidth = newWidth; 150 winHeight = newHeight; 151 } 152 153 int main(int argc, char* argv[]) 154 { 155 glutInit(&argc, argv); 156 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 157 glutInitWindowPosition(100, 100); 158 159 glutInitWindowSize(winWidth, winHeight); 160 //*********************************** 161 glutCreateWindow("Line Chart Data Plot"); 162 163 init(); 164 glutDisplayFunc(lineGraph); 165 glutReshapeFunc(winReshapeFcn); 166 167 //*********************************** 168 169 glutCreateWindow("Bar Chart Data Plot"); 170 171 init(); 172 glutDisplayFunc(barChart); 173 glutReshapeFunc(winReshapeFcn); 174 175 //*********************************** 176 177 glutCreateWindow("Pie Chart Data Plot"); 178 179 init(); 180 glutDisplayFunc(pieChart); 181 glutReshapeFunc(winReshapeFcn); 182 183 glutMainLoop(); 184 return 0; 185 }
标签:
原文地址:http://www.cnblogs.com/clairvoyant/p/5565588.html