Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <gl/glut.h>
  4. #include <cmath>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. GLUnurbsObj* nobj;
  10. GLfloat ctlarray[][3] = {
  11. { -0.8, 0.8 },
  12. { -0.6, -0.7 },
  13. { 0.0, 0.5 },
  14. { 0.7, 0.2 }
  15. };
  16.  
  17. GLfloat bp[][4][2] = {
  18. { { 0.0,0.0 },{ -0.5,-0.95 },{ 0.0,-0.95 },{ 0.5,-0.95 } } ,
  19. { { -0.95,-0.5 },{ -0.5,-0.5 },{ 0.0,-0.5 },{ 0.5,-0.5 } },
  20. { { 0.95,0.0 },{ -0.5,0.0 },{ 0.0,0.0 },{ 0.5,0.0 } },
  21. { { -0.95,0.5 },{ -0.5,0.5 },{ 0.0,0.5 },{ 0.0,0.0 } }
  22. };
  23.  
  24. int fact(int n) {
  25. int val = 1;
  26. for (int i = 2; i < n + 1; i++) {
  27. val = val * i;
  28. }
  29. return val;
  30. }
  31.  
  32. int C(int n, int k) {
  33. return fact(n) / (fact(k) * fact(n - k));
  34. }
  35.  
  36. float BezePoly(int n, int i, float t) {
  37. int c = C(n, i);
  38. return c * pow(t, i) * pow(1 - t, n - i);
  39. }
  40.  
  41. float BezePoly3d(int n, int m, int i, int j, float u, float w) {
  42. int c1 = C(n, i);
  43. int c2 = C(m, j);
  44. return c1 * pow(u, i) * pow(1 - u, n - i) * c2 * pow(w, j) * pow(1 - w, m - j);
  45. }
  46.  
  47. void Display()
  48. {
  49. glClear(GL_COLOR_BUFFER_BIT);
  50. glPointSize(3.0);
  51. glColor3f(0.0, 1.0, 0.0);
  52. //int n = 3;
  53. /*glBegin(GL_POINTS);
  54. for (float t = 0.0; t < 1.0; t = t + 0.01) {
  55. GLfloat x = 0.0;
  56. GLfloat y = 0.0;
  57. for (int i = 0; i <= n; i++) {
  58. x = x + BezePoly(n, i, t) * ctlarray[i][0];
  59. y = y + BezePoly(n, i, t) * ctlarray[i][1];
  60. }
  61. cout << x << y << endl;
  62. glVertex3f(x, y, 0.5);
  63. }
  64. glEnd();
  65. glFlush();*/
  66.  
  67. int n = 3;
  68. int m = 3;
  69. glBegin(GL_POINTS);
  70. for (int i = 0; i <= n; i++) {
  71. for (int j = 0; j <= n; j++) {
  72. glVertex3f(bp[i][j][0], bp[i][j][1], 0);
  73. }
  74. }
  75.  
  76. for (float u = 0; u <= 1; u += 0.01) {
  77. for (float w = 0; w <= 1; w += 0.01) {
  78. GLfloat x = 0.0;
  79. GLfloat y = 0.0;
  80. for (int i = 0; i <= n; i++) {
  81. for (int j = 0; j <= m; j++) {
  82. x += bp[i][j][0] * BezePoly3d(n, m, i, j, u, w);
  83. y += bp[i][j][1] * BezePoly3d(n, m, i, j, u, w);
  84. }
  85. }
  86. glVertex3f(x, y, 0);
  87. }
  88. }
  89. glEnd();
  90. glFlush();
  91. }
  92.  
  93.  
  94. void main()
  95. {
  96. glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
  97. glutInitWindowSize(960, 960);
  98. glutInitWindowPosition(100, 100);
  99. glutCreateWindow(" ");
  100. glutDisplayFunc(Display);
  101. glutMainLoop();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement