• 【无标题】


    第四版 计算机图形学 中例子 代码有点瑕疵,见下图,本道长保证这个程序没有运行过。

    可运行代码如下。

    1. #include "stdafx.h"
    2. #include
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. struct screenPt
    8. {
    9. GLint x;
    10. GLint y;
    11. };
    12. typedef enum {
    13. limacon = 1,
    14. cardioid,
    15. threeLeaf,
    16. fourLeaf,
    17. spiral
    18. } curveName;
    19. GLsizei winWidth = 600, winHeight = 500;
    20. void init(void) {
    21. glClearColor(1.0,1.0,1.0,1.0);
    22. glMatrixMode(GL_PROJECTION);
    23. gluOrtho2D(0.0,200.0,0.0,150.0);
    24. }
    25. void lineSegment(screenPt pt1,screenPt pt2) {
    26. glBegin(GL_LINES);
    27. glVertex2i(pt1.x,pt1.y);
    28. glVertex2i(pt2.x,pt2.y);
    29. glEnd();
    30. }
    31. void drawCurve(GLint curveNum) {
    32. const GLdouble twoPi = 6.283185;
    33. const GLint a = 175, b = 60;
    34. GLfloat r, theta, dtheta = 1.0 / float(a);
    35. GLint x0 = 200, y0 = 250;
    36. screenPt curvePt[2];
    37. glColor3f(0.0,0.0,0.0);
    38. curvePt[0].x = x0;
    39. curvePt[0].y = y0;
    40. switch (curveNum) {
    41. case limacon: curvePt[0].x += a + b; break;
    42. case cardioid:curvePt[0].x += a + a; break;
    43. case threeLeaf: curvePt[0].x += a; break;
    44. case fourLeaf: curvePt[0].x += a; break;
    45. case spiral: break;
    46. default: break;
    47. }
    48. theta = dtheta;
    49. while (theta < twoPi) {
    50. switch (curveNum)
    51. {
    52. case limacon:
    53. r = a * cos(theta) + b; break;
    54. case cardioid:
    55. r = a * (1 + cos(theta)); break;
    56. case threeLeaf:
    57. r = a * cos(3 * theta); break;
    58. case fourLeaf:
    59. r = a * cos(2 * theta); break;
    60. case spiral:
    61. r = (a / 4.0) * theta; break;
    62. default: break;
    63. }
    64. curvePt[1].x = x0 + r * cos(theta);
    65. curvePt[1].y = y0 + r * sin(theta);
    66. lineSegment(curvePt[0],curvePt[1]);
    67. curvePt[0].x = curvePt[1].x;
    68. curvePt[0].y = curvePt[1].y;
    69. theta += dtheta;
    70. }
    71. }
    72. void displayFcn(void) {
    73. GLint curveNum;
    74. glClear(GL_COLOR_BUFFER_BIT);
    75. cout << "\nEnter the integr value corresponding to \n";
    76. cout << "one of the folloing curve names .\n";
    77. cout << "Press any other key to exit. \n";
    78. cout << "\n1-limacon. 2-cardioid,3-threeLeaf,4-fourLea,5-spira1:";
    79. cin >> curveNum;
    80. if (curveNum == 1 || curveNum == 2 || curveNum == 3 || curveNum == 4 || curveNum == 5) {
    81. drawCurve(curveNum);
    82. }
    83. else
    84. {
    85. exit(0);
    86. }
    87. glFlush();
    88. }
    89. void winReshapFcn(GLint newWidth, GLint newHeight) {
    90. glMatrixMode(GL_PROJECTION);
    91. glLoadIdentity();
    92. gluOrtho2D(0.0,(GLdouble) newWidth,0.0,(GLdouble) newHeight);
    93. glClear(GL_COLOR_BUFFER_BIT);
    94. }
    95. void main(int argc,char ** argv) {
    96. glutInit(&argc, argv);
    97. glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB);
    98. glutInitWindowPosition(100,100);
    99. glutInitWindowSize(winWidth,winHeight);
    100. glutCreateWindow("Draw Curves");
    101. init();
    102. glutDisplayFunc(displayFcn);
    103. glutReshapeFunc(winReshapFcn);
    104. glutMainLoop();
    105. }

  • 相关阅读:
    【计算机毕业设计】小型OA系统设计与实现Springboot
    2022.6.29-----leetcode.535
    Android ijkplayer播放rtsp直播流
    [附源码]Python计算机毕业设计Django动物保护协会网站
    【数学思维】论文阅读中的逆变换定理
    arduino32 esp32路舵机驱动板(开源可自制,附程序和固件以及烧录方法)
    Java:实现快速傅里叶变换算法(附完整源码)
    echarts实现横向和纵向滚动条(使用dataZoom)
    (五)C++中的排序函数性能比较
    1-前端基本知识-CSS
  • 原文地址:https://blog.csdn.net/zhanglixin999/article/details/132790207