diff --git a/src/_impl/_in_shape_impl.scad b/src/_impl/_in_shape_impl.scad new file mode 100644 index 00000000..7805da94 --- /dev/null +++ b/src/_impl/_in_shape_impl.scad @@ -0,0 +1,37 @@ +use <__comm__/__in_line.scad>; + +function _in_shape_in_line_equation(edge, pt) = + let( + x1 = edge[0][0], + y1 = edge[0][1], + x2 = edge[1][0], + y2 = edge[1][1], + a = (y2 - y1) / (x2 - x1), + b = y1 - a * x1 + ) + (pt[1] == a * pt[0] + b); + +function _in_shape_in_any_edges(edges, pt, epsilon) = + let( + leng = len(edges), + maybe_last = [for(i = 0; i < leng && !__in_line(edges[i], pt, epsilon); i = i + 1) i][leng - 1] + ) + is_undef(maybe_last); + +function _in_shape_interpolate_x(y, p1, p2) = + p1[1] == p2[1] ? p1[0] : ( + p1[0] + (p2[0] - p1[0]) * (y - p1[1]) / (p2[1] - p1[1]) + ); + +function _in_shape_does_pt_cross(pts, i, j, pt) = + ((pts[i][1] > pt[1]) != (pts[j][1] > pt[1])) && + (pt[0] < _in_shape_interpolate_x(pt[1], pts[i], pts[j])); + + +function _in_shape_sub(shapt_pts, leng, pt, cond, i, j) = + j == leng ? cond : ( + _in_shape_does_pt_cross(shapt_pts, i, j, pt) ? + _in_shape_sub(shapt_pts, leng, pt, !cond, j, j + 1) : + _in_shape_sub(shapt_pts, leng, pt, cond, j, j + 1) + ); + \ No newline at end of file diff --git a/src/in_shape.scad b/src/in_shape.scad index a1afa0a3..7062ef6e 100644 --- a/src/in_shape.scad +++ b/src/in_shape.scad @@ -8,45 +8,9 @@ * **/ -include <__comm__/__to3d.scad>; -include <__comm__/__lines_from.scad>; -include <__comm__/__in_line.scad>; +use <__comm__/__lines_from.scad>; +use <_impl/_in_shape_impl.scad>; -function _in_shape_in_line_equation(edge, pt) = - let( - x1 = edge[0][0], - y1 = edge[0][1], - x2 = edge[1][0], - y2 = edge[1][1], - a = (y2 - y1) / (x2 - x1), - b = y1 - a * x1 - ) - (pt[1] == a * pt[0] + b); - -function _in_shape_in_any_edges(edges, pt, epsilon) = - let( - leng = len(edges), - maybe_last = [for(i = 0; i < leng && !__in_line(edges[i], pt, epsilon); i = i + 1) i][leng - 1] - ) - is_undef(maybe_last); - -function _in_shape_interpolate_x(y, p1, p2) = - p1[1] == p2[1] ? p1[0] : ( - p1[0] + (p2[0] - p1[0]) * (y - p1[1]) / (p2[1] - p1[1]) - ); - -function _in_shape_does_pt_cross(pts, i, j, pt) = - ((pts[i][1] > pt[1]) != (pts[j][1] > pt[1])) && - (pt[0] < _in_shape_interpolate_x(pt[1], pts[i], pts[j])); - - -function _in_shape_sub(shapt_pts, leng, pt, cond, i, j) = - j == leng ? cond : ( - _in_shape_does_pt_cross(shapt_pts, i, j, pt) ? - _in_shape_sub(shapt_pts, leng, pt, !cond, j, j + 1) : - _in_shape_sub(shapt_pts, leng, pt, cond, j, j + 1) - ); - function in_shape(shapt_pts, pt, include_edge = false, epsilon = 0.0001) = let( leng = len(shapt_pts),