简单的图元

一些用作练习的非常简单的图元。

 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 }
circle.h
  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 }
Main.cpp
  1 #include <gl/glut.h>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <iostream>
  5 
  6 class screenPt {
  7 public:
  8     GLint x, y;
  9 };
 10 
 11 typedef enum { limacon = 1, cardioid, threeLeaf, fourLeaf, spiral } curveName;
 12 GLsizei winWidth = 600, winHeight = 500;
 13 
 14 void init(void)
 15 {
 16     glClearColor(0.5, 0.5, 0.5, 1.0);
 17 
 18     glMatrixMode(GL_PROJECTION);
 19     gluOrtho2D(0.0, 200.0, 0.0, 150.0);
 20 }
 21 
 22 void lineSegment(screenPt pt1, screenPt pt2)
 23 {
 24     glBegin(GL_LINES);
 25     glVertex2i(pt1.x, pt1.y);
 26     glVertex2i(pt2.x, pt2.y);
 27     glEnd();
 28 }
 29 
 30 void drawCurve(GLint curveNum)
 31 {
 32     const GLdouble twoPi = 6.283185;
 33     const GLint a = 175, b = 60;
 34 
 35     GLfloat r, theta, dtheta = 1.0 / static_cast<float>(a);
 36     GLint x0 = 200, y0 = 250;
 37     screenPt curvePt[2];
 38 
 39     glColor3f(0.0, 0.0, 0.0);
 40 
 41     curvePt[0].x = x0;
 42     curvePt[0].y = y0;
 43 
 44     switch (curveNum) {
 45     case limacon: curvePt[0].x += a + b; break;
 46     case cardioid: curvePt[0].x += a + a; break;
 47     case threeLeaf: curvePt[0].x += a; break;
 48     case fourLeaf: curvePt[0].x += a; break;
 49     case spiral: break;
 50     default: break;
 51     }
 52 
 53     theta = dtheta;
 54     while (theta < twoPi) {
 55         switch (curveNum) {
 56         case limacon: r = a * cos(theta) + b; break;
 57         case cardioid: r = a * (1 + cos(theta)); break;
 58         case threeLeaf: r = a * cos(3 * theta); break;
 59         case fourLeaf: r = a * cos(2 * theta); break;
 60         case spiral: r = (a / 4.0) * theta; break;
 61         default: break;
 62         }
 63         curvePt[1].x = x0 + r * cos(theta);
 64         curvePt[1].y = y0 + r * sin(theta);
 65         lineSegment(curvePt[0], curvePt[1]);
 66 
 67         curvePt[0].x = curvePt[1].x;
 68         curvePt[0].y = curvePt[1].y;
 69         theta += dtheta;
 70     }
 71 }
 72 
 73 void displayFcn(void)
 74 {
 75     GLint curveNum = 1;
 76 
 77     glClear(GL_COLOR_BUFFER_BIT);
 78 /*
 79  *    std::cout << "\nEnter the integer value corresponding to\n"
 80  *        << "one of the following curve names.\n"
 81  *        << "Press any other key to exit.\n"
 82  *        << "\n1-limacon, 2-cardioid, 3-threeLeaf, 4-fourLeaf, 5-spiral: ";
 83  */
 84 
 85     while (curveNum < 6 && curveNum > 0) {
 86         static int count = 0;
 87         if (count == 0) {
 88             glViewport(0, 250, 200, 200);
 89             count++;
 90         }
 91         else if (count == 1) {
 92             glViewport(175, 250, 200, 200);
 93             count++;
 94         }
 95         else if (count == 2) {
 96             glViewport(400, 250, 200, 200);
 97             count++;
 98         }
 99         else if (count == 3) {
100             glViewport(40, 100, 200, 200);
101             count++;
102         }
103         else if (count == 4) {
104             glViewport(205, 100, 200, 200);
105             count++;
106         }
107         if (curveNum == 1 || curveNum == 2 || curveNum == 3 || curveNum == 4 || curveNum == 5)
108             drawCurve(curveNum);
109         curveNum++;
110         glFlush();
111 //        glClear(GL_COLOR_BUFFER_BIT);
112     }
113 }
114 
115 void winReshapeFcn(GLint newWidth, GLint newHeight)
116 {
117     glMatrixMode(GL_PROJECTION);
118     glLoadIdentity();
119     gluOrtho2D(0.0, (GLdouble)newWidth, 0.0, (GLdouble)newHeight);
120 
121     glClear(GL_COLOR_BUFFER_BIT);
122 }
123 
124 int main(int argc, char* argv[])
125 {
126     glutInit(&argc, argv);
127     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
128     glutInitWindowPosition(100, 100);
129 
130     glutInitWindowSize(winWidth, winHeight);
131     glutCreateWindow("Draw Curves");
132     
133     init();
134     glutDisplayFunc(displayFcn);
135     glutReshapeFunc(winReshapeFcn);
136     
137     glutMainLoop();
138     return 0;
139 }
metafiles.cpp

转载于:https://www.cnblogs.com/clairvoyant/p/5565588.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值