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



你可能感兴趣的:(cocos2dx Bezier 曲线 长度)