Skip to content

Commit ccbbae3

Browse files
committed
Merge branch 'main' of github.com:khonsulabs/figures
2 parents 5a65a8f + d811eb1 commit ccbbae3

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- `wgpu` has been updated to `24.0.1`.
1313

14+
### Added
15+
16+
- Functions to get the four corners of a `Rect`.
17+
1418
## v0.5.0 (2024-11-19)
1519

1620
### Breaking Changes

src/rect.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<Unit> Rect<Unit> {
9595
/// # Errors
9696
///
9797
/// Returns `<NewUnit as TryFrom>::Error` when the inner type cannot be
98-
/// converted. For this crate's types, this genenerally will be
98+
/// converted. For this crate's types, this generally will be
9999
pub fn try_cast<NewUnit>(self) -> Result<Rect<NewUnit>, NewUnit::Error>
100100
where
101101
NewUnit: TryFrom<Unit>,
@@ -191,24 +191,53 @@ impl<Unit> Rect<Unit> {
191191
/// Returns the non-origin point.
192192
pub fn extent(&self) -> Point<Unit>
193193
where
194-
Unit: crate::Unit,
194+
Unit: Add<Output = Unit> + Copy,
195195
{
196196
self.origin + self.size
197197
}
198198
}
199199

200200
impl<Unit> Rect<Unit>
201201
where
202+
// alternatively we could reduce the traits for `extent()`
202203
Unit: Add<Output = Unit> + Ord + Copy,
203204
{
204205
/// Returns the top-left and bottom-right points of this rectangle.
205206
///
206-
/// The first point returned will always be the top-right point, even if the size of the rectangle is negative.
207+
/// The first point returned will always be the top-left point, even if the size of the rectangle is negative.
207208
pub fn extents(&self) -> (Point<Unit>, Point<Unit>) {
208-
let extent = self.origin + self.size;
209-
(
210-
Point::new(self.origin.x.min(extent.x), self.origin.y.min(extent.y)),
211-
Point::new(self.origin.x.max(extent.x), self.origin.y.max(extent.y)),
209+
(self.top_left(), self.bottom_right())
210+
}
211+
212+
/// Returns the top-left corner of this rectangle.
213+
pub fn top_left(&self) -> Point<Unit> {
214+
Point::new(
215+
self.origin.x.min(self.extent().x),
216+
self.origin.y.min(self.extent().y),
217+
)
218+
}
219+
220+
/// Returns the top-right corner of this rectangle.
221+
pub fn top_right(&self) -> Point<Unit> {
222+
Point::new(
223+
self.origin.x.max(self.extent().x),
224+
self.origin.y.min(self.extent().y),
225+
)
226+
}
227+
228+
/// Returns the bottom-left corner of this rectangle.
229+
pub fn bottom_left(&self) -> Point<Unit> {
230+
Point::new(
231+
self.origin.x.min(self.extent().x),
232+
self.origin.y.max(self.extent().y),
233+
)
234+
}
235+
236+
/// Returns the bottom-right corner of this rectangle.
237+
pub fn bottom_right(&self) -> Point<Unit> {
238+
Point::new(
239+
self.origin.x.max(self.extent().x),
240+
self.origin.y.max(self.extent().y),
212241
)
213242
}
214243
}

src/traits.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::Fraction;
88

99
/// Converts a type to its floating point representation.
1010
///
11-
/// This trait exists because there is no trait in Rust to peform `x as f32`.
11+
/// This trait exists because there is no trait in Rust to perform `x as f32`.
1212
pub trait FloatConversion {
1313
/// The type that represents this type in floating point form.
1414
type Float;
@@ -332,7 +332,7 @@ pub trait ScreenScale {
332332
/// Converts this value from its current unit into device independent pixels
333333
/// ([`Lp`](crate::units::Lp)) using the provided `scale` factor.
334334
fn into_lp(self, scale: Fraction) -> Self::Lp;
335-
/// Converts from Lp into this type, using the provided `scale` factor.
335+
/// Converts from [`Lp`](crate::units::Lp) into this type, using the provided `scale` factor.
336336
fn from_lp(lp: Self::Lp, scale: Fraction) -> Self;
337337
}
338338

@@ -560,7 +560,7 @@ impl PixelScaling for Lp {
560560

561561
/// Information about scaling for a numerical unit type.
562562
pub trait UnscaledUnit {
563-
/// The internal reprsentation used by this type.
563+
/// The internal representation used by this type.
564564
type Representation: CastInto<i32>;
565565

566566
/// Returns a new instance using the unscaled representation.

0 commit comments

Comments
 (0)