From 16c1eeef27165bd5cf571b09bf277f5935d219c8 Mon Sep 17 00:00:00 2001 From: Chris Palmer Date: Wed, 6 Jan 2021 10:54:21 +0000 Subject: [PATCH] Documented circle_tangent() and simplified it. --- readme.md | 4 +++- utils/rounded_polygon.scad | 12 +++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 055fd5d..486a39e 100644 --- a/readme.md +++ b/readme.md @@ -5736,7 +5736,8 @@ Cylinder with a rounded end. --- ## 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. @@ -5747,6 +5748,7 @@ Because the tangents need to be calculated to find the length these can be calcu ### Functions | 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_tangents(points)` | Compute the straight sections needed to draw and to compute the lengths | diff --git a/utils/rounded_polygon.scad b/utils/rounded_polygon.scad index 8b4db06..85d0f6b 100644 --- a/utils/rounded_polygon.scad +++ b/utils/rounded_polygon.scad @@ -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. // 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( r1 = p1[2], r2 = p2[2], @@ -32,11 +33,8 @@ function circle_tangent(p1, p2) = dy = p2.y - p1.y, d = sqrt(dx * dx + dy * dy), theta = atan2(dy, dx) + acos((r1 - r2) / d), - xa = p1.x +(cos(theta) * r1), - ya = p1.y +(sin(theta) * r1), - xb = p2.x +(cos(theta) * r2), - yb = p2.y +(sin(theta) * r2) - )[ [xa, ya], [xb, yb] ]; + v = [cos(theta), sin(theta)] + )[ p1 + r1 * v, p2 + r2 * v ]; function rounded_polygon_tangents(points) = //! Compute the straight sections needed to draw and to compute the lengths let(len = len(points))