Skip to content

Commit

Permalink
Add bounding box feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rcolyer committed Aug 5, 2022
1 parent 039d997 commit 8ce827f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,9 @@ function Translate(v)
// Prepare a matrix to scale by vector v.
function Scale(v)
// Find the bounding box of pointarrays.
// Returns [[min_x, min_y, min_z], [max_x, max_y, max_z]]
function BBox(pointarrays)
```

16 changes: 15 additions & 1 deletion closepoints.scad
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Created 2021 by Ryan A. Colyer.
// Created 2021-2022 by Ryan A. Colyer.
// This work is released with CC0 into the public domain.
// https://creativecommons.org/publicdomain/zero/1.0/

Expand Down Expand Up @@ -159,3 +159,17 @@ function Scale(v) =
[ 0, v[1], 0, 0],
[ 0, 0, v[2], 0]];

// Find the bounding box of pointarrays.
// Returns [[min_x, min_y, min_z], [max_x, max_y, max_z]]
function BBox(pointarrays) = let(
inf = 1e300*1e300,
minmax = function(p, a=0, b=0, res=[[inf, inf, inf], [-inf, -inf, -inf]])
a >= len(p) ? res :
minmax(p, b >= len(p[a])-1 ? a+1 : a, (b+1) % len(p[a]),
[[min(res[0][0], p[a][b][0]), min(res[0][1], p[a][b][1]),
min(res[0][2], p[a][b][2])],
[max(res[1][0], p[a][b][0]), max(res[1][1], p[a][b][1]),
max(res[1][2], p[a][b][2])]])
)
minmax(pointarrays);

0 comments on commit 8ce827f

Please sign in to comment.