diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/src/rectangle.rs | 14 | ||||
-rw-r--r-- | core/src/time.rs | 25 |
2 files changed, 39 insertions, 0 deletions
diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index cff33991..14d2a2e8 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -143,6 +143,20 @@ impl Rectangle<f32> { && point.y < self.y + self.height } + /// Returns the minimum distance from the given [`Point`] to any of the edges + /// of the [`Rectangle`]. + pub fn distance(&self, point: Point) -> f32 { + let center = self.center(); + + let distance_x = + ((point.x - center.x).abs() - self.width / 2.0).max(0.0); + + let distance_y = + ((point.y - center.y).abs() - self.height / 2.0).max(0.0); + + distance_x.hypot(distance_y) + } + /// Returns true if the current [`Rectangle`] is completely within the given /// `container`. pub fn is_within(&self, container: &Rectangle) -> bool { diff --git a/core/src/time.rs b/core/src/time.rs index dcfe4e41..c6e30444 100644 --- a/core/src/time.rs +++ b/core/src/time.rs @@ -2,3 +2,28 @@ pub use web_time::Duration; pub use web_time::Instant; + +/// Creates a [`Duration`] representing the given amount of milliseconds. +pub fn milliseconds(milliseconds: u64) -> Duration { + Duration::from_millis(milliseconds) +} + +/// Creates a [`Duration`] representing the given amount of seconds. +pub fn seconds(seconds: u64) -> Duration { + Duration::from_secs(seconds) +} + +/// Creates a [`Duration`] representing the given amount of minutes. +pub fn minutes(minutes: u64) -> Duration { + seconds(minutes * 60) +} + +/// Creates a [`Duration`] representing the given amount of hours. +pub fn hours(hours: u64) -> Duration { + minutes(hours * 60) +} + +/// Creates a [`Duration`] representing the given amount of days. +pub fn days(days: u64) -> Duration { + hours(days * 24) +} |