diff options
author | 2023-09-08 02:36:17 +0200 | |
---|---|---|
committer | 2023-09-08 02:36:17 +0200 | |
commit | 90cbab18b95a2a90a6a527280a6ca461203ea1b3 (patch) | |
tree | 3d1c109d1e92f822cb841797788cccf55ce8b09c | |
parent | d2294737c2e28b3b3050d7bf820bbca09896b00e (diff) | |
download | iced-90cbab18b95a2a90a6a527280a6ca461203ea1b3.tar.gz iced-90cbab18b95a2a90a6a527280a6ca461203ea1b3.tar.bz2 iced-90cbab18b95a2a90a6a527280a6ca461203ea1b3.zip |
Fine-tune `Radians::to_distance`
An angle of 0 radians will "point" to the top-center of a `Rectangle` and
then increase clockwise.
-rw-r--r-- | core/src/angle.rs | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/core/src/angle.rs b/core/src/angle.rs index 91fc2ba7..c8f3f013 100644 --- a/core/src/angle.rs +++ b/core/src/angle.rs @@ -1,6 +1,6 @@ use crate::{Point, Rectangle, Vector}; -use std::f32::consts::PI; +use std::f32::consts::{FRAC_PI_2, PI}; use std::ops::RangeInclusive; /// Degrees @@ -57,15 +57,16 @@ impl num_traits::FromPrimitive for Radians { impl Radians { /// Calculates the line in which the [`Angle`] intercepts the `bounds`. pub fn to_distance(&self, bounds: &Rectangle) -> (Point, Point) { - let v1 = Vector::new(f32::cos(self.0), f32::sin(self.0)); + let angle = self.0 - FRAC_PI_2; + let r = Vector::new(f32::cos(angle), f32::sin(angle)); - let distance_to_rect = f32::min( - f32::abs((bounds.y - bounds.center().y) / v1.y), - f32::abs(((bounds.x + bounds.width) - bounds.center().x) / v1.x), + let distance_to_rect = f32::max( + f32::abs(r.x * bounds.width / 2.0), + f32::abs(r.y * bounds.height / 2.0), ); - let start = bounds.center() + v1 * distance_to_rect; - let end = bounds.center() - v1 * distance_to_rect; + let start = bounds.center() - r * distance_to_rect; + let end = bounds.center() + r * distance_to_rect; (start, end) } |