From 7dc29772cc4e0a8bdce1ab2740f08c206c64d57e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 12:27:28 +0100 Subject: [PATCH 01/27] Improve formatting --- crates/fj-math/src/arc.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index d5039731d..4b235172f 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -4,20 +4,26 @@ use crate::{Point, Scalar}; pub struct Arc { /// Start point of the arc pub start: Point<2>, + /// End point of the arc pub end: Point<2>, + /// Center of the circle the arc is constructed on pub center: Point<2>, + /// Radius of the circle the arc is constructed on pub radius: Scalar, + /// Angle of `start` relative to `center`, in radians /// /// Guaranteed to be less than `end_angle`. pub start_angle: Scalar, + /// Angle of `end` relative to `center`, in radians /// /// Guaranteed to be greater than `end_angle`. pub end_angle: Scalar, + /// True if `start` and `end` were switched to ensure `end_angle` > `start_angle` pub flipped_construction: bool, } From bcadf4fd12ae22103ea7d0f9de66c655d43cc739 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 12:45:49 +0100 Subject: [PATCH 02/27] Improve test output --- crates/fj-math/src/arc.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 4b235172f..35c3d871e 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -87,7 +87,7 @@ mod tests { use super::Arc; - use approx::AbsDiffEq; + use approx::{assert_abs_diff_eq, AbsDiffEq}; fn check_arc_calculation(center: [f64; 2], radius: f64, a0: f64, a1: f64) { let angle = a1 - a0; @@ -103,17 +103,37 @@ mod tests { dbg!(arc.start_angle); dbg!(arc.end_angle); dbg!(arc.flipped_construction); - assert!(arc.center.abs_diff_eq(&Point::from(center), epsilon)); - assert!(arc.radius.abs_diff_eq(&Scalar::from(radius), epsilon)); + assert_abs_diff_eq!(arc.center, Point::from(center), epsilon = epsilon); + assert_abs_diff_eq!( + arc.radius, + Scalar::from(radius), + epsilon = epsilon + ); if a0 < a1 { assert!(!arc.flipped_construction); - assert!(arc.start_angle.abs_diff_eq(&Scalar::from(a0), epsilon)); - assert!(arc.end_angle.abs_diff_eq(&Scalar::from(a1), epsilon)); + assert_abs_diff_eq!( + arc.start_angle, + Scalar::from(a0), + epsilon = epsilon + ); + assert_abs_diff_eq!( + arc.end_angle, + Scalar::from(a1), + epsilon = epsilon + ); } else { assert!(arc.flipped_construction); - assert!(arc.end_angle.abs_diff_eq(&Scalar::from(a0), epsilon)); - assert!(arc.start_angle.abs_diff_eq(&Scalar::from(a1), epsilon)); + assert_abs_diff_eq!( + arc.end_angle, + Scalar::from(a0), + epsilon = epsilon + ); + assert_abs_diff_eq!( + arc.start_angle, + Scalar::from(a1), + epsilon = epsilon + ); } } From a088e7c2f6ab062a89f5cbe0b206380f92b8ec39 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 12:46:14 +0100 Subject: [PATCH 03/27] Update order of code --- crates/fj-math/src/arc.rs | 68 +++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 35c3d871e..819b12476 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -89,6 +89,40 @@ mod tests { use approx::{assert_abs_diff_eq, AbsDiffEq}; + #[test] + fn arc_construction() { + check_arc_calculation( + [0., 0.], + 1., + 0_f64.to_radians(), + 90_f64.to_radians(), + ); + check_arc_calculation( + [-4., 2.], + 1.5, + 5_f64.to_radians(), + -5_f64.to_radians(), + ); + check_arc_calculation( + [3., 8.], + 3., + 0_f64.to_radians(), + 100_f64.to_radians(), + ); + check_arc_calculation( + [1., -1.], + 1., + 90_f64.to_radians(), + 180_f64.to_radians(), + ); + check_arc_calculation( + [0., 0.], + 1., + 0_f64.to_radians(), + 270_f64.to_radians(), + ); + } + fn check_arc_calculation(center: [f64; 2], radius: f64, a0: f64, a1: f64) { let angle = a1 - a0; @@ -136,38 +170,4 @@ mod tests { ); } } - - #[test] - fn arc_construction() { - check_arc_calculation( - [0., 0.], - 1., - 0_f64.to_radians(), - 90_f64.to_radians(), - ); - check_arc_calculation( - [-4., 2.], - 1.5, - 5_f64.to_radians(), - -5_f64.to_radians(), - ); - check_arc_calculation( - [3., 8.], - 3., - 0_f64.to_radians(), - 100_f64.to_radians(), - ); - check_arc_calculation( - [1., -1.], - 1., - 90_f64.to_radians(), - 180_f64.to_radians(), - ); - check_arc_calculation( - [0., 0.], - 1., - 0_f64.to_radians(), - 270_f64.to_radians(), - ); - } } From fedb96a93741ba46e66dbf183b9fc25f72225d92 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 12:50:21 +0100 Subject: [PATCH 04/27] Refactor --- crates/fj-math/src/arc.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 819b12476..e9ad7a092 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -123,11 +123,17 @@ mod tests { ); } - fn check_arc_calculation(center: [f64; 2], radius: f64, a0: f64, a1: f64) { + fn check_arc_calculation( + center: impl Into>, + radius: f64, + a0: f64, + a1: f64, + ) { + let center = center.into(); let angle = a1 - a0; - let p0 = [center[0] + radius * a0.cos(), center[1] + radius * a0.sin()]; - let p1 = [center[0] + radius * a1.cos(), center[1] + radius * a1.sin()]; + let p0 = [center.u + radius * a0.cos(), center.v + radius * a0.sin()]; + let p1 = [center.u + radius * a1.cos(), center.v + radius * a1.sin()]; let arc = Arc::from_endpoints_and_angle(p0, p1, Scalar::from(angle)); @@ -137,7 +143,7 @@ mod tests { dbg!(arc.start_angle); dbg!(arc.end_angle); dbg!(arc.flipped_construction); - assert_abs_diff_eq!(arc.center, Point::from(center), epsilon = epsilon); + assert_abs_diff_eq!(arc.center, center, epsilon = epsilon); assert_abs_diff_eq!( arc.radius, Scalar::from(radius), From e3c9bc9a73a20a57c898591bbd3be0625dcb6c1e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 12:51:46 +0100 Subject: [PATCH 05/27] Refactor --- crates/fj-math/src/arc.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index e9ad7a092..8d14f36d6 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -83,7 +83,7 @@ impl Arc { #[cfg(test)] mod tests { - use crate::{Point, Scalar}; + use crate::{Point, Scalar, Vector}; use super::Arc; @@ -132,8 +132,8 @@ mod tests { let center = center.into(); let angle = a1 - a0; - let p0 = [center.u + radius * a0.cos(), center.v + radius * a0.sin()]; - let p1 = [center.u + radius * a1.cos(), center.v + radius * a1.sin()]; + let p0 = center + Vector::from([a0.cos(), a0.sin()]) * radius; + let p1 = center + Vector::from([a1.cos(), a1.sin()]) * radius; let arc = Arc::from_endpoints_and_angle(p0, p1, Scalar::from(angle)); From c6ba1e33d7c6a7d2aa6cee5bcd9f7aa5938dc48c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 12:53:04 +0100 Subject: [PATCH 06/27] Remove redundant test output --- crates/fj-math/src/arc.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 8d14f36d6..3fc61d572 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -139,7 +139,6 @@ mod tests { let epsilon = Scalar::default_epsilon() * 10.; - dbg!(center, arc.center); dbg!(arc.start_angle); dbg!(arc.end_angle); dbg!(arc.flipped_construction); From 989877d3a1183097b2564acd82e2fc77b70d60d4 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 13:04:30 +0100 Subject: [PATCH 07/27] Remove redundant (and unused) fields from `Arc` --- crates/fj-math/src/arc.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 3fc61d572..dcedb2bf7 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -2,12 +2,6 @@ use crate::{Point, Scalar}; /// Calculated geometry that is useful when dealing with an arc pub struct Arc { - /// Start point of the arc - pub start: Point<2>, - - /// End point of the arc - pub end: Point<2>, - /// Center of the circle the arc is constructed on pub center: Point<2>, @@ -70,8 +64,6 @@ impl Arc { let start_angle = (y0 - cy).atan2(x0 - cx); let end_angle = (y1 - cy).atan2(x1 - cx) + end_angle_offset; Self { - start: p0, - end: p1, center: Point::from([cx, cy]), radius: r, start_angle, From 3f1d30245ece0d3f04ce3df19e683b73c3cd525b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 13:05:30 +0100 Subject: [PATCH 08/27] Update argument name --- crates/fj-math/src/arc.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index dcedb2bf7..e440e3770 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -27,14 +27,14 @@ impl Arc { pub fn from_endpoints_and_angle( p0: impl Into>, p1: impl Into>, - angle: Scalar, + angle_rad: Scalar, ) -> Self { use num_traits::Float; let (p0, p1) = (p0.into(), p1.into()); - let flipped_construction = angle <= Scalar::ZERO; - let angle_rad = angle.abs(); + let flipped_construction = angle_rad <= Scalar::ZERO; + let angle_rad = angle_rad.abs(); let [p0, p1] = if flipped_construction { [p1, p0] From 31ecc84bb7c367ea43bae946a97833ae8bf20e25 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 13:33:02 +0100 Subject: [PATCH 09/27] Move import to top level --- crates/fj-math/src/arc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index e440e3770..3dc00802c 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -1,3 +1,5 @@ +use num_traits::Float; + use crate::{Point, Scalar}; /// Calculated geometry that is useful when dealing with an arc @@ -29,8 +31,6 @@ impl Arc { p1: impl Into>, angle_rad: Scalar, ) -> Self { - use num_traits::Float; - let (p0, p1) = (p0.into(), p1.into()); let flipped_construction = angle_rad <= Scalar::ZERO; From 02698267b25025f2a8e039b7685a1060de1f2b91 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 13:34:02 +0100 Subject: [PATCH 10/27] Update comment --- crates/fj-math/src/arc.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 3dc00802c..b10283779 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -31,6 +31,9 @@ impl Arc { p1: impl Into>, angle_rad: Scalar, ) -> Self { + // This is an implementation of this solution: + // https://math.stackexchange.com/a/87374 + let (p0, p1) = (p0.into(), p1.into()); let flipped_construction = angle_rad <= Scalar::ZERO; @@ -48,7 +51,6 @@ impl Arc { (Scalar::ONE, Scalar::ZERO) }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); - // https://math.stackexchange.com/questions/27535/how-to-find-center-of-an-arc-given-start-point-end-point-radius-and-arc-direc // distance between endpoints let d = ((x1 - x0).powi(2) + (y1 - y0).powi(2)).sqrt(); // radius From 66f7e1c379d4e6f9adf4fbe0629522f6809e4bb9 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 13:40:27 +0100 Subject: [PATCH 11/27] Make variable name more explicit --- crates/fj-math/src/arc.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index b10283779..9df80c154 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -54,9 +54,9 @@ impl Arc { // distance between endpoints let d = ((x1 - x0).powi(2) + (y1 - y0).powi(2)).sqrt(); // radius - let r = d / (2. * (angle_rad.into_f64() / 2.).sin()); + let radius = d / (2. * (angle_rad.into_f64() / 2.).sin()); // distance from center to midpoint between endpoints - let h = (r.powi(2) - (d.powi(2) / 4.)).sqrt(); + let h = (radius.powi(2) - (d.powi(2) / 4.)).sqrt(); // (u, v) is the unit normal in the direction of p1 - p0 let u = (x1 - x0) / d * uv_factor; let v = (y1 - y0) / d * uv_factor; @@ -67,7 +67,7 @@ impl Arc { let end_angle = (y1 - cy).atan2(x1 - cx) + end_angle_offset; Self { center: Point::from([cx, cy]), - radius: r, + radius, start_angle, end_angle, flipped_construction, From 2f2cd23722e3a8db0cb23d0aae4ef005a48239ca Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 13:40:36 +0100 Subject: [PATCH 12/27] Remove redundant comment --- crates/fj-math/src/arc.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 9df80c154..fec3b2df6 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -53,7 +53,6 @@ impl Arc { let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); // distance between endpoints let d = ((x1 - x0).powi(2) + (y1 - y0).powi(2)).sqrt(); - // radius let radius = d / (2. * (angle_rad.into_f64() / 2.).sin()); // distance from center to midpoint between endpoints let h = (radius.powi(2) - (d.powi(2) / 4.)).sqrt(); From 6e2d70f2381d0b7e0d42afabf74df16c9ba0b9ef Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:13:36 +0100 Subject: [PATCH 13/27] Refactor --- crates/fj-math/src/arc.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index fec3b2df6..69cab70d2 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -31,11 +31,12 @@ impl Arc { p1: impl Into>, angle_rad: Scalar, ) -> Self { + let p0 = p0.into(); + let p1 = p1.into(); + // This is an implementation of this solution: // https://math.stackexchange.com/a/87374 - let (p0, p1) = (p0.into(), p1.into()); - let flipped_construction = angle_rad <= Scalar::ZERO; let angle_rad = angle_rad.abs(); From c6eb7b4e252d5d0970bfa9aff97d03200669bf0d Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:16:40 +0100 Subject: [PATCH 14/27] Simplify calculation --- crates/fj-math/src/arc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 69cab70d2..8cf8028b6 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -53,7 +53,7 @@ impl Arc { }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); // distance between endpoints - let d = ((x1 - x0).powi(2) + (y1 - y0).powi(2)).sqrt(); + let d = (p1 - p0).magnitude(); let radius = d / (2. * (angle_rad.into_f64() / 2.).sin()); // distance from center to midpoint between endpoints let h = (radius.powi(2) - (d.powi(2) / 4.)).sqrt(); From 9c1b3afbac57d93b8032b8d4ebb8d39da290ca03 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:19:55 +0100 Subject: [PATCH 15/27] Make variable name more explicit --- crates/fj-math/src/arc.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 8cf8028b6..a456715e2 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -53,13 +53,15 @@ impl Arc { }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); // distance between endpoints - let d = (p1 - p0).magnitude(); - let radius = d / (2. * (angle_rad.into_f64() / 2.).sin()); + let distance_between_endpoints = (p1 - p0).magnitude(); + let radius = distance_between_endpoints + / (2. * (angle_rad.into_f64() / 2.).sin()); // distance from center to midpoint between endpoints - let h = (radius.powi(2) - (d.powi(2) / 4.)).sqrt(); + let h = + (radius.powi(2) - (distance_between_endpoints.powi(2) / 4.)).sqrt(); // (u, v) is the unit normal in the direction of p1 - p0 - let u = (x1 - x0) / d * uv_factor; - let v = (y1 - y0) / d * uv_factor; + let u = (x1 - x0) / distance_between_endpoints * uv_factor; + let v = (y1 - y0) / distance_between_endpoints * uv_factor; // (cx, cy) is the center of the circle let cx = ((x0 + x1) / 2.) - h * v; let cy = ((y0 + y1) / 2.) + h * u; From 69241d6fec1bd286aa3bc51dd3f3bfddca73bb4d Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:20:13 +0100 Subject: [PATCH 16/27] Remove redunant comment --- crates/fj-math/src/arc.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index a456715e2..b5cb0077f 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -52,7 +52,6 @@ impl Arc { (Scalar::ONE, Scalar::ZERO) }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); - // distance between endpoints let distance_between_endpoints = (p1 - p0).magnitude(); let radius = distance_between_endpoints / (2. * (angle_rad.into_f64() / 2.).sin()); From 07846bb456b73c719ecf34881ab8251a2f628245 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:20:32 +0100 Subject: [PATCH 17/27] Move code --- crates/fj-math/src/arc.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index b5cb0077f..3e21ad832 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -37,6 +37,8 @@ impl Arc { // This is an implementation of this solution: // https://math.stackexchange.com/a/87374 + let distance_between_endpoints = (p1 - p0).magnitude(); + let flipped_construction = angle_rad <= Scalar::ZERO; let angle_rad = angle_rad.abs(); @@ -52,7 +54,6 @@ impl Arc { (Scalar::ONE, Scalar::ZERO) }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); - let distance_between_endpoints = (p1 - p0).magnitude(); let radius = distance_between_endpoints / (2. * (angle_rad.into_f64() / 2.).sin()); // distance from center to midpoint between endpoints From 22e4536a418ce25a943d7d8256aeb1eb4ba1eb72 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:28:08 +0100 Subject: [PATCH 18/27] Refactor --- crates/fj-math/src/arc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 3e21ad832..41f3a4921 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -38,6 +38,8 @@ impl Arc { // https://math.stackexchange.com/a/87374 let distance_between_endpoints = (p1 - p0).magnitude(); + let radius = distance_between_endpoints + / (2. * (angle_rad.abs().into_f64() / 2.).sin()); let flipped_construction = angle_rad <= Scalar::ZERO; let angle_rad = angle_rad.abs(); @@ -54,8 +56,6 @@ impl Arc { (Scalar::ONE, Scalar::ZERO) }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); - let radius = distance_between_endpoints - / (2. * (angle_rad.into_f64() / 2.).sin()); // distance from center to midpoint between endpoints let h = (radius.powi(2) - (distance_between_endpoints.powi(2) / 4.)).sqrt(); From e56c1d71284a528012ce75c89f350831aed22094 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:28:51 +0100 Subject: [PATCH 19/27] Make variable name more explicit --- crates/fj-math/src/arc.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 41f3a4921..f05e60bae 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -57,14 +57,14 @@ impl Arc { }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); // distance from center to midpoint between endpoints - let h = + let distance_center_to_midpoint = (radius.powi(2) - (distance_between_endpoints.powi(2) / 4.)).sqrt(); // (u, v) is the unit normal in the direction of p1 - p0 let u = (x1 - x0) / distance_between_endpoints * uv_factor; let v = (y1 - y0) / distance_between_endpoints * uv_factor; // (cx, cy) is the center of the circle - let cx = ((x0 + x1) / 2.) - h * v; - let cy = ((y0 + y1) / 2.) + h * u; + let cx = ((x0 + x1) / 2.) - distance_center_to_midpoint * v; + let cy = ((y0 + y1) / 2.) + distance_center_to_midpoint * u; let start_angle = (y0 - cy).atan2(x0 - cx); let end_angle = (y1 - cy).atan2(x1 - cx) + end_angle_offset; Self { From e48c0d6b7bf95b99f723b1ede5a44374d83d20b3 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:29:00 +0100 Subject: [PATCH 20/27] Remove redundant comment --- crates/fj-math/src/arc.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index f05e60bae..d40bb85da 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -56,7 +56,6 @@ impl Arc { (Scalar::ONE, Scalar::ZERO) }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); - // distance from center to midpoint between endpoints let distance_center_to_midpoint = (radius.powi(2) - (distance_between_endpoints.powi(2) / 4.)).sqrt(); // (u, v) is the unit normal in the direction of p1 - p0 From 4b4354f2cc3b5ffe0b62b2541d1e0488c9601a58 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:29:28 +0100 Subject: [PATCH 21/27] Move code --- crates/fj-math/src/arc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index d40bb85da..6da8eab67 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -40,6 +40,8 @@ impl Arc { let distance_between_endpoints = (p1 - p0).magnitude(); let radius = distance_between_endpoints / (2. * (angle_rad.abs().into_f64() / 2.).sin()); + let distance_center_to_midpoint = + (radius.powi(2) - (distance_between_endpoints.powi(2) / 4.)).sqrt(); let flipped_construction = angle_rad <= Scalar::ZERO; let angle_rad = angle_rad.abs(); @@ -56,8 +58,6 @@ impl Arc { (Scalar::ONE, Scalar::ZERO) }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); - let distance_center_to_midpoint = - (radius.powi(2) - (distance_between_endpoints.powi(2) / 4.)).sqrt(); // (u, v) is the unit normal in the direction of p1 - p0 let u = (x1 - x0) / distance_between_endpoints * uv_factor; let v = (y1 - y0) / distance_between_endpoints * uv_factor; From ce0fbeefb30b340e48842a2317a3acdcc6ab9ded Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:47:32 +0100 Subject: [PATCH 22/27] Refactor --- crates/fj-math/src/arc.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index 6da8eab67..f583e1d62 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -59,11 +59,13 @@ impl Arc { }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); // (u, v) is the unit normal in the direction of p1 - p0 - let u = (x1 - x0) / distance_between_endpoints * uv_factor; - let v = (y1 - y0) / distance_between_endpoints * uv_factor; + let unit_vector_p0_to_p1 = + (p1 - p0) / distance_between_endpoints * uv_factor; // (cx, cy) is the center of the circle - let cx = ((x0 + x1) / 2.) - distance_center_to_midpoint * v; - let cy = ((y0 + y1) / 2.) + distance_center_to_midpoint * u; + let cx = ((x0 + x1) / 2.) + - distance_center_to_midpoint * unit_vector_p0_to_p1.v; + let cy = ((y0 + y1) / 2.) + + distance_center_to_midpoint * unit_vector_p0_to_p1.u; let start_angle = (y0 - cy).atan2(x0 - cx); let end_angle = (y1 - cy).atan2(x1 - cx) + end_angle_offset; Self { From 0ef7909a4fd971395e1b3d6c0a9f423039236495 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:47:41 +0100 Subject: [PATCH 23/27] Remove redunant comment --- crates/fj-math/src/arc.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index f583e1d62..b94c9967b 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -58,7 +58,6 @@ impl Arc { (Scalar::ONE, Scalar::ZERO) }; let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); - // (u, v) is the unit normal in the direction of p1 - p0 let unit_vector_p0_to_p1 = (p1 - p0) / distance_between_endpoints * uv_factor; // (cx, cy) is the center of the circle From 6a442037d3f2a9c843ea4b56dc104e0fa855f743 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:49:30 +0100 Subject: [PATCH 24/27] Refactor --- crates/fj-math/src/arc.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index b94c9967b..de00a2d03 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -1,6 +1,6 @@ use num_traits::Float; -use crate::{Point, Scalar}; +use crate::{Point, Scalar, Vector}; /// Calculated geometry that is useful when dealing with an arc pub struct Arc { @@ -60,11 +60,13 @@ impl Arc { let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); let unit_vector_p0_to_p1 = (p1 - p0) / distance_between_endpoints * uv_factor; + let unit_vector_midpoint_to_center = + Vector::from([-unit_vector_p0_to_p1.v, unit_vector_p0_to_p1.u]); // (cx, cy) is the center of the circle let cx = ((x0 + x1) / 2.) - - distance_center_to_midpoint * unit_vector_p0_to_p1.v; + + distance_center_to_midpoint * unit_vector_midpoint_to_center.u; let cy = ((y0 + y1) / 2.) - + distance_center_to_midpoint * unit_vector_p0_to_p1.u; + + distance_center_to_midpoint * unit_vector_midpoint_to_center.v; let start_angle = (y0 - cy).atan2(x0 - cx); let end_angle = (y1 - cy).atan2(x1 - cx) + end_angle_offset; Self { From 0bbd5ab911a5045932650242f0c6ea283eab37c9 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:51:22 +0100 Subject: [PATCH 25/27] Refactor --- crates/fj-math/src/arc.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index de00a2d03..c9ccd3bb4 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -63,14 +63,14 @@ impl Arc { let unit_vector_midpoint_to_center = Vector::from([-unit_vector_p0_to_p1.v, unit_vector_p0_to_p1.u]); // (cx, cy) is the center of the circle - let cx = ((x0 + x1) / 2.) - + distance_center_to_midpoint * unit_vector_midpoint_to_center.u; - let cy = ((y0 + y1) / 2.) - + distance_center_to_midpoint * unit_vector_midpoint_to_center.v; - let start_angle = (y0 - cy).atan2(x0 - cx); - let end_angle = (y1 - cy).atan2(x1 - cx) + end_angle_offset; + let center = Point { + coords: (p0.coords + p1.coords) / 2. + + unit_vector_midpoint_to_center * distance_center_to_midpoint, + }; + let start_angle = (y0 - center.v).atan2(x0 - center.u); + let end_angle = (y1 - center.v).atan2(x1 - center.u) + end_angle_offset; Self { - center: Point::from([cx, cy]), + center, radius, start_angle, end_angle, From 57eaaabda562ee0e8a2cdce4ad4e4083513ecaa9 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:51:31 +0100 Subject: [PATCH 26/27] Remove redundant comment --- crates/fj-math/src/arc.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index c9ccd3bb4..b4e5fabb8 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -62,7 +62,6 @@ impl Arc { (p1 - p0) / distance_between_endpoints * uv_factor; let unit_vector_midpoint_to_center = Vector::from([-unit_vector_p0_to_p1.v, unit_vector_p0_to_p1.u]); - // (cx, cy) is the center of the circle let center = Point { coords: (p0.coords + p1.coords) / 2. + unit_vector_midpoint_to_center * distance_center_to_midpoint, From 8c78a11fe1fa25af75b7bbf5e5125f78d71016cf Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Feb 2023 14:54:43 +0100 Subject: [PATCH 27/27] Refactor --- crates/fj-math/src/arc.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/fj-math/src/arc.rs b/crates/fj-math/src/arc.rs index b4e5fabb8..0cd991056 100644 --- a/crates/fj-math/src/arc.rs +++ b/crates/fj-math/src/arc.rs @@ -57,7 +57,6 @@ impl Arc { } else { (Scalar::ONE, Scalar::ZERO) }; - let [[x0, y0], [x1, y1]] = [p0, p1].map(|p| p.coords.components); let unit_vector_p0_to_p1 = (p1 - p0) / distance_between_endpoints * uv_factor; let unit_vector_midpoint_to_center = @@ -66,8 +65,14 @@ impl Arc { coords: (p0.coords + p1.coords) / 2. + unit_vector_midpoint_to_center * distance_center_to_midpoint, }; - let start_angle = (y0 - center.v).atan2(x0 - center.u); - let end_angle = (y1 - center.v).atan2(x1 - center.u) + end_angle_offset; + let start_angle = { + let center_to_start = p0 - center; + center_to_start.v.atan2(center_to_start.u) + }; + let end_angle = { + let center_to_end = p1 - center; + center_to_end.v.atan2(center_to_end.u) + end_angle_offset + }; Self { center, radius,