|
1 | 1 | use crate::{Point, Rectangle, Vector}; |
2 | 2 |
|
3 | | -use std::f32::consts::PI; |
| 3 | +use std::f32::consts::{FRAC_PI_2, PI}; |
4 | 4 | use std::ops::RangeInclusive; |
5 | 5 |
|
6 | 6 | /// Degrees |
@@ -57,15 +57,16 @@ impl num_traits::FromPrimitive for Radians { |
57 | 57 | impl Radians { |
58 | 58 | /// Calculates the line in which the [`Angle`] intercepts the `bounds`. |
59 | 59 | 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)); |
61 | 62 |
|
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), |
65 | 66 | ); |
66 | 67 |
|
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; |
69 | 70 |
|
70 | 71 | (start, end) |
71 | 72 | } |
|
0 commit comments