#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
typedef struct point{
float x;
float y;
} Point;
long long * fun(int n){
long long *res = new long long[10];
res[0] = 1;
res[1] = 1;
for(int i = 2; i<=n; ++i){
res[i] = res[i-1] * i;
}
return res;
}
void decastljao(Point *res, Point *al, float span, int al_size){
int n = al_size - 1;
if(n < 1){
return ;
}
int steps = (int) (1.0 / span);
long long * NA = fun(n);
// for(int i = 0; i<n; ++i){
// printf("%lld \n", NA[i]);
// }
//分别求曲线上的每一个点,按顺序
for(int i=0; i<= steps; ++i){
float t = i * span;
printf("t = %f \t %f\n", t, span);
if(t>1){
t = 1;
}
float xf = 0;
float yf = 0;
float * tka = new float[n+1];
float * otka = new float[n+1];
for(int j=0; j<=n; ++j){
tka[j] = pow(t, j);
otka[j] = pow(1.0 - t, j);
}
for(int k = 0; k<=n; ++k){
float xs = (NA[n] / (NA[k] * NA[n-k])) * tka[k] * otka[n-k]; //这是公式,死记,曲线上的每一个点,和所有控制点有关(NA),还有分段有关(tka, otka);
xf += al[k].x * xs;
yf += al[k].y * xs;
}
res[i].x = xf;
res[i].y = yf;
printf("temp xf yf = %8.5f : %8.5f\n", xf, yf);
printf("\n");
}
}
int main(){
Point temp;
Point al[10];
int al_size = 3;
float temp_x[] = {1.0, 2.0, 3,0};
float temp_y[] = {1.0, 3.0, 1.0};
for(int i=0; i<al_size; ++i){
al[i].x = temp_x[i];
al[i].y = temp_y[i];
}
Point *res = new Point[10];
float span = 1.0 / 8; //分段数,每一段的比重
decastljao(res, al, span, al_size);
return 0;
}