zoukankan      html  css  js  c++  java
  • 简单的图元

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

     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 << "
    Enter the integer value corresponding to
    "
     80  *        << "one of the following curve names.
    "
     81  *        << "Press any other key to exit.
    "
     82  *        << "
    1-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

  • 相关阅读:
    python之 网络编程、异常处理、面向对象
    python sys模块、mongodb操作、接口开发、程序分目录、压缩解压文件
    linux安装redis完整步骤
    python之:map、filter、random、datatime,写日志,发邮件,操作mysql、redis,MD5加密,写excel
    函数、模块
    python json处理、集合操作、函数定义、文件读写
    python字典、list、字符串常见处理方法、元组、文件读写
    学习笔记
    接口测试笔记
    【04】Cent OS 7 中部署JDK + Tomcat 环境
  • 原文地址:https://www.cnblogs.com/clairvoyant/p/5565588.html
Copyright © 2011-2022 走看看