Implement Image::translate for positioning floating images
This commit is contained in:
parent
e64c58d032
commit
a2a7c18c9d
3 changed files with 81 additions and 22 deletions
|
|
@ -157,6 +157,30 @@ impl Rectangle<f32> {
|
|||
distance_x.hypot(distance_y)
|
||||
}
|
||||
|
||||
/// Computes the offset that must be applied to the [`Rectangle`] to be placed
|
||||
/// inside the given `container`.
|
||||
pub fn offset(&self, container: &Rectangle) -> Vector {
|
||||
if let Some(intersection) = self.intersection(container) {
|
||||
let left = intersection.x - self.x;
|
||||
let top = intersection.y - self.y;
|
||||
|
||||
Vector::new(
|
||||
if left > 0.0 {
|
||||
left
|
||||
} else {
|
||||
intersection.x + intersection.width - self.x - self.width
|
||||
},
|
||||
if top > 0.0 {
|
||||
top
|
||||
} else {
|
||||
intersection.y + intersection.height - self.y - self.height
|
||||
},
|
||||
)
|
||||
} else {
|
||||
Vector::ZERO
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the current [`Rectangle`] is completely within the given
|
||||
/// `container`.
|
||||
pub fn is_within(&self, container: &Rectangle) -> bool {
|
||||
|
|
@ -268,6 +292,17 @@ impl Rectangle<f32> {
|
|||
|
||||
Self::new(position, size)
|
||||
}
|
||||
|
||||
/// Scales the [`Rectangle`] without changing its position, effectively
|
||||
/// "zooming" it.
|
||||
pub fn zoom(self, zoom: f32) -> Self {
|
||||
Self {
|
||||
x: self.x - (self.width * (zoom - 1.0)) / 2.0,
|
||||
y: self.y - (self.height * (zoom - 1.0)) / 2.0,
|
||||
width: self.width * zoom,
|
||||
height: self.height * zoom,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Mul<f32> for Rectangle<f32> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue