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
//! Traits for placing a geometric object at a point.
use serde::{Deserialize, Serialize};
use crate::{
bbox::Bbox, corner::Corner, point::Point, rect::Rect, side::Side, transform::TranslateMut,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
/// An enumeration of possible ways to place a geometric shape at a point.
pub enum PlaceMode {
/// Place the corner of a geometric shape at a point.
Corner(Corner),
/// Place the side of a geometric shape at a point's coordinate along the same axis.
Side(Side),
/// Place the center of a side of a geometric shape at a point.
SideCenter(Side),
/// Place the center of a geometric shape at a point.
Center,
/// Place the x-coordinate of a geometric shape's center at the x-coordinate of a point.
CenterX,
/// Place the y-coordinate of a geometric shape's center at the y-coordinate of a point.
CenterY,
}
/// A geometric shape that can be placed at a point.
///
/// # Examples
///
/// ```
/// # use geometry::prelude::*;
/// # use geometry::place::PlaceRectMut;
/// let mut rect1 = Rect::from_sides(0, 0, 100, 200);
/// rect1.place_mut(PlaceMode::Center, rect1, Point::new(25, 25));
/// assert_eq!(rect1, Rect::from_sides(-25, -75, 75, 125));
///
/// // 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 placement.
/// let rect1_alt = rect1.shrink_all(5).unwrap();
/// rect1.place_mut(PlaceMode::Corner(Corner::UpperRight), rect1_alt, Point::new(25, 25));
/// assert_eq!(rect1, Rect::from_sides(-70, -170, 30, 30));
/// ```
pub trait PlaceRectMut: TranslateMut {
/// Places an object at the given point.
///
/// For center alignments, the center's non-integer coordinates are rounded down to the nearest integer.
/// This behavior is subject to change and should not be relied upon.
fn place_mut(&mut self, mode: PlaceMode, srect: Rect, pt: Point) {
match mode {
PlaceMode::Corner(corner) => {
let ofs = pt - srect.corner(corner);
self.translate_mut(ofs);
}
PlaceMode::Side(side) => {
let dir_ofs = side.coord_dir();
let ofs = pt.coord(dir_ofs) - srect.side(side);
self.translate_mut(Point::from_dir_coords(dir_ofs, ofs, 0));
}
PlaceMode::SideCenter(side) => {
let edge = srect.edge(side);
let center =
Point::from_dir_coords(side.coord_dir(), edge.coord(), edge.span().center());
let ofs = pt - center;
self.translate_mut(ofs);
}
PlaceMode::Center => {
let ofs = pt - srect.center();
self.translate_mut(ofs);
}
PlaceMode::CenterX => {
let ofs = pt.x - srect.center().x;
self.translate_mut(Point::new(ofs, 0));
}
PlaceMode::CenterY => {
let ofs = pt.y - srect.center().y;
self.translate_mut(Point::new(0, ofs));
}
}
}
}
impl<T: TranslateMut> PlaceRectMut for T {}
/// A geometric shape that can be placed at a point.
///
/// # Examples
///
/// ```
/// # use geometry::prelude::*;
/// let mut rect1 = Rect::from_sides(0, 0, 100, 200);
/// assert_eq!(
/// rect1.place(PlaceMode::Center, rect1, Point::new(25, 25)),
/// Rect::from_sides(-25, -75, 75, 125),
/// );
///
/// // 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 placement.
/// let rect1_alt = rect1.shrink_all(5).unwrap();
///
/// assert_eq!(
/// rect1.place(
/// PlaceMode::Corner(Corner::UpperRight),
/// rect1_alt,
/// Point::new(25, 25)
/// ),
/// Rect::from_sides(-70, -170, 30, 30),
/// );
/// ```
pub trait PlaceRect: PlaceRectMut + Sized {
/// Places an object at the given point.
///
/// For center alignments, the center's non-integer coordinates are rounded down to the nearest integer.
/// This behavior is subject to change and should not be relied upon.
///
/// Creates a new shape at the placed location.
fn place(mut self, mode: PlaceMode, srect: Rect, pt: Point) -> Self {
self.place_mut(mode, srect, pt);
self
}
}
impl<T: PlaceRectMut + Sized> PlaceRect for T {}
/// A geometric shape that can be placed at a point using its bounding box.
///
/// # Examples
///
/// ```
/// # use geometry::prelude::*;
/// # use geometry::place::PlaceBboxMut;
/// let mut rect1 = Rect::from_sides(0, 0, 100, 200);
/// rect1.place_bbox_mut(PlaceMode::Center, Point::new(25, 25));
/// assert_eq!(rect1, Rect::from_sides(-25, -75, 75, 125));
/// ```
pub trait PlaceBboxMut: PlaceRectMut + Bbox {
/// Places an object at the given point using its bounding box.
///
/// For center alignments, the center's non-integer coordinates are rounded down to the nearest integer.
/// This behavior is subject to change and should not be relied upon.
fn place_bbox_mut(&mut self, mode: PlaceMode, pt: Point) {
self.place_mut(mode, self.bbox().unwrap(), pt)
}
}
impl<T: PlaceRectMut + Bbox> PlaceBboxMut for T {}
/// A geometric shape that can be placed at a point using its bounding box.
///
/// # Examples
///
/// ```
/// # use geometry::prelude::*;
/// let mut rect1 = Rect::from_sides(0, 0, 100, 200);
/// assert_eq!(
/// rect1.place_bbox(PlaceMode::Center, Point::new(25, 25)),
/// Rect::from_sides(-25, -75, 75, 125),
/// );
/// ```
pub trait PlaceBbox: PlaceBboxMut + Sized {
/// Places an object at the given point using its boudning box.
///
/// For center alignments, the center's non-integer coordinates are rounded down to the nearest integer.
/// This behavior is subject to change and should not be relied upon.
///
/// Creates a new shape at the placed location.
fn place_bbox(mut self, mode: PlaceMode, pt: Point) -> Self {
self.place_bbox_mut(mode, pt);
self
}
}
impl<T: PlaceBboxMut + Sized> PlaceBbox for T {}
#[cfg(test)]
mod tests {
use crate::{
corner::Corner,
place::{PlaceBbox, PlaceMode, PlaceRect},
point::Point,
rect::Rect,
side::Side,
};
#[test]
fn place_and_place_bbox_work() {
let rect1 = Rect::from_sides(0, 0, 100, 200);
let pt = Point::new(75, 75);
assert_eq!(
rect1.place(PlaceMode::Corner(Corner::UpperLeft), rect1, pt),
Rect::from_sides(75, -125, 175, 75)
);
assert_eq!(
rect1.place_bbox(PlaceMode::Corner(Corner::UpperLeft), pt),
Rect::from_sides(75, -125, 175, 75)
);
assert_eq!(
rect1.place(PlaceMode::Side(Side::Right), rect1, pt),
Rect::from_sides(-25, 0, 75, 200)
);
assert_eq!(
rect1.place_bbox(PlaceMode::Side(Side::Right), pt),
Rect::from_sides(-25, 0, 75, 200)
);
assert_eq!(
rect1.place(PlaceMode::SideCenter(Side::Left), rect1, pt),
Rect::from_sides(75, -25, 175, 175)
);
assert_eq!(
rect1.place_bbox(PlaceMode::SideCenter(Side::Left), pt),
Rect::from_sides(75, -25, 175, 175)
);
assert_eq!(
rect1.place(PlaceMode::Center, rect1, pt),
Rect::from_sides(25, -25, 125, 175)
);
assert_eq!(
rect1.place_bbox(PlaceMode::Center, pt),
Rect::from_sides(25, -25, 125, 175)
);
assert_eq!(
rect1.place(PlaceMode::CenterX, rect1, pt),
Rect::from_sides(25, 0, 125, 200)
);
assert_eq!(
rect1.place_bbox(PlaceMode::CenterX, pt),
Rect::from_sides(25, 0, 125, 200)
);
assert_eq!(
rect1.place(PlaceMode::CenterY, rect1, pt),
Rect::from_sides(0, -25, 100, 175)
);
assert_eq!(
rect1.place_bbox(PlaceMode::CenterY, pt),
Rect::from_sides(0, -25, 100, 175)
);
}
}