Skip to content

Commit 90cbab1

Browse files
committed
Fine-tune Radians::to_distance
An angle of 0 radians will "point" to the top-center of a `Rectangle` and then increase clockwise.
1 parent d229473 commit 90cbab1

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

core/src/angle.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{Point, Rectangle, Vector};
22

3-
use std::f32::consts::PI;
3+
use std::f32::consts::{FRAC_PI_2, PI};
44
use std::ops::RangeInclusive;
55

66
/// Degrees
@@ -57,15 +57,16 @@ impl num_traits::FromPrimitive for Radians {
5757
impl Radians {
5858
/// Calculates the line in which the [`Angle`] intercepts the `bounds`.
5959
pub fn to_distance(&self, bounds: &Rectangle) -> (Point, Point) {
60-
let v1 = Vector::new(f32::cos(self.0), f32::sin(self.0));
60+
let angle = self.0 - FRAC_PI_2;
61+
let r = Vector::new(f32::cos(angle), f32::sin(angle));
6162

62-
let distance_to_rect = f32::min(
63-
f32::abs((bounds.y - bounds.center().y) / v1.y),
64-
f32::abs(((bounds.x + bounds.width) - bounds.center().x) / v1.x),
63+
let distance_to_rect = f32::max(
64+
f32::abs(r.x * bounds.width / 2.0),
65+
f32::abs(r.y * bounds.height / 2.0),
6566
);
6667

67-
let start = bounds.center() + v1 * distance_to_rect;
68-
let end = bounds.center() - v1 * distance_to_rect;
68+
let start = bounds.center() - r * distance_to_rect;
69+
let end = bounds.center() + r * distance_to_rect;
6970

7071
(start, end)
7172
}

0 commit comments

Comments
 (0)