курва безье, памятка себе |
[Jul. 20th, 2017|06:27 pm] |
когда надо быстренько, несколькими точками ебануть кривую безье.
// evaluate a point on a bezier-curve. t goes from 0 to 1
static VT bezierAt(VT) (float t, const(VT)[] pts...) if (IsVectorDim!(VT, 2)) {
assert(pts.length == 3 || pts.length == 4); // quadratic or cubic
VT[4][2] xpts;
xpts[0][0..pts.length] = pts[];
int curidx = 0;
foreach (immutable lc; 1..pts.length) {
foreach (immutable idx; 0..pts.length-lc) xpts[1-curidx][idx] = xpts[curidx][idx].lerp(xpts[curidx][idx+1], t);
curidx ^= 1;
}
return xpts[curidx][0];
}
enum BezierPoints = 16;
foreach (immutable int pn; 0..BezierPoints) {
float t = cast(float)pn/cast(float)(BezierPoints-1);
auto pt = bezierAt(t, pt0, pt1, pt2); // you may add pt3 too
drawPixel(pt);
} и конвертация quadratic в cubic:
VT[4] bezier2cubic(VT) (in auto ref VT v0, in auto ref VT v1, in auto ref VT v2) if (IsVectorDim!(VT, 2)) {
auto np0 = v0+(v1-v0)*2/3;
auto np1 = v2+(v1-v2)*2/3;
return [v0, np0, np1, v2];
} |
|
|