Skip to content

Commit 1f024b2

Browse files
committed
Fix Rectangle::offset for non-overlapping rectangles
1 parent bb22add commit 1f024b2

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

core/src/rectangle.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,20 @@ impl Rectangle<f32> {
161161
/// Computes the offset that must be applied to the [`Rectangle`] to be placed
162162
/// inside the given `container`.
163163
pub fn offset(&self, container: &Rectangle) -> Vector {
164-
let Some(intersection) = self.intersection(container) else {
165-
return Vector::ZERO;
166-
};
167-
168-
let left = intersection.x - self.x;
169-
let top = intersection.y - self.y;
170-
171164
Vector::new(
172-
if left > 0.0 {
173-
left
165+
if container.x > self.x {
166+
container.x - self.x
167+
} else if self.x + self.width > container.x + container.width {
168+
container.x + container.width - self.x - self.width
174169
} else {
175-
intersection.x + intersection.width - self.x - self.width
170+
0.0
176171
},
177-
if top > 0.0 {
178-
top
172+
if container.y > self.y {
173+
container.y - self.y
174+
} else if self.y + self.height > container.y + container.height {
175+
container.y + container.height - self.y - self.height
179176
} else {
180-
intersection.y + intersection.height - self.y - self.height
177+
0.0
181178
},
182179
)
183180
}

0 commit comments

Comments
 (0)