diff --git a/src/experimental/_impl/_px_surround_impl.scad b/src/experimental/_impl/_px_surround_impl.scad new file mode 100644 index 00000000..d33354c5 --- /dev/null +++ b/src/experimental/_impl/_px_surround_impl.scad @@ -0,0 +1,35 @@ +use ; + +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) + ); diff --git a/src/experimental/px_surround.scad b/src/experimental/px_surround.scad new file mode 100644 index 00000000..5cb21245 --- /dev/null +++ b/src/experimental/px_surround.scad @@ -0,0 +1,10 @@ +use ; +use ; + +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); \ No newline at end of file