Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <GL/glut.h>
  2.  
  3. GLfloat ctlarray[][3] = { { -0.9, -0.5,0.0 },
  4. { -0.7, 0.5, 0.0 },
  5. { -0.4, -0.5, 0.0 },
  6. { -0.1, 0.5, 0.0 },
  7. { 0.2, -0.5, 0.0 },
  8. { 0.5, 0.5, 0.0 } };
  9.  
  10.  
  11. double N_i_k(int k, int i, GLfloat x[], double t) {
  12. if (k == 1)
  13. {
  14. if ((x[i] <= t) && (t <= x[i + 1]))
  15. return 1;
  16. else
  17. return 0;
  18. }
  19. else
  20. {
  21. double sum1, sum2;
  22. if (x[i + k -1] == x[i])
  23. sum1 = 0;
  24. else
  25. sum1 = ((t - x[i]) / (x[i + k - 1] - x[i]))*N_i_k(k - 1, i, x, t);
  26. if (x[i + k + 1] == x[i + 1])
  27. sum2 = 0;
  28. else
  29. sum2 = ((x[i + k ] - t) / (x[i + k ] - x[i + 1]))*N_i_k(k - 1, i + 1, x, t);
  30. return sum1 + sum2;
  31. }
  32. }
  33.  
  34. void init(void) {
  35. glClearColor(1, 1, 1, 1);
  36. }
  37.  
  38. void Display() {
  39. glClear(GL_COLOR_BUFFER_BIT);
  40. glLoadIdentity();
  41. int k = 4;
  42. int n = 6;
  43. GLfloat knot[] = { 0.0, 1.0, 2.0,3.0 ,4.0 ,5.0,6.0,7.0,8.0,9.0 };
  44.  
  45. glScalef(0.3, 0.3, 1.0f);
  46. glTranslatef(-2, 0.0, 0.0);
  47. glColor3f(1, 0, 0);
  48. glPointSize(3);
  49. glBegin(GL_POINTS);
  50. for (int i = 0; i < n; i++) {
  51. glVertex3f(ctlarray[i][0], ctlarray[i][1], ctlarray[i][2]);
  52. }
  53. glEnd();
  54.  
  55.  
  56. double X = 0.0;
  57. double Y = 0.0;
  58. double basis = 0.0;
  59. for (double t = k-1; t <= n; t += 0.001) {
  60. for (int i = 0; i < n; i++) {
  61. basis = N_i_k(k, i, knot, t);
  62.  
  63. glBegin(GL_POINTS);
  64. glColor3f(0, 0, 1);
  65. glVertex3f(t-2, basis-1, 0);
  66. X += ctlarray[i][0] * basis;
  67. Y += ctlarray[i][1] * basis;
  68. }
  69. glEnd();
  70.  
  71.  
  72.  
  73.  
  74. glBegin(GL_POINTS);
  75. glColor3f(0, 1, 1);
  76. glVertex3f(X, Y, 0);
  77. glEnd();
  78. X = 0.0;
  79. Y = 0.0;
  80. }
  81.  
  82. glEnd();
  83. glFlush();
  84. }
  85.  
  86. void main() {
  87. glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
  88. glutInitWindowSize(480, 480);
  89. glutInitWindowPosition(100, 100);
  90. glutCreateWindow(" ");
  91. init();
  92. glutDisplayFunc(Display);
  93. glutMainLoop();
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement