1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-01 04:20:27 +02:00

add px_surround

This commit is contained in:
Justin Lin
2020-03-09 11:33:16 +08:00
parent 56d169e677
commit 2d1bbe05f2
2 changed files with 45 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
use <experimental/has.scad>;
function _px_surround_corner_value(pts, x, y) =
let(
c1 = has(pts, [x, y - 1]) || has(pts, [x - 1, y - 1]) ? 1 : 0,
c2 = has(pts, [x - 1, y]) || has(pts, [x - 1, y + 1]) ? 2 : 0,
c3 = has(pts, [x, y + 1]) || has(pts, [x + 1, y + 1]) ? 4 : 0,
c4 = has(pts, [x + 1, y]) || has(pts, [x + 1, y - 1]) ? 8 : 0
)
c1 + c2 + c3 + c4;
_px_surround_dir_table = [
[4, 0], [6, 0], [7, 0], // RIGHT
[8, 1], [12, 1], [14, 1], // DOWN
[2, 2], [3, 2], [11, 2], // UP
[1, 3], [9, 3], [13, 3] // LEFT
];
function _px_surround_dir(cr_value) =
lookup(cr_value, _px_surround_dir_table);
_px_surround_nxt_offset = [
[1, 0], // RIGHT
[0, -1], // DOWN
[0, 1], // UP
[-1, 0] // LEFT
];
function _px_surround_travel(pts, p, fst) =
let(
dir_i = _px_surround_dir(_px_surround_corner_value(pts, p[0], p[1])),
nxt_p = p + _px_surround_nxt_offset[dir_i]
)
nxt_p == fst ? [p] :
concat(
[p], _px_surround_travel(pts, nxt_p, fst)
);

View File

@@ -0,0 +1,10 @@
use <experimental/_impl/_px_surround_impl.scad>;
use <util/sort.scad>;
function px_surround(points) =
let(
// always start from the left-bottom pt
sortedXY = sort(sort(points, by = "x"), by = "y"),
fst = sortedXY[0] + [-1, -1]
)
_px_surround_travel(sortedXY, fst, fst);