gpt4 book ai didi

c++ - C++ 中的 B 样条曲线

转载 作者:行者123 更新时间:2023-11-30 05:18:49 28 4
gpt4 key购买 nike

谁能帮我解决 B 样条曲线错误问题?

我想用c++画B样条曲线,但是即使所有坐标都是正数,线段的坐标是负数。

这是 B 样条曲线代码。

void BSplineCurve(Dot &ControlPoint1, Dot &ControlPoint2, 
Dot &ControlPoint3,Dot &ControlPoint4,
Dot &DrawCurve, double &t){

double t2 = t * t;
double t3 = t2 * t;
double mt3 = (1 - t) * (1 - t) * (1 - t);

double bi3 = mt3 / 6;
double bi2 = ((3 * t3) - (6 * t2) + 4) / 6;
double bi1 = ((-3 * t3) + (3 * t2) + (3 * t) + 1) / 6;
double bi = mt3 / 6;

DrawCurve.x = ControlPoint1.x * bi3;
DrawCurve.x += ControlPoint2.x * bi2;
DrawCurve.x += ControlPoint3.x * bi1;
DrawCurve.x += ControlPoint4.x * bi;

DrawCurve.y = ControlPoint1.y * bi3;
DrawCurve.y += ControlPoint2.y * bi2;
DrawCurve.y += ControlPoint3.y * bi1;
DrawCurve.y += ControlPoint4.y * bi;
}

这是绘图代码。

double t = 3.f;
do{

if ((3 < t) && (t <= 4)) {
BSplineCurve(ControlPoint1, ControlPoint2, ControlPoint3, ControlPoint4, DrawCurve, t);
Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
}
else if ((4 < t) && (t <= 5)) {
BSplineCurve(ControlPoint2, ControlPoint3, ControlPoint4, ControlPoint5, DrawCurve, t);
Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
}
else if ((5 < t) && (t <= 6)) {
BSplineCurve(ControlPoint3, ControlPoint4, ControlPoint5, ControlPoint6, DrawCurve, t);
Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
}
t += 0.001;
} while(t < 6.001);

这是控制点的坐标。

第 1 点:50、50

第 2 点:50、100

第 3 点:200、100

第 4 点:200、50

第 5 点:350、50

第 6 点:350、100

但这是第一段的坐标。

Q3:-1543,-349

最佳答案

您的绘图代码看起来不对。

在函数 BSplineCurve 中,t 参数的值应在 [0, 1] 范围内。通过将 t 从 0 更改为 1,将在点 ControlPoint2ControlPoint3 之间构建三次 B 样条曲线。

你可以尝试这样的事情:

Dot points[6] = {ControlPoint1, ControlPoint2, ControlPoint3, ControlPoint4, ControlPoint5, ControlPoint6};
for(double t = 3.0; t < 6.0; t += 0.001)
{
const int start = static_cast<int>(t);
BSplineCurve(points[start - 3],
points[start - 2],
points[start - 1],
points[start],
DrawCurve,
t - start);
Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
}

更新

您的 B 样条计算代码看起来也不对:-)

bi 应该是 t3/6.0 而不是 mt3/6.0。参见 here (幻灯片 25)。


固定函数看起来像这样(我没有测试):

void BSplineCurve(const Dot &point1, 
const Dot &point2,
const Dot &point3,
const Dot &point4,
const double t,
Dot &result)
{
const double t2 = t * t;
const double t3 = t2 * t;
const double mt = 1.0 - t;
const double mt3 = mt * mt * mt;

const double bi3 = mt3;
const double bi2 = 3 * t3 - 6 * t2 + 4;
const double bi1 =-3 * t3 + 3 * t2 + 3 * t + 1;
const double bi = t3;

result.x = point1.x * bi3 +
point2.x * bi2 +
point3.x * bi1 +
point4.x * bi;
result.x /= 6.0;

result.y = point1.y * bi3 +
point2.y * bi2 +
point3.y * bi1 +
point4.y * bi;
result.y /= 6.0;
}

关于c++ - C++ 中的 B 样条曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41423203/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com