Fine-tune Radians::to_distance
An angle of 0 radians will "point" to the top-center of a `Rectangle` and then increase clockwise.
This commit is contained in:
parent
d2294737c2
commit
90cbab18b9
1 changed files with 8 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{Point, Rectangle, Vector};
|
use crate::{Point, Rectangle, Vector};
|
||||||
|
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::{FRAC_PI_2, PI};
|
||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
/// Degrees
|
/// Degrees
|
||||||
|
|
@ -57,15 +57,16 @@ impl num_traits::FromPrimitive for Radians {
|
||||||
impl Radians {
|
impl Radians {
|
||||||
/// Calculates the line in which the [`Angle`] intercepts the `bounds`.
|
/// Calculates the line in which the [`Angle`] intercepts the `bounds`.
|
||||||
pub fn to_distance(&self, bounds: &Rectangle) -> (Point, Point) {
|
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(
|
let distance_to_rect = f32::max(
|
||||||
f32::abs((bounds.y - bounds.center().y) / v1.y),
|
f32::abs(r.x * bounds.width / 2.0),
|
||||||
f32::abs(((bounds.x + bounds.width) - bounds.center().x) / v1.x),
|
f32::abs(r.y * bounds.height / 2.0),
|
||||||
);
|
);
|
||||||
|
|
||||||
let start = bounds.center() + v1 * distance_to_rect;
|
let start = bounds.center() - r * distance_to_rect;
|
||||||
let end = bounds.center() - v1 * distance_to_rect;
|
let end = bounds.center() + r * distance_to_rect;
|
||||||
|
|
||||||
(start, end)
|
(start, end)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue