Casteljau c++ Vector2D Spline



我没有得到deCasteljau运行。我在没有检查的代码中写了注释。

这里是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 <GLglut.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. */
}

你可以这样写

//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;

最新更新