我有 2 個函式來計算樣條曲線、二次曲線或三次曲線上的一個點:
struct vec2 {float x, y;};
vec2 spline_quadratic(vec2 & a, vec2 & b, vec2 & c, float t) {
return {
(1 - t) * (1 - t) * p1.x 2 * (1 - t) * t * p2.x t * t * p3.x,
(1 - t) * (1 - t) * p1.y 2 * (1 - t) * t * p2.y t * t * p3.y
};
}
vec2 spline_cubic(vec2 & a, vec2 & b, vec2 & c, vec2 & d, float t){
return {
//B(t) = (1-t)**3 p0 3(1 - t)**2 t P1 3(1-t)t**2 P2 t**3 P3
(1 - t) * (1 - t) * (1 - t) * p1.x 3 * (1 - t) * (1 - t) * t * p2.x 3 * (1 - t) * t * t * p3.x t * t * t * p4.x,
(1 - t) * (1 - t) * (1 - t) * p1.y 3 * (1 - t) * (1 - t) * t * p2.y 3 * (1 - t) * t * t * p3.y t * t * t * p4.y
};
是否可以連接一組點的多條曲線?
我正在尋找一個具有此簽名的函式:
vector<vec2> spline_join(vector<vec2> & points, int segments = 16){
vector<vec2> spline_points;
for(int i = 0; i < points.size()-2; i){
for(int div = 0; div < segments; div){
spline_points.push_back(spline_quadratic(points[0], points[1], points[2], 1.f/segments);
}
}
}
我讀過它需要插值,但我不確定......代碼會是什么樣子?我已經搜索過,但找不到相關的問題和答案...
我已經看到有圖書館,但我正在尋找一個更短的實作。
編輯:我在這里試過問題和答案,顯然這就是我想要的:

我已經清理了這個答案Joining B-Spline segment in OpenGL / C
這不是 Hermite 樣條,Hermite 樣條通過點,B 樣條不通過。
這是有效的方法和結果
float B0(float u) {
//return float(pow(u - 1, 3) / 6.0);
// (1-t)*(1-t)*(1-t)/6.f
return float(pow(1-u, 3) / 6.0);
}
float B1(float u) {
return float((3 * pow(u, 3) - 6 * pow(u, 2) 4) / 6.0);
// (3 * t * t * t - 6 * t * t 4) / 6
}
float B2(float u) {
return float((-3 * pow(u, 3) 3 * pow(u, 2) 3 * u 1) / 6.0);
// (-3 * t * t * t 3 * t * t 3 * t 1) / 6
}
float B3(float u) {
return float(pow(u, 3) / 6.0);
// t * t * t / 6
}
vector<Vec2> computeBSpline(vector<Vec2>& points) {
vector<Vec2> result;
int MAX_STEPS = 100;
int NUM_OF_POINTS = points.size();
for (int i = 0; i < NUM_OF_POINTS - 3; i )
{
//cout << "Computing for P" << i << " P " << i 1 << " P " << i 2 << " P " << i 3 << endl;
for (int j = 0; j <= MAX_STEPS; j )
{
float u = float(j) / float(MAX_STEPS);
float Qx =
B0(u) * points[i].x
B1(u) * points[i 1].x
B2(u) * points[i 2].x
B3(u) * points[i 3].x;
float Qy =
B0(u) * points[i].y
B1(u) * points[i 1].y
B2(u) * points[i 2].y
B3(u) * points[i 3].y;
result.push_back({ Qx, Qy });
//cout << count << '(' << Qx << ", " << Qy << ")\n";
}
}
return result;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382610.html
上一篇:圖中的最短路徑查詢
