三次贝塞尔曲线的x坐标求y,快速Newton-Raphson方法
Getting y from x co-ord for cubic bezier curve, fast Newton-Raphson method
给定2D中Bezier曲线(P0,P1,P2,P3)的点,我想找到给定x坐标的y坐标。由于以下限制,该问题得到了很好的定义:
- P0=(0,0),P3=(1,1)
- P1=(t,1-t),对于0,1之间的t
- P2=1-P1(x和y)
考虑到所有的限制,我有以下函数来计算答案我使用Newton-Raphson来计算我想要的点的参数,我知道这很有效,因为我不会让循环完成,直到它完成(在定义的公差范围内)。
我正在使用此功能对图像应用对比度过滤器。对于此0.5,会返回相同的图像,0.0会最大限度地降低对比度,1.0会最大程度地增加对比度。
编辑以下功能已更正,现在可以完美工作。
/*
* Parameters: p - x co-ord of P1,
* x - x we want to find y for
*
* This method is unstable for p ~= 0.5, maybe needs refinement.
*/
#include <iostream>
#include <math.h>
#define ITER_TOL 0.00001
float maths::bezier(float p, float x)
{
if(p < 0.f || p > 1.f || x < 0.f || x > 1.f)
{
std::cerr << "Both parameters must be between 0 and 1, returning dummy value" << std::endl;
return 0.f;
}
//First guess for u
float u = x;
//Coefficients of curve (for x and y co-ord)
float x3 = 6 * p - 2;
float x2 = 3 - 9 * p;
float x1 = 3 * p;
float x0 = -x;
float y3 = 6 * (1-p) - 2;
float y2 = 3 - 9 * (1-p);
float y1 = 3 * (1-p);
//Newton-Raphson refinement
for(int i=0; fabs(x3*u*u*u + x2*u*u + x1*u + x0) > ITER_TOL && i<1000; i++)
{
u = u - (x3*u*u*u + x2*u*u + x1*u + x0) /
(3*x3*u*u + 2*x2*u + x1);
//std::cout << i << ": " << u << std::endl;
//Deal with non-convergence
if(i==999)
{
std::cerr << "Warning, Newton-Raphson method did not converge in Maths.cpp, returning dummy" << std::endl;
return 0.f;
}
}
//Calculate y co-ord
return y3*u*u*u + y2*u*u + y1*u;
}
如果我们设置p=0.5,我们应该得到一条直线,但当我对林空间这样做时绘制这些点,我得到了0.5到1.0之间的弯曲。有人明白为什么会发生这种事吗?如果我能做些什么的话?
我编译了您的代码,注意到循环只运行0或1次迭代。可能是因为fabs
在收敛检查中的某个地方丢失了?
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 比较两个节点坐标的最佳方法是什么?
- 在 c++ 生成器中检测绘制线的窗口坐标的最佳方法
- 此方法是否不适合在 std::unordered_map 中使用 2D 坐标作为键
- 有没有简单的方法可以乘以 cv::矩形大小和坐标
- 允许 n 维坐标的有效方法
- 在窗口中的屏幕上获取坐标像素数据的简单方法
- 三次贝塞尔曲线的x坐标求y,快速Newton-Raphson方法
- 解析c++中坐标字符串的最佳方法
- 在越来越多的坐标范围内运行prim的最快方法
- 在OpenGL 4.0中,是否有一种围绕局部坐标(即从模型视图矩阵)旋转的标准方法