2014-06-26 78 views
0

我想讓deCasteljau運行。我在代碼中寫了評論,我沒有檢查。Casteljau C++ Vector2D Spline

這裏的僞代碼deCasteljau:

DeCasteljau(anz, P[], t){ 
for(j=anz-2; j>=0; j--) 
for(i=0; i<=j; i++) 
    P[i]=(1-t) * P[i] + t * P[i+1]; 
} 

我必須使用的Vector2D。

#include <stdlib.h> 
#include <GL\glut.h> 
#include <iostream> 
#include <cmath> 
#include "Vector2D.h" 

using namespace std; 

#define PI 3.14159265 

int w = 600; 
int h = 600; 

int array[][2] = { { 0, 0 }, { 2, 4 }, { 3 ,5 } }; 


Vector2D p0(0,0); 
Vector2D p1(0,0); 
Vector2D p2(0,0); 

float deCasteljau(float t){ 

Vector2D x; 
Vector2D y; 

p0.setXY(array[0][0], array[0][1]); 
p1.setXY(array[1][0], array[1][1]); 
p2.setXY(array[2][0], array[2][1]); 

for (int j=3-2;j>=0;j--) 
    for (int i=0; i<=j; i++) 
    { 
     //how to write this: P[i]=(1-t) * P[i] + t * P[i+1] 
    } 
return P[0]; //and here to 

} 

void display(void) { 
/* clear all pixels */ 
glClear(GL_COLOR_BUFFER_BIT); 

glColor3f(1.0, 1.0, 0.0); 
glBegin(GL_POINTS); 

    //how is the code for draw the spline? 

glEnd(); 


glFlush(); 
} 

void init(void) { 
/* select clearing color */ 
glClearColor(0.0, 0.0, 0.0, 0.0); 

/* initialize viewing values */ 
glOrtho(0, w, 0, h, -1, 1); 
} 

int main(int argc, char** argv) { 
glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
glutInitWindowSize(w + 100, h + 100); 
glutInitWindowPosition(100, 0); 
glutCreateWindow("Aufgabe7"); 
    for(int t=0; t<=1;t=t+0.01){ 
     deCasteljau(t); 
    } 
init(); 
glutDisplayFunc(display); 
glutMainLoop(); 

return 0; /* ANSI C requires main to return int. */ 
} 

回答

0

你可以這樣寫

//how to write this: P[i]=(1-t) * P[i] + t * P[i+1] 

P[i].x= (1-t) * P[i].x + t * P[i+1].x; 
P[i].y= (1-t) * P[i].y + t * P[i+1].y; 
+0

@Joel:目的是保持OP的評論逐字 –