Documented circle_tangent() and simplified it.

This commit is contained in:
Chris Palmer 2021-01-06 10:54:21 +00:00
parent 800bb89921
commit 16c1eeef27
2 changed files with 8 additions and 8 deletions

View File

@ -5736,7 +5736,8 @@ Cylinder with a rounded end.
--- ---
<a name="Rounded_polygon"></a> <a name="Rounded_polygon"></a>
## Rounded_polygon ## Rounded_polygon
Draw a polygon with rounded corners. Each element of the vector is the XY coordinate and a radius. Radius can be negative for a concave corner. Draw a polygon with rounded corners. Each element of the vector is the XY coordinate and a radius in clockwise order.
Radius can be negative for a concave corner.
Because the tangents need to be calculated to find the length these can be calculated separately and re-used when drawing to save calculating them twice. Because the tangents need to be calculated to find the length these can be calculated separately and re-used when drawing to save calculating them twice.
@ -5747,6 +5748,7 @@ Because the tangents need to be calculated to find the length these can be calcu
### Functions ### Functions
| Function | Description | | Function | Description |
|:--- |:--- | |:--- |:--- |
| `circle_tangent(p1, p2)` | Compute the clockwise tangent between two circles represented as [x,y,r] |
| `rounded_polygon_length(points, tangents)` | Calculate the length given the point list and the list of tangents computed by ` rounded_polygon_tangents` | | `rounded_polygon_length(points, tangents)` | Calculate the length given the point list and the list of tangents computed by ` rounded_polygon_tangents` |
| `rounded_polygon_tangents(points)` | Compute the straight sections needed to draw and to compute the lengths | | `rounded_polygon_tangents(points)` | Compute the straight sections needed to draw and to compute the lengths |

View File

@ -18,13 +18,14 @@
// //
// //
//! Draw a polygon with rounded corners. Each element of the vector is the XY coordinate and a radius. Radius can be negative for a concave corner. //! Draw a polygon with rounded corners. Each element of the vector is the XY coordinate and a radius in clockwise order.
//! Radius can be negative for a concave corner.
//! //!
//! Because the tangents need to be calculated to find the length these can be calculated separately and re-used when drawing to save calculating them twice. //! Because the tangents need to be calculated to find the length these can be calculated separately and re-used when drawing to save calculating them twice.
// //
include <../utils/core/core.scad> include <../utils/core/core.scad>
function circle_tangent(p1, p2) = function circle_tangent(p1, p2) = //! Compute the clockwise tangent between two circles represented as [x,y,r]
let( let(
r1 = p1[2], r1 = p1[2],
r2 = p2[2], r2 = p2[2],
@ -32,11 +33,8 @@ function circle_tangent(p1, p2) =
dy = p2.y - p1.y, dy = p2.y - p1.y,
d = sqrt(dx * dx + dy * dy), d = sqrt(dx * dx + dy * dy),
theta = atan2(dy, dx) + acos((r1 - r2) / d), theta = atan2(dy, dx) + acos((r1 - r2) / d),
xa = p1.x +(cos(theta) * r1), v = [cos(theta), sin(theta)]
ya = p1.y +(sin(theta) * r1), )[ p1 + r1 * v, p2 + r2 * v ];
xb = p2.x +(cos(theta) * r2),
yb = p2.y +(sin(theta) * r2)
)[ [xa, ya], [xb, yb] ];
function rounded_polygon_tangents(points) = //! Compute the straight sections needed to draw and to compute the lengths function rounded_polygon_tangents(points) = //! Compute the straight sections needed to draw and to compute the lengths
let(len = len(points)) let(len = len(points))