1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-03-14 11:10:01 +01:00

add px_polygon

This commit is contained in:
Justin Lin 2019-06-25 18:08:14 +08:00
parent 07fd4e47e3
commit cad5623248
3 changed files with 45 additions and 0 deletions

24
src/pixel/px_polygon.scad Normal file
View File

@ -0,0 +1,24 @@
include <in_shape.scad>;
include <pixel/px_polyline.scad>;
function px_polygon(points, filled = true) =
filled ?
let(
xs = [for(pt = points) pt[0]],
ys = [for(pt = points) pt[1]],
max_x = max(xs),
min_x = min(xs),
max_y = max(ys),
min_y = min(ys)
)
[
for(x = min_x; x <= max_x; x = x + 1)
for(y = min_y; y <= max_y; y = y + 1)
let(pt = [x, y])
if(in_shape(points, pt, true)) pt
]
:
px_polyline(
concat(points, [points[len(points) - 1], points[0]])
);

View File

@ -0,0 +1,20 @@
module test_px_polygon() {
echo("==== test_px_polygon ====");
include <unittest.scad>;
include <pixel/px_line.scad>;
include <pixel/px_polyline.scad>;
include <pixel/px_polygon.scad>;
expected = [[5, 10], [4, 10], [3, 9], [2, 9], [1, 9], [0, 8], [-1, 8], [-2, 8], [-3, 7], [-4, 7], [-5, 7], [-6, 6], [-7, 6], [-8, 6], [-9, 5], [-10, 5], [-10, 4], [-9, 3], [-9, 2], [-9, 1], [-8, 0], [-8, -1], [-8, -2], [-7, -3], [-7, -4], [-7, -5], [-6, -6], [-6, -7], [-6, -8], [-5, -9], [-5, -10], [-5, -10], [-4, -9], [-4, -8], [-3, -7], [-3, -6], [-2, -5], [-2, -4], [-1, -3], [-1, -2], [0, -1], [0, 0], [1, 1], [1, 2], [2, 3], [2, 4], [3, 5], [3, 6], [4, 7], [4, 8], [5, 9]];
actual = px_polygon([[5, 10], [-10, 5], [-5, -10]], false);
assertEqualPoints(expected, actual);
expected2 = [[-10, 5], [-9, 2], [-9, 3], [-9, 4], [-9, 5], [-8, -1], [-8, 0], [-8, 1], [-8, 2], [-8, 3], [-8, 4], [-8, 5], [-7, -4], [-7, -3], [-7, -2], [-7, -1], [-7, 0], [-7, 1], [-7, 2], [-7, 3], [-7, 4], [-7, 5], [-7, 6], [-6, -7], [-6, -6], [-6, -5], [-6, -4], [-6, -3], [-6, -2], [-6, -1], [-6, 0], [-6, 1], [-6, 2], [-6, 3], [-6, 4], [-6, 5], [-6, 6], [-5, -10], [-5, -9], [-5, -8], [-5, -7], [-5, -6], [-5, -5], [-5, -4], [-5, -3], [-5, -2], [-5, -1], [-5, 0], [-5, 1], [-5, 2], [-5, 3], [-5, 4], [-5, 5], [-5, 6], [-4, -8], [-4, -7], [-4, -6], [-4, -5], [-4, -4], [-4, -3], [-4, -2], [-4, -1], [-4, 0], [-4, 1], [-4, 2], [-4, 3], [-4, 4], [-4, 5], [-4, 6], [-4, 7], [-3, -6], [-3, -5], [-3, -4], [-3, -3], [-3, -2], [-3, -1], [-3, 0], [-3, 1], [-3, 2], [-3, 3], [-3, 4], [-3, 5], [-3, 6], [-3, 7], [-2, -4], [-2, -3], [-2, -2], [-2, -1], [-2, 0], [-2, 1], [-2, 2], [-2, 3], [-2, 4], [-2, 5], [-2, 6], [-2, 7], [-1, -2], [-1, -1], [-1, 0], [-1, 1], [-1, 2], [-1, 3], [-1, 4], [-1, 5], [-1, 6], [-1, 7], [-1, 8], [0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [3, 6], [3, 7], [3, 8], [3, 9], [4, 8], [4, 9], [5, 10]];
actual2 = px_polygon([[5, 10], [-10, 5], [-5, -10]]);
}
test_px_polygon();

View File

@ -75,6 +75,7 @@ include <pixel/test_px_polyline.scad>;
include <pixel/test_px_circle.scad>;
include <pixel/test_px_cylinder.scad>;
include <pixel/test_px_sphere.scad>;
include <pixel/test_px_polygon.scad>;
// Matrix
include <matrix/test_m_cumulate.scad>;