From 07343e9e37b97374dfee5c70380de277f28f6d78 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sat, 16 Apr 2022 12:59:58 +0800 Subject: [PATCH] refactor --- src/__comm__/__in_line.scad | 5 ++++- src/_impl/_in_shape_impl.scad | 17 +++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/__comm__/__in_line.scad b/src/__comm__/__in_line.scad index db4ebf07..a595a5be 100644 --- a/src/__comm__/__in_line.scad +++ b/src/__comm__/__in_line.scad @@ -5,4 +5,7 @@ function __in_line(line_pts, pt, epsilon = 0.0001) = v1 = pts[0] - pt3d, v2 = pts[1] - pt3d ) - (norm(cross(v1, v2)) < epsilon) && ((v1 * v2) <= epsilon); \ No newline at end of file + v1 * v2 <= epsilon && ( + let(v = cross(v1, v2)) + v * v < (epsilon ^ 2) + ); \ No newline at end of file diff --git a/src/_impl/_in_shape_impl.scad b/src/_impl/_in_shape_impl.scad index 9e177a92..614447a1 100644 --- a/src/_impl/_in_shape_impl.scad +++ b/src/_impl/_in_shape_impl.scad @@ -2,11 +2,10 @@ use <__comm__/__in_line.scad>; function _in_shape_in_line_equation(edge, pt) = let( - v = edge[1] - edge[0], - a = v.y / v.x, - b = edge[0].y - a * edge[0].x + p0 = edge[0], + v = edge[1] - p0 ) - (pt.y == a * pt.x + b); + pt.y == (v.y / v.x) * (pt.x - p0.x) + p0.y; function _in_shape_in_any_edges(edges, pt, epsilon) = let( @@ -16,15 +15,13 @@ function _in_shape_in_any_edges(edges, pt, epsilon) = is_undef(maybe_last); function _in_shape_interpolate_x(y, p1, p2) = - p1.y == p2.y ? p1.x : ( - p1.x + (p2.x - p1.x) * (y - p1.y) / (p2.y - p1.y) - ); + p1.y == p2.y ? p1.x : + let(v = p2 - p1) p1.x + v.x * (y - p1.y) / v.y; function _in_shape_does_pt_cross(pts, i, j, pt) = - ((pts[i].y > pt.y) != (pts[j].y > pt.y)) && - (pt.x < _in_shape_interpolate_x(pt.y, pts[i], pts[j])); + let(pi = pts[i], pj = pts[j]) + ((pi.y - pt.y) * (pj.y - pt.y) < 0) && (pt.x < _in_shape_interpolate_x(pt.y, pi, pj)); - function _in_shape_sub(shapt_pts, leng, pt, cond, i, j) = j == leng ? cond : _in_shape_sub(shapt_pts, leng, pt, _in_shape_does_pt_cross(shapt_pts, i, j, pt) ? !cond : cond, j, j + 1); \ No newline at end of file