德卡斯特里奥算法:手打(备忘)

#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;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值