贝塞尔函数求值 C++实现

本实现完全基于一般意义下的贝塞尔函数定义,验证了部分阶数为正数的贝塞尔函数值。

此外,本实现需要利用我博客下的那个伽马函数实现。

#include <cmath>
#include <complex>

//任意阶三的第一类贝塞尔函数
//n 为阶数
//z 为复数(也满足实数求值,仅需设置虚部为0)
std::complex<double> JFunction(const double n, const std::complex<double>& z) {	
	std::complex<double> sum(0.0, 0.0);
	std::complex<double> temp(0.0, 0.0);
	int m = 0;
	while (m <= 100)
	{
		temp = pow(-1, m) / tgamma(n + m + 1)/ tgamma(m + 1)* pow(0.5 * z, 2.0 * m + n);
		sum += temp;
		m++;
	}
	return sum;
}

验证:

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在C++实现匀速贝塞尔曲线,可以使用以下步骤: 1. 定义一个结构体来表示二维点: ```cpp struct Point { float x; float y; }; ``` 2. 实现一个函数来计算贝塞尔曲线上的点,该函数接受起点、控制点和终点作为输入参数: ```cpp Point calculateBezierPoint(Point p0, Point p1, Point p2, float t) { float u = 1 - t; float tt = t * t; float uu = u * u; float uuu = uu * u; float ttt = tt * t; Point p; p.x = uuu * p0.x + 3 * uu * t * p1.x + 3 * u * tt * p2.x + ttt * p3.x; p.y = uuu * p0.y + 3 * uu * t * p1.y + 3 * u * tt * p2.y + ttt * p3.y; return p; } ``` 3. 实现一个函数来计算匀速贝塞尔曲线上的点,该函数接受起点、控制点和终点作为输入参数,并且根据给定的速度返回曲线上的点: ```cpp Point calculateUniformBezierPoint(Point p0, Point p1, Point p2, float speed) { float length = 0.0f; float step = 0.001f; // 步长,可以根据需要进行调整 Point previousPoint = calculateBezierPoint(p0, p1, p2, 0); for (float t = step; t <= 1.0f; t += step) { Point currentPoint = calculateBezierPoint(p0, p1, p2, t); length += sqrt((currentPoint.x - previousPoint.x) * (currentPoint.x - previousPoint.x) + (currentPoint.y - previousPoint.y) * (currentPoint.y - previousPoint.y)); previousPoint = currentPoint; } float targetLength = speed * length; float currentLength = 0.0f; previousPoint = calculateBezierPoint(p0, p1, p2, 0); for (float t = step; t <= 1.0f; t += step) { Point currentPoint = calculateBezierPoint(p0, p1, p2, t); float segmentLength = sqrt((currentPoint.x - previousPoint.x) * (currentPoint.x - previousPoint.x) + (currentPoint.y - previousPoint.y) * (currentPoint.y - previousPoint.y)); if (currentLength + segmentLength >= targetLength) { float ratio = (targetLength - currentLength) / segmentLength; Point result; result.x = previousPoint.x + (currentPoint.x - previousPoint.x) * ratio; result.y = previousPoint.y + (currentPoint.y - previousPoint.y) * ratio; return result; } currentLength += segmentLength; previousPoint = currentPoint; } return calculateBezierPoint(p0, p1, p2, 1.0f); } ``` 这样,你就可以使用`calculateUniformBezierPoint`函数来计算匀速贝塞尔曲线上的点了。你需要提供起点、控制点和终点,以及期望的速度作为输入,并且函数会返回曲线上对应速度的点。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东风吹柳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值