1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-07 15:26:39 +02:00
This commit is contained in:
Justin Lin
2020-03-12 20:45:41 +08:00
parent 7f03bc7561
commit b192a8ff48

View File

@@ -1,34 +1,32 @@
/**
* px_polygon.scad
*
* @copyright Justin Lin, 2019
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib2x-px_polygon.html
*
**/
use <in_shape.scad>; use <in_shape.scad>;
use <util/sort.scad>;
use <util/dedup.scad>;
use <pixel/px_polyline.scad>; use <pixel/px_polyline.scad>;
function px_polygon(points, filled = false) = function px_polygon(points, filled = false) =
filled ? let(contour = px_polyline(concat(points, [points[0]])))
!filled ? contour :
let( let(
xs = [for(pt = points) pt[0]], sortedXY = sort(sort(contour, by = "x"), by = "y"),
ys = [for(pt = points) pt[1]], ys = [for(p = sortedXY) p[1]],
max_x = max(xs), rows = [
min_x = min(xs), for(y = [min(ys):max(ys)])
max_y = max(ys), let(
min_y = min(ys) idxes = search(y, sortedXY, num_returns_per_match = 0, index_col_num = 1)
)
[for(i = idxes) sortedXY[i]]
]
) )
[ dedup(
for(y = min_y; y <= max_y; y = y + 1) concat(
for(x = min_x; x <= max_x; x = x + 1) sortedXY,
let(pt = [x, y]) [
if(in_shape(points, pt, true)) pt for(row = rows)
] let(to = len(row) - 1, y = row[0][1])
: if(to > 0 && (row[0][0] + 1 != row[to][0]))
px_polyline( for(i = [row[0][0] + 1:row[to][0] - 1])
concat(points, [points[len(points) - 1], points[0]]) let(p = [i, y])
if(in_shape(points, p)) p
]
)
); );