cocos2dx Bezier 曲线 长度

static int Factorial(int number) 
{
	int factorial = 1;
	int temp = number;
	for (int i = 0; i < number; ++i) 
	{
		factorial *= temp;
		--temp;
	}

	return factorial;
}
static int Combination(int count, int r) {
	return Factorial(count) / (Factorial(r) * Factorial(count - r));
}
static float BezierLenth(const CCPoint points[], int points_count) 
{
	std::vector<CCPoint> move_points;
	
	move_points.clear();

	move_points.push_back(points[0]);

	int index = 0;
	CCPoint  temp_pos;
	float t = 0.f;
	int count = points_count - 1;
	float temp_value = 0.f;
	float lenth = 0;
	CCPoint oldP = points[0];
	while (t < 1.0f) 
	{
		temp_pos.x = 0.f;
		temp_pos.y = 0.f;
		index = 0;
		while (index <= count)
		{
			temp_value = std::pow(t, (float)index) * std::pow(1.f - t, float(count - index)) * Combination(count, index);
			temp_pos.x += points[index].x * temp_value;
			temp_pos.y += points[index].y * temp_value;
			++index;
		}
		move_points.push_back(temp_pos);

		CCPoint tempP = CCPoint((temp_pos.x - oldP.x), (temp_pos.y - oldP.y));
		lenth += sqrt( tempP.x*tempP.x+ tempP.y*tempP.y  );
		oldP = temp_pos;
		t += 0.002f;
	}
	CCPoint tempP = CCPoint((points[points_count - 1].x - oldP.x), (points[points_count - 1].y - oldP.y));
	lenth += sqrt( tempP.x*tempP.x+ tempP.y*tempP.y  );
	return lenth;
}
<pre name="code" class="cpp">void FishArmature::update( float dt )
{

	m_pastDt+=(dt)/m_duration;
	int b = move_points.size()*(m_pastDt+m_pastDuration/m_duration);
	if (b>=move_points.size())
	{
		swimDonCallback();
		return;
	}
	this->m_armature->setPosition(move_points[b]);

	if(oldPosition.x == m_armature->getPosition().x && oldPosition.y == m_armature->getPosition().y )
	{
		return;
	}
	CCPoint dp = oldPosition - m_armature->getPosition();
	float deg       =   CC_RADIANS_TO_DEGREES(atan2(dp.x,dp.y));

	m_armature->setRotation(deg );

	oldPosition = m_armature->getPosition();
	//CCLOG("%f", m_pastDt);
}



                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值