Guest User

Untitled

a guest
Dec 12th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. // Поверхность Безье
  2. #include "glut.h"
  3. #include <string>
  4. #include <iostream>
  5. #include <ostream>
  6. #include <math.h>
  7. #include <stdio.h>
  8. using namespace std;
  9.  
  10. // Полигональная сетка
  11. const int n = 3;
  12. const int m = 3;
  13.  
  14. GLfloat B[][4][2] = {
  15. { { -0.75, -0.75 },{ -0.75, -0.25 },{ -0.75, 0.25 },{ -0.75, 0.75 } } ,
  16. { { -0.25, -0.75 },{ -0.25, -0.25 },{ -0.25, 0.25 },{ -0.25, 0.75 } },
  17. { { 0.25, -0.75 },{ 0.25, -0.25 },{ 0.25, 0.25 },{ 0.25, 0.75 } },
  18. { { 0.0, 0.0 },{ 0.75, -0.25 },{ 0.75, 0.25 },{ 0.75, 0.75 } }
  19. };
  20.  
  21. double B_sum = 0;
  22.  
  23. double u_step = 0.01;
  24. double w_step = 0.01;
  25.  
  26. double** J = new double* [1 / u_step + 1];
  27. double** K = new double* [1 / w_step + 1];
  28.  
  29. double** Q = new double* [1 / u_step + 1];
  30.  
  31. double*** P = new double** [1 / u_step + 1];
  32.  
  33. // Вычисление факториала
  34. int factorial(int x)
  35. {
  36. if (x == 0)
  37. return 1;
  38. return x * factorial(x - 1);
  39. }
  40.  
  41. // Вычисление значения для P_u_w для x
  42. double get_P_u_w_x(double u, double w)
  43. {
  44. double* mas_1 = new double[4];
  45. mas_1[0] = pow(1 - u, 3);
  46. mas_1[1] = 3 * pow(1 - u, 2) * u;
  47. mas_1[2] = 3 * (1 - u) * u * u;
  48. mas_1[3] = pow(u, 3);
  49.  
  50. double* mas_2 = new double[4];
  51. mas_2[0] = pow(1 - w, 3);
  52. mas_2[1] = 3 * pow(1 - w, 2) * w;
  53. mas_2[2] = 3 * (1 - w) * w * w;
  54. mas_2[3] = pow(w, 3);
  55.  
  56. double* helper_mas = new double[4];
  57.  
  58. for (int i = 0; i < 4; i++)
  59. {
  60. helper_mas[i] = 0;
  61. for (int j = 0; j < 4; j++)
  62. {
  63. helper_mas[i] = helper_mas[i] + mas_1[j] * B[j][i][0];
  64. }
  65. }
  66.  
  67. double P_u_w = 0;
  68. for (int i = 0; i < 4; i++)
  69. {
  70. P_u_w = P_u_w + helper_mas[i] * mas_2[i];
  71. }
  72. return P_u_w;
  73. }
  74.  
  75. // Вычисление значения для P_u_w для y
  76. double get_P_u_w_y(double u, double w)
  77. {
  78. double* mas_1 = new double[4];
  79. mas_1[0] = pow(1 - u, 3);
  80. mas_1[1] = 3 * pow(1 - u, 2) * u;
  81. mas_1[2] = 3 * (1 - u) * u * u;
  82. mas_1[3] = pow(u, 3);
  83.  
  84. double* mas_2 = new double[4];
  85. mas_2[0] = pow(1 - w, 3);
  86. mas_2[1] = 3 * pow(1 - w, 2) * w;
  87. mas_2[2] = 3 * (1 - w) * w * w;
  88. mas_2[3] = pow(w, 3);
  89.  
  90. double* helper_mas = new double[4];
  91.  
  92. for (int i = 0; i < 4; i++)
  93. {
  94. helper_mas[i] = 0;
  95. for (int j = 0; j < 4; j++)
  96. {
  97. helper_mas[i] = helper_mas[i] + mas_1[j] * B[j][i][1];
  98. }
  99. }
  100.  
  101. double P_u_w = 0;
  102. for (int i = 0; i < 4; i++)
  103. {
  104. P_u_w = P_u_w + helper_mas[i] * mas_2[i];
  105. }
  106. return P_u_w;
  107. }
  108.  
  109. void init()
  110. {
  111. // Инициализируем массив J
  112. int counter = 0;
  113. for (double u = 0; u <= 1; u = u + u_step)
  114. {
  115. J[counter] = new double[n + 1];
  116. counter++;
  117. }
  118.  
  119. // Инициализируем массив K
  120. counter = 0;
  121. for (double w = 0; w <= 1; w = w + w_step)
  122. {
  123. K[counter] = new double[m + 1];
  124. counter++;
  125. }
  126.  
  127. // Заполнение матрицы J
  128. counter = 0;
  129. for (double u = 0; u <= 1; u = u + u_step)
  130. {
  131. for (int i = 0; i <= n; i++)
  132. {
  133. J[counter][i] = factorial(n) * pow(u, i) * pow(1 - u, n - i) / (factorial(i) * factorial(n - i));
  134. }
  135. counter++;
  136. }
  137.  
  138. // Заполнение матрицы K
  139. counter = 0;
  140. for (double w = 0; w <= 1; w = w + w_step)
  141. {
  142. for (int j = 0; j <= m; j++)
  143. {
  144. K[counter][j] = factorial(m) * pow(w, j) * pow(1 - w, m - j) / (factorial(j) * factorial(m - j));
  145. }
  146. }
  147.  
  148. // Подсчёт значений для P
  149. counter = 0;
  150. for (double u = 0; u <= 1; u = u + u_step)
  151. {
  152. P[counter] = new double* [1 / w_step + 1];
  153. int counter_2 = 0;
  154. for (double w = 0; w <= 1; w = w + w_step)
  155. {
  156. P[counter][counter_2] = new double[2];
  157. P[counter][counter_2][0] = get_P_u_w_x(u, w);
  158. P[counter][counter_2][1] = get_P_u_w_y(u, w);
  159. counter_2++;
  160. }
  161. counter++;
  162. }
  163. }
  164.  
  165. void Display()
  166. {
  167. glClear(GL_COLOR_BUFFER_BIT);
  168.  
  169. glPointSize(3.0);
  170. glColor3f(0, 1, 0);
  171. glBegin(GL_POINTS);
  172.  
  173. int counter = 0;
  174. for (double u = 0; u <= 1; u = u + u_step)
  175. {
  176. int counter_2 = 0;
  177. for (double w = 0; w <= 1; w = w + w_step)
  178. {
  179. glVertex3d(P[counter][counter_2][0], P[counter][counter_2][1], 0);
  180. counter_2++;
  181. }
  182. counter++;
  183. }
  184.  
  185. glEnd();
  186. glFlush();
  187.  
  188. glutPostRedisplay();
  189. glutSwapBuffers();
  190. }
  191.  
  192. int main(int arcg, char** arcv)
  193. {
  194. init();
  195. glutInit(&arcg, arcv);
  196.  
  197. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  198. glutInitWindowSize(960, 480);
  199.  
  200. glutInitWindowPosition(100, 100);
  201. glutCreateWindow(" ");
  202. glClearColor(1, 1, 1, 1);
  203. glutDisplayFunc(Display);
  204. glutMainLoop();
  205. }
Advertisement
Add Comment
Please, Sign In to add comment