Skip to content

Commit 221edfb

Browse files
committed
Remove circular dependency: rectangle <-> angle
1 parent 5cafd63 commit 221edfb

4 files changed

Lines changed: 27 additions & 21 deletions

File tree

core/src/angle.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::{Point, Rectangle, Vector};
2-
3-
use std::f32::consts::{FRAC_PI_2, PI};
1+
use std::f32::consts::PI;
42
use std::fmt::Display;
53
use std::ops::{Add, AddAssign, Div, Mul, RangeInclusive, Rem, Sub, SubAssign};
64

@@ -93,22 +91,6 @@ impl Radians {
9391

9492
/// The amount of radians in half a circle.
9593
pub const PI: Self = Self(PI);
96-
97-
/// Calculates the line in which the angle intercepts the `bounds`.
98-
pub fn to_distance(&self, bounds: &Rectangle) -> (Point, Point) {
99-
let angle = self.0 - FRAC_PI_2;
100-
let r = Vector::new(f32::cos(angle), f32::sin(angle));
101-
102-
let distance_to_rect = f32::max(
103-
f32::abs(r.x * bounds.width / 2.0),
104-
f32::abs(r.y * bounds.height / 2.0),
105-
);
106-
107-
let start = bounds.center() - r * distance_to_rect;
108-
let end = bounds.center() + r * distance_to_rect;
109-
110-
(start, end)
111-
}
11294
}
11395

11496
impl From<Degrees> for Radians {

core/src/rectangle.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,30 @@ impl Rectangle<f32> {
337337

338338
Point::new(x, y)
339339
}
340+
341+
/// Returns the two endpoints of a chord through the rectangle's center
342+
/// at the given `angle`.
343+
///
344+
/// The angle is measured clockwise from the negative y-axis, so `0`
345+
/// produces a vertical chord and `π/2` a horizontal one. The returned
346+
/// points are `(start, end)`, symmetric about [`Rectangle::center`].
347+
pub fn chord(&self, angle: impl Into<Radians>) -> (Point, Point) {
348+
use std::f32::consts::FRAC_PI_2;
349+
350+
let angle = angle.into().0 - FRAC_PI_2;
351+
let r = Vector::new(f32::cos(angle), f32::sin(angle));
352+
353+
let distance_to_rect = f32::max(
354+
f32::abs(r.x * self.width / 2.0),
355+
f32::abs(r.y * self.height / 2.0),
356+
);
357+
358+
let center = self.center();
359+
let start = center - r * distance_to_rect;
360+
let end = center + r * distance_to_rect;
361+
362+
(start, end)
363+
}
340364
}
341365

342366
impl std::ops::Mul<f32> for Rectangle<f32> {

graphics/src/gradient.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed {
160160
pack_f16s([offsets[6], offsets[7]]),
161161
];
162162

163-
let (start, end) = linear.angle.to_distance(&bounds);
163+
let (start, end) = bounds.chord(linear.angle);
164164

165165
let direction = [start.x, start.y, end.x, end.y];
166166

tiny_skia/src/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl Engine {
136136
shader: match background {
137137
Background::Color(color) => tiny_skia::Shader::SolidColor(into_color(*color)),
138138
Background::Gradient(Gradient::Linear(linear)) => {
139-
let (start, end) = linear.angle.to_distance(&quad.bounds);
139+
let (start, end) = quad.bounds.chord(linear.angle);
140140

141141
let stops: Vec<tiny_skia::GradientStop> = linear
142142
.stops

0 commit comments

Comments
 (0)