mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-01-17 14:18:13 +01:00
added bezier
This commit is contained in:
parent
2ebfe6940b
commit
f5d3b90992
@ -23,3 +23,5 @@ I've been using OpenSCAD for years and created some funny things. Some of them i
|
|||||||
- [hollow_out](https://openhome.cc/eGossip/OpenSCAD/lib-hollow_out.html)
|
- [hollow_out](https://openhome.cc/eGossip/OpenSCAD/lib-hollow_out.html)
|
||||||
- [bend](https://openhome.cc/eGossip/OpenSCAD/lib-bend.html)
|
- [bend](https://openhome.cc/eGossip/OpenSCAD/lib-bend.html)
|
||||||
|
|
||||||
|
- Math
|
||||||
|
- [bezier](https://openhome.cc/eGossip/OpenSCAD/lib-bezier.html)
|
||||||
|
BIN
docs/images/lib-bezier-1.JPG
Normal file
BIN
docs/images/lib-bezier-1.JPG
Normal file
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
28
docs/lib-bezier.md
Normal file
28
docs/lib-bezier.md
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# bezier
|
||||||
|
|
||||||
|
Given a set of control points, the bezier function returns points of the Bézier path. Combined with the `polyline`, `polyline3d` or `hull_polyline3d` module defined in my lib-openscad, you can create a Bézier curve.
|
||||||
|
|
||||||
|
## Parameters
|
||||||
|
|
||||||
|
- `t_step` : The distance between two points of the Bézier path.
|
||||||
|
- `points` : A set of `[x, y, z]` control points.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
If you have four control points and combine with the `hull_polyline3d` module:
|
||||||
|
|
||||||
|
t_step = 0.05;
|
||||||
|
width = 2;
|
||||||
|
|
||||||
|
p0 = [0, 0, 0];
|
||||||
|
p1 = [40, 60, 35];
|
||||||
|
p2 = [-50, 90, 0];
|
||||||
|
p3 = [0, 200, -35];
|
||||||
|
|
||||||
|
points = bezier(t_step,
|
||||||
|
[p0, p1, p2, p3]
|
||||||
|
);
|
||||||
|
|
||||||
|
hull_polyline3d(points, width);
|
||||||
|
|
||||||
|
![bezier](images/lib-bezier-1.JPG)
|
46
src/bezier.scad
Normal file
46
src/bezier.scad
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* bezier.scad
|
||||||
|
*
|
||||||
|
* Given a set of control points, the bezier function returns points of the Bézier path.
|
||||||
|
* Combined with the polyline, polyline3d or hull_polyline3d module defined in my lib-openscad,
|
||||||
|
* you can create a Bézier curve.
|
||||||
|
*
|
||||||
|
* @copyright Justin Lin, 2017
|
||||||
|
* @license https://opensource.org/licenses/lgpl-3.0.html
|
||||||
|
*
|
||||||
|
* @see https://openhome.cc/eGossip/OpenSCAD/lib-bezier.html
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
|
||||||
|
function _combi(r, n) =
|
||||||
|
n == 0 ? 1 : (_combi(r, n - 1) * (r - n + 1) / n);
|
||||||
|
|
||||||
|
function bezier_coordinate(t, pn, n, i = 0) =
|
||||||
|
i == n + 1 ? 0 :
|
||||||
|
(_combi(n, i) * pn[i] * pow(1 - t, n - i) * pow(t, i) +
|
||||||
|
bezier_coordinate(t, pn, n, i + 1));
|
||||||
|
|
||||||
|
function _bezier_point(t, points) =
|
||||||
|
[
|
||||||
|
bezier_coordinate(
|
||||||
|
t,
|
||||||
|
[points[0][0], points[1][0], points[2][0], points[3][0]],
|
||||||
|
len(points) - 1
|
||||||
|
),
|
||||||
|
bezier_coordinate(
|
||||||
|
t,
|
||||||
|
[points[0][1], points[1][1], points[2][1], points[3][1]],
|
||||||
|
len(points) - 1
|
||||||
|
),
|
||||||
|
bezier_coordinate(
|
||||||
|
t,
|
||||||
|
[points[0][2], points[1][2], points[2][2], points[3][2]],
|
||||||
|
len(points) - 1
|
||||||
|
)
|
||||||
|
];
|
||||||
|
|
||||||
|
function bezier(t_step, points) =
|
||||||
|
[
|
||||||
|
for(t = [0: t_step: 1 + t_step])
|
||||||
|
_bezier_point(t, points)
|
||||||
|
];
|
Loading…
x
Reference in New Issue
Block a user