Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- GLfloat ctlarray[][3] = { { -0.9, -0.5,0.0 },
- { -0.7, 0.5, 0.0 },
- { -0.4, -0.5, 0.0 },
- { -0.1, 0.5, 0.0 },
- { 0.2, -0.5, 0.0 },
- { 0.5, 0.5, 0.0 } };
- double N_i_k(int k, int i, GLfloat x[], double t) {
- if (k == 1)
- {
- if ((x[i] <= t) && (t <= x[i + 1]))
- return 1;
- else
- return 0;
- }
- else
- {
- double sum1, sum2;
- if (x[i + k -1] == x[i])
- sum1 = 0;
- else
- sum1 = ((t - x[i]) / (x[i + k - 1] - x[i]))*N_i_k(k - 1, i, x, t);
- if (x[i + k + 1] == x[i + 1])
- sum2 = 0;
- else
- sum2 = ((x[i + k ] - t) / (x[i + k ] - x[i + 1]))*N_i_k(k - 1, i + 1, x, t);
- return sum1 + sum2;
- }
- }
- void init(void) {
- glClearColor(1, 1, 1, 1);
- }
- void Display() {
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- int k = 4;
- int n = 6;
- GLfloat knot[] = { 0.0, 1.0, 2.0,3.0 ,4.0 ,5.0,6.0,7.0,8.0,9.0 };
- glScalef(0.3, 0.3, 1.0f);
- glTranslatef(-2, 0.0, 0.0);
- glColor3f(1, 0, 0);
- glPointSize(3);
- glBegin(GL_POINTS);
- for (int i = 0; i < n; i++) {
- glVertex3f(ctlarray[i][0], ctlarray[i][1], ctlarray[i][2]);
- }
- glEnd();
- double X = 0.0;
- double Y = 0.0;
- double basis = 0.0;
- for (double t = k-1; t <= n; t += 0.001) {
- for (int i = 0; i < n; i++) {
- basis = N_i_k(k, i, knot, t);
- glBegin(GL_POINTS);
- glColor3f(0, 0, 1);
- glVertex3f(t-2, basis-1, 0);
- X += ctlarray[i][0] * basis;
- Y += ctlarray[i][1] * basis;
- }
- glEnd();
- glBegin(GL_POINTS);
- glColor3f(0, 1, 1);
- glVertex3f(X, Y, 0);
- glEnd();
- X = 0.0;
- Y = 0.0;
- }
- glEnd();
- glFlush();
- }
- void main() {
- glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
- glutInitWindowSize(480, 480);
- glutInitWindowPosition(100, 100);
- glutCreateWindow(" ");
- init();
- glutDisplayFunc(Display);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement