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
// Copyright 2019 The kurbo Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! A description of the distances between the edges of two rectangles. use core::ops::{Add, Neg, Sub}; //// ////use std::ops::{Add, Neg, Sub}; use crate::Rect; /// Insets from the edges of a rectangle. /// /// /// The inset value for each edge can be thought of as a delta computed from /// the center of the rect to that edge. For instance, with an inset of `2.0` on /// the x-axis, a rectange with the origin `(0.0, 0.0)` with that inset added /// will have the new origin at `(-2.0, 0.0)`. /// /// Put alternatively, a positive inset represents increased distance from center, /// and a negative inset represents decreased distance from center. /// /// # Examples /// /// Positive insets added to a [`Rect`] produce a larger [`Rect`]: /// ``` /// # use kurbo::{Insets, Rect}; /// let rect = Rect::from_origin_size((0., 0.,), (10., 10.,)); /// let insets = Insets::uniform_xy(3., 0.,); /// /// let inset_rect = rect + insets; /// assert_eq!(inset_rect.width(), 16.0, "10.0 + 3.0 × 2"); /// assert_eq!(inset_rect.x0, -3.0); /// ``` /// /// Negative insets added to a [`Rect`] produce a smaller [`Rect`]: /// /// ``` /// # use kurbo::{Insets, Rect}; /// let rect = Rect::from_origin_size((0., 0.,), (10., 10.,)); /// let insets = Insets::uniform_xy(-3., 0.,); /// /// let inset_rect = rect + insets; /// assert_eq!(inset_rect.width(), 4.0, "10.0 - 3.0 × 2"); /// assert_eq!(inset_rect.x0, 3.0); /// ``` /// /// [`Insets`] operate on the absolute rectangle [`Rect::abs`], and so ignore /// existing negative widths and heights. /// /// ``` /// # use kurbo::{Insets, Rect}; /// let rect = Rect::new(7., 11., 0., 0.,); /// let insets = Insets::uniform_xy(0., 1.,); /// /// assert_eq!(rect.width(), -7.0); /// /// let inset_rect = rect + insets; /// assert_eq!(inset_rect.width(), 7.0); /// assert_eq!(inset_rect.x0, 0.0); /// assert_eq!(inset_rect.height(), 13.0); /// ``` /// /// The width and height of an inset operation can still be negative if the /// [`Insets`]' dimensions are greater than the dimensions of the original [`Rect`]. /// /// ``` /// # use kurbo::{Insets, Rect}; /// let rect = Rect::new(0., 0., 3., 5.); /// let insets = Insets::uniform_xy(0., 7.,); /// /// let inset_rect = rect - insets; /// assert_eq!(inset_rect.height(), -9., "5 - 7 × 2") /// ``` /// /// `Rect - Rect = Insets`: /// /// /// ``` /// # use kurbo::{Insets, Rect}; /// let rect = Rect::new(0., 0., 5., 11.); /// let insets = Insets::uniform_xy(1., 7.,); /// /// let inset_rect = rect + insets; /// let insets2 = inset_rect - rect; /// /// assert_eq!(insets2.x0, insets.x0); /// assert_eq!(insets2.y1, insets.y1); /// assert_eq!(insets2.x_value(), insets.x_value()); /// assert_eq!(insets2.y_value(), insets.y_value()); /// ``` /// /// [`Rect`]: struct.Rect.html /// [`Insets`]: struct.Insets.html /// [`Rect::abs`]: struct.Rect.html#method.abs #[derive(Clone, Copy, Default, Debug)] pub struct Insets { /// The minimum x coordinate (left edge). pub x0: f64, /// The minimum y coordinate (top edge in y-down spaces). pub y0: f64, /// The maximum x coordinate (right edge). pub x1: f64, /// The maximum y coordinate (bottom edge in y-down spaces). pub y1: f64, } impl Insets { /// Zero'd insets. pub const ZERO: Insets = Insets::uniform(0.); /// New uniform insets. #[inline] pub const fn uniform(d: f64) -> Insets { Insets { x0: d, y0: d, x1: d, y1: d, } } /// New insets with uniform values along each axis. #[inline] pub const fn uniform_xy(x: f64, y: f64) -> Insets { Insets { x0: x, y0: y, x1: x, y1: y, } } /// New insets. The ordering of the arguments is "left, top, right, bottom", /// assuming a y-down coordinate space. #[inline] pub const fn new(x0: f64, y0: f64, x1: f64, y1: f64) -> Insets { Insets { x0, y0, x1, y1 } } /// The total delta on the x-axis represented by these insets. /// /// # Examples /// /// ``` /// use kurbo::Insets; /// /// let insets = Insets::uniform_xy(3., 8.); /// assert_eq!(insets.x_value(), 6.); /// /// let insets = Insets::new(5., 0., -12., 0.,); /// assert_eq!(insets.x_value(), -7.); /// ``` #[inline] pub fn x_value(self) -> f64 { self.x0 + self.x1 } /// The total delta on the y-axis represented by these insets. /// /// # Examples /// /// ``` /// use kurbo::Insets; /// /// let insets = Insets::uniform_xy(3., 7.); /// assert_eq!(insets.y_value(), 14.); /// /// let insets = Insets::new(5., 10., -12., 4.,); /// assert_eq!(insets.y_value(), 14.); /// ``` #[inline] pub fn y_value(self) -> f64 { self.y0 + self.y1 } } impl Neg for Insets { type Output = Insets; #[inline] fn neg(self) -> Insets { Insets::new(-self.x0, -self.y0, -self.x1, -self.y1) } } impl Add<Rect> for Insets { type Output = Rect; #[inline] fn add(self, other: Rect) -> Rect { let other = other.abs(); Rect::new( other.x0 - self.x0, other.y0 - self.y0, other.x1 + self.x1, other.y1 + self.y1, ) } } impl Add<Insets> for Rect { type Output = Rect; #[inline] fn add(self, other: Insets) -> Rect { other + self } } impl Sub<Rect> for Insets { type Output = Rect; #[inline] fn sub(self, other: Rect) -> Rect { other + -self } } impl Sub<Insets> for Rect { type Output = Rect; #[inline] fn sub(self, other: Insets) -> Rect { other - self } } impl From<f64> for Insets { fn from(src: f64) -> Insets { Insets::uniform(src) } } impl From<(f64, f64)> for Insets { fn from(src: (f64, f64)) -> Insets { Insets::uniform_xy(src.0, src.1) } } impl From<(f64, f64, f64, f64)> for Insets { fn from(src: (f64, f64, f64, f64)) -> Insets { Insets::new(src.0, src.1, src.2, src.3) } }