Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include<windows.h>
  2. #include<stdio.h>
  3. #include<string>
  4. #include"glut.h"
  5.  
  6. const int n = 5;
  7. const int k = 4;
  8. GLfloat pp[n][2] = {
  9. -0.4, -0.4,
  10. -0.2, 0.4,
  11. 0, -0.4,
  12. 0.2, 0.4,
  13. 0.4, -0.4
  14. };
  15. GLfloat x[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; //period
  16. //GLfloat x[] = { -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 5 }; //открытый В сплайн
  17.  
  18. void init() {
  19. glClearColor(1, 1, 1, 1);
  20. }
  21.  
  22. double N(int i, int k, double t) {
  23. if (k == 1) {
  24. if (t < x[i] || t > x[i + 1])
  25. return 0;
  26. return 1;
  27. }
  28. double a = 0, b = 0;
  29. if ((x[i + k - 1] - x[i]) != 0) a = (t - x[i])*N(i, k - 1, t) / (x[i + k - 1] - x[i]);
  30. if ((x[i + k] - x[i + 1]) != 0) b = (x[i + k] - t)*N(i + 1, k - 1, t) / (x[i + k] - x[i + 1]);
  31. return a + b;
  32.  
  33. }
  34.  
  35. void Display() {
  36. //glColor3f(0, 0, 0);
  37. glBegin(GL_POINTS);
  38. for (double i = 3; i <= 5; i += 0.001) { // (k-1) < < (n+1)
  39. double x = 0;
  40. double y = 0;
  41. for (int j = 1; j <= n; ++j) {
  42. double n = N(j, k, i);
  43. x += pp[j - 1][0] * n;
  44. y += pp[j - 1][1] * n;
  45. glColor3f(0.01*j, j % 7 * 0.1, j % 2);
  46. //glVertex2d(0.3*(i - 3), 0.3*n - 0.8); //внизу кривые
  47. }
  48. glColor3f(0, 0, 0);
  49. glVertex2d(x - 0.3, y); // splain
  50. }
  51. for (int i = 0; i < n; ++i) {
  52. glVertex2d(pp[i][0] - 0.3, pp[i][1]); // to4ki
  53. }
  54. glEnd();
  55. glutSwapBuffers();
  56. glutPostRedisplay();
  57. }
  58.  
  59. int main() {
  60. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  61. glutInitWindowSize(480, 480);
  62. glutInitWindowPosition(100, 100);
  63. glutCreateWindow(" ");
  64. init();
  65. glutDisplayFunc(Display);
  66. glutMainLoop();
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement