1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
//! Traits for aligning geometric objects.
use serde::{Deserialize, Serialize};
use crate::{
bbox::Bbox,
point::Point,
rect::Rect,
transform::{Translate, TranslateMut},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
/// An enumeration of possible alignment modes between two geometric shapes.
pub enum AlignMode {
/// Align the left sides of the two shapes.
Left,
/// Align the right sides of the two shapes.
Right,
/// Align the bottom sides of the two shapes.
Bottom,
/// Align the top sides of the two shapes.
Top,
/// Align the centers of the two shapes horizontally.
CenterHorizontal,
/// Align the centers of the two shapes vertically.
CenterVertical,
/// Align the left side of one shape to the right of
/// the right side of the other.
ToTheRight,
/// Align the right side of one shape to the left of
/// the left side of the other.
ToTheLeft,
/// Align the top side of one shape beneath
/// the bottom side of the other.
Beneath,
/// Align the bottom side of one shape above
/// the top side of the other.
Above,
}
/// A geometric shape that can be aligned using the relationship between two [`Rect`]s.
///
/// # Examples
///
/// ```
/// # use geometry::prelude::*;
/// # use geometry::align::AlignRectMut;
/// let mut rect1 = Rect::from_sides(0, 0, 100, 200);
/// let rect2 = Rect::from_sides(500, 600, 700, 700);
/// rect1.align_mut(AlignMode::Left, rect1, rect2, 0);
/// assert_eq!(rect1.left(), rect2.left());
/// assert_eq!(rect1, Rect::from_sides(500, 0, 600, 200));
///
/// // Alternate rectangle to align `rect1` with.
/// // Conceptually, this represents that `rect1` has
/// // 5 units of hangover space on all sides that should
/// // not contribute to alignment.
/// let rect1_alt = rect1.shrink_all(5).unwrap();
/// rect1.align_mut(AlignMode::Left, rect1_alt, rect2, 0);
/// assert_eq!(rect1, Rect::from_sides(495, 0, 595, 200));
/// ```
pub trait AlignRectMut: TranslateMut {
/// Align `self` based on the relationship between `srect` and `orect`.
///
/// `offset` represents an offset from the base alignment in the positive direction
/// along the alignment axis.
///
/// For center alignments, if the centers are a non-integer number of units apart,
/// the translation amount is rounded down to the nearest integer. This behavior is subject
/// to change and should not be relied upon.
fn align_mut(&mut self, mode: AlignMode, srect: Rect, orect: Rect, offset: i64) {
match mode {
AlignMode::Left => {
self.translate_mut(Point::new(orect.left() - srect.left() + offset, 0));
}
AlignMode::Right => {
self.translate_mut(Point::new(orect.right() - srect.right() + offset, 0));
}
AlignMode::Bottom => {
self.translate_mut(Point::new(0, orect.bot() - srect.bot() + offset));
}
AlignMode::Top => {
self.translate_mut(Point::new(0, orect.top() - srect.top() + offset));
}
AlignMode::ToTheRight => {
self.translate_mut(Point::new(orect.right() - srect.left() + offset, 0));
}
AlignMode::ToTheLeft => {
self.translate_mut(Point::new(orect.left() - srect.right() + offset, 0));
}
AlignMode::CenterHorizontal => {
self.translate_mut(Point::new(
((orect.left() + orect.right()) - (srect.left() + srect.right())) / 2 + offset,
0,
));
}
AlignMode::CenterVertical => {
self.translate_mut(Point::new(
0,
((orect.bot() + orect.top()) - (srect.bot() + srect.top())) / 2 + offset,
));
}
AlignMode::Beneath => {
self.translate_mut(Point::new(0, orect.bot() - srect.top() + offset));
}
AlignMode::Above => {
self.translate_mut(Point::new(0, orect.top() - srect.bot() + offset));
}
}
}
}
impl<T: Translate> AlignRectMut for T {}
/// A geometric shape that can be aligned using the relationship between two [`Rect`]s.
///
/// Takes in an owned copy of the shape and returns the aligned version.
///
/// # Examples
///
/// ```
/// # use geometry::prelude::*;
/// let mut rect1 = Rect::from_sides(0, 0, 100, 200);
/// let rect2 = Rect::from_sides(500, 600, 700, 700);
/// let rect1 = rect1.align(AlignMode::Left, rect1, rect2, 0);
/// assert_eq!(rect1.left(), rect2.left());
/// assert_eq!(rect1, Rect::from_sides(500, 0, 600, 200));
///
/// // Alternate rectangle to align `rect1` with.
/// // Conceptually, this represents that `rect1` has
/// // 5 units of hangover space on all sides that should
/// // not contribute to alignment.
/// let rect1_alt = rect1.shrink_all(5).unwrap();
/// assert_eq!(rect1.align(AlignMode::Left, rect1_alt, rect2, 0), Rect::from_sides(495, 0, 595, 200));
/// ```
pub trait AlignRect: AlignRectMut + Sized {
/// Align `self` based on the relationship between `srect` and `orect`.
///
/// `offset` represents an offset from the base alignment in the positive direction
/// along the alignment axis.
///
/// For center alignments, if the centers are a non-integer number of units apart,
/// the translation amount is rounded down to the nearest integer. This behavior is subject
/// to change and should not be relied upon.
///
/// Creates a new shape at the aligned location of the original.
fn align(mut self, mode: AlignMode, srect: Rect, orect: Rect, offset: i64) -> Self {
self.align_mut(mode, srect, orect, offset);
self
}
}
impl<T: AlignRectMut + Sized> AlignRect for T {}
/// A geometric shape that can be aligned with another shape using their bounding boxes.
///
/// # Examples
///
/// ```
/// # use geometry::prelude::*;
/// # use geometry::align::AlignBboxMut;
/// let mut rect1 = Rect::from_sides(0, 0, 100, 200);
/// let rect2 = Rect::from_sides(500, 600, 700, 700);
/// rect1.align_bbox_mut(AlignMode::Left, rect2, 0);
/// assert_eq!(rect1.left(), rect2.left());
/// assert_eq!(rect1, Rect::from_sides(500, 0, 600, 200));
/// ```
pub trait AlignBboxMut: AlignRectMut + Bbox {
/// Align `self` using its bounding box and the bounding box of `other`.
///
/// `offset` represents an offset from the base alignment in the positive direction
/// along the alignment axis.
///
/// For center alignments, if the centers are a non-integer number of units apart,
/// the translation amount is rounded down to the nearest integer. This behavior is subject
/// to change and should not be relied upon.
fn align_bbox_mut(&mut self, mode: AlignMode, other: impl Bbox, offset: i64) {
self.align_mut(mode, self.bbox().unwrap(), other.bbox().unwrap(), offset);
}
}
impl<T: AlignRectMut + Bbox> AlignBboxMut for T {}
/// A geometric shape that can be aligned with another shape using their bounding boxes.
///
/// Takes in an owned copy of the shape and returns the aligned version.
///
/// # Examples
///
/// ```
/// # use geometry::prelude::*;
/// let mut rect1 = Rect::from_sides(0, 0, 100, 200);
/// let rect2 = Rect::from_sides(500, 600, 700, 700);
/// let rect1 = rect1.align_bbox(AlignMode::Left, rect2, 0);
/// assert_eq!(rect1.left(), rect2.left());
/// assert_eq!(rect1, Rect::from_sides(500, 0, 600, 200));
/// ```
pub trait AlignBbox: AlignBboxMut + Sized {
/// Align `self` using its bounding box and the bounding box of `other`.
///
/// `offset` represents an offset from the base alignment in the positive direction
/// along the alignment axis.
///
/// For center alignments, if the centers are a non-integer number of units apart,
/// the translation amount is rounded down to the nearest integer. This behavior is subject
/// to change and should not be relied upon.
///
/// Creates a new shape at the aligned location of the original.
fn align_bbox(mut self, mode: AlignMode, other: impl Bbox, offset: i64) -> Self {
self.align_bbox_mut(mode, other, offset);
self
}
}
impl<T: AlignBboxMut + Sized> AlignBbox for T {}
#[cfg(test)]
mod tests {
use crate::{
align::{AlignBboxMut, AlignMode, AlignRectMut},
rect::Rect,
};
#[test]
fn align_and_align_bbox_work() {
let mut rect1 = Rect::from_sides(0, 0, 100, 200);
let mut rect1_bbox = Rect::from_sides(0, 0, 100, 200);
let rect2 = Rect::from_sides(500, 600, 700, 700);
rect1.align_mut(AlignMode::Left, rect1, rect2, 0);
rect1_bbox.align_bbox_mut(AlignMode::Left, rect2, 0);
assert_eq!(rect1, Rect::from_sides(500, 0, 600, 200));
assert_eq!(rect1_bbox, Rect::from_sides(500, 0, 600, 200));
rect1.align_mut(AlignMode::Right, rect1, rect2, 0);
rect1_bbox.align_bbox_mut(AlignMode::Right, rect2, 0);
assert_eq!(rect1, Rect::from_sides(600, 0, 700, 200));
assert_eq!(rect1_bbox, Rect::from_sides(600, 0, 700, 200));
rect1.align_mut(AlignMode::Bottom, rect1, rect2, 0);
rect1_bbox.align_bbox_mut(AlignMode::Bottom, rect2, 0);
assert_eq!(rect1, Rect::from_sides(600, 600, 700, 800));
assert_eq!(rect1_bbox, Rect::from_sides(600, 600, 700, 800));
rect1.align_mut(AlignMode::Top, rect1, rect2, 0);
rect1_bbox.align_bbox_mut(AlignMode::Top, rect2, 0);
assert_eq!(rect1, Rect::from_sides(600, 500, 700, 700));
assert_eq!(rect1_bbox, Rect::from_sides(600, 500, 700, 700));
rect1.align_mut(AlignMode::ToTheLeft, rect1, rect2, 0);
rect1_bbox.align_bbox_mut(AlignMode::ToTheLeft, rect2, 0);
assert_eq!(rect1, Rect::from_sides(400, 500, 500, 700));
assert_eq!(rect1_bbox, Rect::from_sides(400, 500, 500, 700));
rect1.align_mut(AlignMode::ToTheRight, rect1, rect2, 0);
rect1_bbox.align_bbox_mut(AlignMode::ToTheRight, rect2, 0);
assert_eq!(rect1, Rect::from_sides(700, 500, 800, 700));
assert_eq!(rect1_bbox, Rect::from_sides(700, 500, 800, 700));
rect1.align_mut(AlignMode::Beneath, rect1, rect2, 0);
rect1_bbox.align_bbox_mut(AlignMode::Beneath, rect2, 0);
assert_eq!(rect1, Rect::from_sides(700, 400, 800, 600));
assert_eq!(rect1_bbox, Rect::from_sides(700, 400, 800, 600));
rect1.align_mut(AlignMode::Above, rect1, rect2, 0);
rect1_bbox.align_bbox_mut(AlignMode::Above, rect2, 0);
assert_eq!(rect1, Rect::from_sides(700, 700, 800, 900));
assert_eq!(rect1_bbox, Rect::from_sides(700, 700, 800, 900));
rect1.align_mut(AlignMode::CenterHorizontal, rect1, rect2, 0);
rect1_bbox.align_bbox_mut(AlignMode::CenterHorizontal, rect2, 0);
assert_eq!(rect1, Rect::from_sides(550, 700, 650, 900));
assert_eq!(rect1_bbox, Rect::from_sides(550, 700, 650, 900));
rect1.align_mut(AlignMode::CenterVertical, rect1, rect2, 0);
rect1_bbox.align_bbox_mut(AlignMode::CenterVertical, rect2, 0);
assert_eq!(rect1, Rect::from_sides(550, 550, 650, 750));
assert_eq!(rect1_bbox, Rect::from_sides(550, 550, 650, 750));
}
}