Skip to content

Commit

Permalink
Fine-tune Radians::to_distance
Browse files Browse the repository at this point in the history
An angle of 0 radians will "point" to the top-center of a `Rectangle` and
then increase clockwise.
  • Loading branch information
hecrj committed Sep 8, 2023
1 parent d229473 commit 90cbab1
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions core/src/angle.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 90cbab1

Please sign in to comment.