Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand/shrink rectangle functions #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,47 @@ impl<T: Copy> Rectangle<T>
}
}

impl<T: Copy + std::ops::Add<Output = T> + std::ops::Sub<Output = T>> Rectangle<T>
{
/// Shifts all the vertices towards the center of the rectangle by the
/// specified 'x' and 'y' amount.
#[inline]
pub fn shrink(&mut self, vector: impl Copy + Into<Vector2<T>>)
{
let offset = vector.into();
self.top_left = self.top_left + offset;
self.bottom_right = self.bottom_right + offset;
}

/// Returns a new rectangle, whose vertices are shifted towards the center
/// by the specified 'x' and 'y' amount.
#[inline]
pub fn shrunk(&self, vector: impl Into<Vector2<T>>) -> Self
{
let offset = vector.into();
Rectangle::new(self.top_left + offset, self.bottom_right - offset)
}

/// Shifts all the vertices away from the center of the rectangle by the
/// specified 'x' and 'y' amount.
#[inline]
pub fn expand(&mut self, vector: impl Copy + Into<Vector2<T>>)
{
let offset = vector.into();
self.top_left = self.top_left - offset;
self.bottom_right = self.bottom_right - offset;
}

/// Returns a new rectangle, whose vertices are shifted away from the center
/// by the specified 'x' and 'y' amount.
#[inline]
pub fn expanded(&self, vector: impl Into<Vector2<T>>) -> Self
{
let offset = vector.into();
Rectangle::new(self.top_left - offset, self.bottom_right + offset)
}
}

impl<T: std::ops::Sub<Output = T> + Copy> Rectangle<T>
{
/// Returns the width of the rectangle.
Expand Down