diff --git a/README.md b/README.md index 28549284..79e43bd8 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,9 @@ These fundamental modules are helpful when playing OpenSCAD. - [circular_sector](https://openhome.cc/eGossip/OpenSCAD/lib-circular_sector.html) - [arc](https://openhome.cc/eGossip/OpenSCAD/lib-arc.html) +## 3D +- [line3d](https://openhome.cc/eGossip/OpenSCAD/lib-line3d.html) + ## Transformations - [hollow_out](https://openhome.cc/eGossip/OpenSCAD/lib-hollow_out.html) diff --git a/docs/images/lib-line3d-1.JPG b/docs/images/lib-line3d-1.JPG new file mode 100644 index 00000000..dfb860bb Binary files /dev/null and b/docs/images/lib-line3d-1.JPG differ diff --git a/docs/images/lib-line3d-2.JPG b/docs/images/lib-line3d-2.JPG new file mode 100644 index 00000000..01e8fa85 Binary files /dev/null and b/docs/images/lib-line3d-2.JPG differ diff --git a/docs/images/lib-line3d-3.JPG b/docs/images/lib-line3d-3.JPG new file mode 100644 index 00000000..b3e7295e Binary files /dev/null and b/docs/images/lib-line3d-3.JPG differ diff --git a/docs/lib-line3d.md b/docs/lib-line3d.md new file mode 100644 index 00000000..01f8ce43 --- /dev/null +++ b/docs/lib-line3d.md @@ -0,0 +1,42 @@ +# line3d + +Creates a 3D line from two points. + +## Parameters + +- `p1` : 3 element vector `[x, y, z]`. +- `p2` : 3 element vector `[x, y, z]`. +- `thickness` : The line thickness. +- `p1Style` : The end-cap style of the point `p1`. The value must be `CAP_BUTT`, `CAP_CIRCLE` or `CAP_SPHERE`. The default value is `CAP_CIRCLE`. +- `p2Style` : The end-cap style of the point `p2`. The value must be `CAP_BUTT`, `CAP_CIRCLE` or `CAP_ROUND`. The default value is `CAP_CIRCLE`. +- `fn` : It controlls the `$fn` value used by the `circle` or `sphere` module. The default value is `24`. + +## Examples + + line3d( + p1 = [0, 0, 0], + p2 = [10, 2, 10], + thickness = 1 + ); + +![line3d](images/lib-line3d-1.JPG) + + line3d( + p1 = [0, 0, 0], + p2 = [10, 2, 10], + thickness = 1, + p1Style = CAP_BUTT, + p2Style = CAP_BUTT + ); + +![line3d](images/lib-line3d-2.JPG) + + line3d( + p1 = [0, 0, 0], + p2 = [10, 2, 10], + thickness = 1, + p1Style = CAP_SPHERE, + p2Style = CAP_SPHERE + ); + +![line3d](images/lib-line3d-3.JPG) diff --git a/docs/line3d.scad b/docs/line3d.scad new file mode 100644 index 00000000..0c4ccba4 --- /dev/null +++ b/docs/line3d.scad @@ -0,0 +1,69 @@ +/** +* line3d.scad +* +* Creates a 3D line from two points. +* +* @copyright Justin Lin, 2017 +* @license https://opensource.org/licenses/lgpl-3.0.html +* +* @see https://openhome.cc/eGossip/OpenSCAD/lib-line3d.html +* +**/ + +CAP_BUTT = 0; +CAP_CIRCLE = 1; +CAP_SPHERE = 2; + +module line3d(p1, p2, thickness, p1Style = CAP_CIRCLE, p2Style = CAP_CIRCLE, fn = 24) { + $fn = fn; + + r = thickness / 2; + + dx = p2[0] - p1[0]; + dy = p2[1] - p1[1]; + dz = p2[2] - p1[2]; + + + length = sqrt(pow(dx, 2) + pow(dy, 2) + pow(dz, 2)); + + ay = 90 - atan2(dz, sqrt(pow(dx, 2) + pow(dy, 2))); + az = atan2(dy, dx); + + module cap_butt() { + translate(p1) + rotate([0, ay, az]) + linear_extrude(length) + circle(r); + } + + module capCube(p) { + w = r / 1.414; + translate(p) + rotate([0, ay, az]) + translate([0, 0, -w]) + linear_extrude(w * 2) + circle(r); + } + + module capSphere(p) { + translate(p) + rotate([0, ay, az]) + sphere(r * 1.0087); + } + + module cap(p, style) { + if(style == CAP_CIRCLE) { + capCube(p); + } else if(style == CAP_SPHERE) { + if(fn > 4) { + capSphere(p); + } else { + capCube(p); + } + } + } + + cap_butt(); + cap(p1, p1Style); + cap(p2, p2Style); +} \ No newline at end of file