1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-12 01:34:12 +02:00

refactor: cosa, sina

This commit is contained in:
Justin Lin
2022-04-16 11:25:41 +08:00
parent d432557fb2
commit 3ba4b3fdec

View File

@@ -34,13 +34,6 @@ module polyline2d(points, width = 1, startingStyle = "CAP_SQUARE", endingStyle =
p1Style = p1Style, p2Style = p2Style); p1Style = p1Style, p2Style = p2Style);
} }
function angle(p1, p2, p3) =
let(
v1 = p2 - p1,
v2 = p3 - p2
)
acos((v1 * v2) / (norm(v1) * norm(v2)));
module joins(line, radius, i_end, i) { module joins(line, radius, i_end, i) {
if(i < i_end) { if(i < i_end) {
p1 = line[i]; p1 = line[i];
@@ -50,26 +43,25 @@ module polyline2d(points, width = 1, startingStyle = "CAP_SQUARE", endingStyle =
v2 = p3 - p2; v2 = p3 - p2;
c = cross(v1, v2); // c > 0: ct_clk c = cross(v1, v2); // c > 0: ct_clk
a = angle(p1, p2, p3); normv1xv2 = sqrt(v1 * v1 * v2 * v2);
v1a = atan2(v1.y, v1.x); cosa = (v1 * v2) / normv1xv2;
sina = -c / normv1xv2;
ra = c > 0 ? (-90 + v1a) : (90 + v1a - a);
if(joinStyle == "JOIN_ROUND") { translate(p2)
translate(p2) rotate(atan2(v1.y, v1.x) + (c > 0 ? -90 : asin(cosa))) {
rotate(ra) if(joinStyle == "JOIN_ROUND") {
a = acos(cosa);
pie( pie(
radius = radius, radius = radius,
angle = [0, a], angle = [0, a],
$fn = fn * 360 / a $fn = fn * 360 / a
); );
} else if(joinStyle == "JOIN_MITER") { } else if(joinStyle == "JOIN_MITER") {
translate(p2) tana2 = sina / (1 + cosa); // tan(a / 2)
rotate(ra) polygon(radius * [[0, 0], [1, 0], [1, tana2], [cosa, sina]]);
polygon([[0, 0], [radius, 0], [radius, radius * tan(a / 2)], [radius * cos(a), radius * sin(a)]]); } else { // "JOIN_BEVEL"
} else { // "JOIN_BEVEL" polygon(radius * [[0, 0], [1, 0], [cosa, sina]]);
translate(p2) }
rotate(ra)
polygon([[0, 0], [radius, 0], [radius * cos(a), radius * sin(a)]]);
} }
joins(line, radius, i_end, i + 1); joins(line, radius, i_end, i + 1);
@@ -80,11 +72,9 @@ module polyline2d(points, width = 1, startingStyle = "CAP_SQUARE", endingStyle =
line2d(points[0], points[1], width, startingStyle, endingStyle); line2d(points[0], points[1], width, startingStyle, endingStyle);
} }
else { else {
union() { for(i = [1:leng_pts - 1]) {
for(i = [1:leng_pts - 1]) { line_segment(i);
line_segment(i);
}
joins(points, width / 2, leng_pts - 2, 0);
} }
joins(points, width / 2, leng_pts - 2, 0);
} }
} }