| 1 | use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, Neg}; |
| 2 | |
| 3 | ////////// POINT /////////////////////////////////////////////////////////////// |
| 4 | |
| 5 | #[macro_export] |
| 6 | macro_rules! point { |
| 7 | ( $x:expr, $y:expr ) => { |
| 8 | Point { x: $x, y: $y } |
| 9 | }; |
| 10 | } |
| 11 | |
| 12 | #[derive(Debug, Default, Copy, Clone, PartialEq)] |
| 13 | pub struct Point<T> { |
| 14 | pub x: T, |
| 15 | pub y: T, |
| 16 | } |
| 17 | |
| 18 | impl Point<f64> { |
| 19 | pub fn length(&self) -> f64 { |
| 20 | ((self.x * self.x) + (self.y * self.y)).sqrt() |
| 21 | } |
| 22 | |
| 23 | pub fn normalized(&self) -> Self { |
| 24 | let l = self.length(); |
| 25 | Self { |
| 26 | x: self.x / l, |
| 27 | y: self.y / l, |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | pub fn to_radians(&self) -> Radians { |
| 32 | Radians(self.y.atan2(self.x)) |
| 33 | } |
| 34 | |
| 35 | pub fn to_degrees(&self) -> Degrees { |
| 36 | self.to_radians().to_degrees() |
| 37 | } |
| 38 | |
| 39 | pub fn to_i32(self) -> Point<i32> { |
| 40 | Point { |
| 41 | x: self.x as i32, |
| 42 | y: self.y as i32, |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | macro_rules! point_op { |
| 48 | ($op:tt, $trait:ident($fn:ident), $trait_assign:ident($fn_assign:ident), $rhs:ident = $Rhs:ty => $x:expr, $y:expr) => { |
| 49 | impl<T: $trait<Output = T>> $trait<$Rhs> for Point<T> { |
| 50 | type Output = Self; |
| 51 | |
| 52 | fn $fn(self, $rhs: $Rhs) -> Self { |
| 53 | Self { |
| 54 | x: self.x $op $x, |
| 55 | y: self.y $op $y, |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl<T: $trait<Output = T> + Copy> $trait_assign<$Rhs> for Point<T> { |
| 61 | fn $fn_assign(&mut self, $rhs: $Rhs) { |
| 62 | *self = Self { |
| 63 | x: self.x $op $x, |
| 64 | y: self.y $op $y, |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | point_op!(+, Add(add), AddAssign(add_assign), rhs = Point<T> => rhs.x, rhs.y); |
| 72 | point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point<T> => rhs.x, rhs.y); |
| 73 | point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point<T> => rhs.x, rhs.y); |
| 74 | point_op!(/, Div(div), DivAssign(div_assign), rhs = Point<T> => rhs.x, rhs.y); |
| 75 | point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1); |
| 76 | point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1); |
| 77 | point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1); |
| 78 | point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1); |
| 79 | point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Dimension<T> => rhs.width, rhs.height); |
| 80 | point_op!(/, Div(div), DivAssign(div_assign), rhs = Dimension<T> => rhs.width, rhs.height); |
| 81 | |
| 82 | ////////// multiply point with scalar ////////////////////////////////////////// |
| 83 | impl<T: Mul<Output = T> + Copy> Mul<T> for Point<T> { |
| 84 | type Output = Self; |
| 85 | |
| 86 | fn mul(self, rhs: T) -> Self { |
| 87 | Self { |
| 88 | x: self.x * rhs, |
| 89 | y: self.y * rhs, |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point<T> { |
| 95 | fn mul_assign(&mut self, rhs: T) { |
| 96 | *self = Self { |
| 97 | x: self.x * rhs, |
| 98 | y: self.y * rhs, |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | ////////// divide point with scalar //////////////////////////////////////////// |
| 104 | impl<T: Div<Output = T> + Copy> Div<T> for Point<T> { |
| 105 | type Output = Self; |
| 106 | |
| 107 | fn div(self, rhs: T) -> Self { |
| 108 | Self { |
| 109 | x: self.x / rhs, |
| 110 | y: self.y / rhs, |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl<T: Div<Output = T> + Copy> DivAssign<T> for Point<T> { |
| 116 | fn div_assign(&mut self, rhs: T) { |
| 117 | *self = Self { |
| 118 | x: self.x / rhs, |
| 119 | y: self.y / rhs, |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | impl<T: Neg<Output = T>> Neg for Point<T> { |
| 125 | type Output = Self; |
| 126 | |
| 127 | fn neg(self) -> Self { |
| 128 | Self { |
| 129 | x: -self.x, |
| 130 | y: -self.y, |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | impl<T> From<(T, T)> for Point<T> { |
| 136 | fn from(item: (T, T)) -> Self { |
| 137 | Point { |
| 138 | x: item.0, |
| 139 | y: item.1, |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl<T> From<Point<T>> for (T, T) { |
| 145 | fn from(item: Point<T>) -> Self { |
| 146 | (item.x, item.y) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | impl From<Degrees> for Point<f64> { |
| 151 | fn from(item: Degrees) -> Self { |
| 152 | let r = item.0.to_radians(); |
| 153 | Point { |
| 154 | x: r.cos(), |
| 155 | y: r.sin(), |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | impl From<Radians> for Point<f64> { |
| 161 | fn from(item: Radians) -> Self { |
| 162 | Point { |
| 163 | x: item.0.cos(), |
| 164 | y: item.0.sin(), |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #[derive(Debug, Default, PartialEq, Clone, Copy)] |
| 170 | pub struct Degrees(pub f64); |
| 171 | #[derive(Debug, Default, PartialEq, Clone, Copy)] |
| 172 | pub struct Radians(pub f64); |
| 173 | |
| 174 | impl Degrees { |
| 175 | #[allow(dead_code)] |
| 176 | fn to_radians(&self) -> Radians { |
| 177 | Radians(self.0.to_radians()) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | impl Radians { |
| 182 | #[allow(dead_code)] |
| 183 | fn to_degrees(&self) -> Degrees { |
| 184 | Degrees(self.0.to_degrees()) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | ////////// INTERSECTION //////////////////////////////////////////////////////// |
| 189 | |
| 190 | #[derive(Debug)] |
| 191 | pub enum Intersection { |
| 192 | Point(Point<f64>), |
| 193 | //Line(Point<f64>, Point<f64>), // TODO: overlapping collinear |
| 194 | None, |
| 195 | } |
| 196 | |
| 197 | impl Intersection { |
| 198 | pub fn lines(p1: Point<f64>, p2: Point<f64>, p3: Point<f64>, p4: Point<f64>) -> Intersection { |
| 199 | let s1 = p2 - p1; |
| 200 | let s2 = p4 - p3; |
| 201 | |
| 202 | let denomimator = -s2.x * s1.y + s1.x * s2.y; |
| 203 | if denomimator != 0.0 { |
| 204 | let s = (-s1.y * (p1.x - p3.x) + s1.x * (p1.y - p3.y)) / denomimator; |
| 205 | let t = ( s2.x * (p1.y - p3.y) - s2.y * (p1.x - p3.x)) / denomimator; |
| 206 | |
| 207 | if s >= 0.0 && s <= 1.0 && t >= 0.0 && t <= 1.0 { |
| 208 | return Intersection::Point(p1 + (s1 * t)) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | Intersection::None |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | ////////// DIMENSION /////////////////////////////////////////////////////////// |
| 217 | |
| 218 | #[macro_export] |
| 219 | macro_rules! dimen { |
| 220 | ( $w:expr, $h:expr ) => { |
| 221 | Dimension { width: $w, height: $h } |
| 222 | }; |
| 223 | } |
| 224 | |
| 225 | #[derive(Debug, Default, Copy, Clone, PartialEq)] |
| 226 | pub struct Dimension<T> { |
| 227 | pub width: T, |
| 228 | pub height: T, |
| 229 | } |
| 230 | |
| 231 | impl<T: Mul<Output = T> + Copy> Dimension<T> { |
| 232 | #[allow(dead_code)] |
| 233 | pub fn area(&self) -> T { |
| 234 | self.width * self.height |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | impl<T> From<(T, T)> for Dimension<T> { |
| 239 | fn from(item: (T, T)) -> Self { |
| 240 | Dimension { |
| 241 | width: item.0, |
| 242 | height: item.1, |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | impl<T> From<Dimension<T>> for (T, T) { |
| 248 | fn from(item: Dimension<T>) -> Self { |
| 249 | (item.width, item.height) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | //////////////////////////////////////////////////////////////////////////////// |
| 254 | |
| 255 | #[allow(dead_code)] |
| 256 | pub fn supercover_line_int(p1: Point<isize>, p2: Point<isize>) -> Vec<Point<isize>> { |
| 257 | let d = p2 - p1; |
| 258 | let n = point!(d.x.abs(), d.y.abs()); |
| 259 | let step = point!( |
| 260 | if d.x > 0 { 1 } else { -1 }, |
| 261 | if d.y > 0 { 1 } else { -1 } |
| 262 | ); |
| 263 | |
| 264 | let mut p = p1.clone(); |
| 265 | let mut points = vec!(point!(p.x as isize, p.y as isize)); |
| 266 | let mut i = point!(0, 0); |
| 267 | while i.x < n.x || i.y < n.y { |
| 268 | let decision = (1 + 2 * i.x) * n.y - (1 + 2 * i.y) * n.x; |
| 269 | if decision == 0 { // next step is diagonal |
| 270 | p.x += step.x; |
| 271 | p.y += step.y; |
| 272 | i.x += 1; |
| 273 | i.y += 1; |
| 274 | } else if decision < 0 { // next step is horizontal |
| 275 | p.x += step.x; |
| 276 | i.x += 1; |
| 277 | } else { // next step is vertical |
| 278 | p.y += step.y; |
| 279 | i.y += 1; |
| 280 | } |
| 281 | points.push(point!(p.x as isize, p.y as isize)); |
| 282 | } |
| 283 | |
| 284 | points |
| 285 | } |
| 286 | |
| 287 | /// Calculates all points a line crosses, unlike Bresenham's line algorithm. |
| 288 | /// There might be room for a lot of improvement here. |
| 289 | pub fn supercover_line(mut p1: Point<f64>, mut p2: Point<f64>) -> Vec<Point<isize>> { |
| 290 | let mut delta = p2 - p1; |
| 291 | if (delta.x.abs() > delta.y.abs() && delta.x.is_sign_negative()) || (delta.x.abs() <= delta.y.abs() && delta.y.is_sign_negative()) { |
| 292 | std::mem::swap(&mut p1, &mut p2); |
| 293 | delta = -delta; |
| 294 | } |
| 295 | |
| 296 | let mut last = point!(p1.x as isize, p1.y as isize); |
| 297 | let mut coords: Vec<Point<isize>> = vec!(); |
| 298 | coords.push(last); |
| 299 | |
| 300 | if delta.x.abs() > delta.y.abs() { |
| 301 | let k = delta.y / delta.x; |
| 302 | let m = p1.y as f64 - p1.x as f64 * k; |
| 303 | for x in (p1.x as isize + 1)..=(p2.x as isize) { |
| 304 | let y = (k * x as f64 + m).floor(); |
| 305 | let next = point!(x as isize - 1, y as isize); |
| 306 | if next != last { |
| 307 | coords.push(next); |
| 308 | } |
| 309 | let next = point!(x as isize, y as isize); |
| 310 | coords.push(next); |
| 311 | last = next; |
| 312 | } |
| 313 | } else { |
| 314 | let k = delta.x / delta.y; |
| 315 | let m = p1.x as f64 - p1.y as f64 * k; |
| 316 | for y in (p1.y as isize + 1)..=(p2.y as isize) { |
| 317 | let x = (k * y as f64 + m).floor(); |
| 318 | let next = point!(x as isize, y as isize - 1); |
| 319 | if next != last { |
| 320 | coords.push(next); |
| 321 | } |
| 322 | let next = point!(x as isize, y as isize); |
| 323 | coords.push(next); |
| 324 | last = next; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | let next = point!(p2.x as isize, p2.y as isize); |
| 329 | if next != last { |
| 330 | coords.push(next); |
| 331 | } |
| 332 | |
| 333 | coords |
| 334 | } |
| 335 | |
| 336 | ////////// TESTS /////////////////////////////////////////////////////////////// |
| 337 | |
| 338 | #[cfg(test)] |
| 339 | mod tests { |
| 340 | use super::*; |
| 341 | |
| 342 | #[test] |
| 343 | fn immutable_copy_of_point() { |
| 344 | let a = point!(0, 0); |
| 345 | let mut b = a; // Copy |
| 346 | assert_eq!(a, b); // PartialEq |
| 347 | b.x = 1; |
| 348 | assert_ne!(a, b); // PartialEq |
| 349 | } |
| 350 | |
| 351 | #[test] |
| 352 | fn add_points() { |
| 353 | let mut a = point!(1, 0); |
| 354 | assert_eq!(a + point!(2, 2), point!(3, 2)); // Add |
| 355 | a += point!(2, 2); // AddAssign |
| 356 | assert_eq!(a, point!(3, 2)); |
| 357 | assert_eq!(point!(1, 0) + (2, 3), point!(3, 3)); |
| 358 | } |
| 359 | |
| 360 | #[test] |
| 361 | fn sub_points() { |
| 362 | let mut a = point!(1, 0); |
| 363 | assert_eq!(a - point!(2, 2), point!(-1, -2)); |
| 364 | a -= point!(2, 2); |
| 365 | assert_eq!(a, point!(-1, -2)); |
| 366 | assert_eq!(point!(1, 0) - (2, 3), point!(-1, -3)); |
| 367 | } |
| 368 | |
| 369 | #[test] |
| 370 | fn mul_points() { |
| 371 | let mut a = point!(1, 2); |
| 372 | assert_eq!(a * 2, point!(2, 4)); |
| 373 | assert_eq!(a * point!(2, 3), point!(2, 6)); |
| 374 | a *= 2; |
| 375 | assert_eq!(a, point!(2, 4)); |
| 376 | a *= point!(3, 1); |
| 377 | assert_eq!(a, point!(6, 4)); |
| 378 | assert_eq!(point!(1, 0) * (2, 3), point!(2, 0)); |
| 379 | } |
| 380 | |
| 381 | #[test] |
| 382 | fn div_points() { |
| 383 | let mut a = point!(4, 8); |
| 384 | assert_eq!(a / 2, point!(2, 4)); |
| 385 | assert_eq!(a / point!(2, 4), point!(2, 2)); |
| 386 | a /= 2; |
| 387 | assert_eq!(a, point!(2, 4)); |
| 388 | a /= point!(2, 4); |
| 389 | assert_eq!(a, point!(1, 1)); |
| 390 | assert_eq!(point!(6, 3) / (2, 3), point!(3, 1)); |
| 391 | } |
| 392 | |
| 393 | #[test] |
| 394 | fn neg_point() { |
| 395 | assert_eq!(point!(1, 1), -point!(-1, -1)); |
| 396 | } |
| 397 | |
| 398 | #[test] |
| 399 | fn angles() { |
| 400 | assert_eq!(Radians(0.0).to_degrees(), Degrees(0.0)); |
| 401 | assert_eq!(Radians(std::f64::consts::PI).to_degrees(), Degrees(180.0)); |
| 402 | assert_eq!(Degrees(180.0).to_radians(), Radians(std::f64::consts::PI)); |
| 403 | assert!((Point::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001); |
| 404 | assert!((Point::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001); |
| 405 | } |
| 406 | |
| 407 | #[test] |
| 408 | fn area_for_dimension_of_multipliable_type() { |
| 409 | let r: Dimension<_> = (30, 20).into(); // the Into trait uses the From trait |
| 410 | assert_eq!(r.area(), 30 * 20); |
| 411 | // let a = Dimension::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String |
| 412 | } |
| 413 | |
| 414 | #[test] |
| 415 | fn intersection_of_lines() { |
| 416 | let p1 = point!(0.0, 0.0); |
| 417 | let p2 = point!(2.0, 2.0); |
| 418 | let p3 = point!(0.0, 2.0); |
| 419 | let p4 = point!(2.0, 0.0); |
| 420 | let r = Intersection::lines(p1, p2, p3, p4); |
| 421 | if let Intersection::Point(p) = r { |
| 422 | assert_eq!(p, point!(1.0, 1.0)); |
| 423 | } else { |
| 424 | panic!(); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | #[test] |
| 429 | fn some_coordinates_on_line() { |
| 430 | // horizontally up |
| 431 | let coords = supercover_line(point!(0.0, 0.0), point!(3.3, 2.2)); |
| 432 | assert_eq!(coords.as_slice(), &[point!(0, 0), point!(1, 0), point!(1, 1), point!(2, 1), point!(2, 2), point!(3, 2)]); |
| 433 | |
| 434 | // horizontally down |
| 435 | let coords = supercover_line(point!(0.0, 5.0), point!(3.3, 2.2)); |
| 436 | assert_eq!(coords.as_slice(), &[point!(0, 5), point!(0, 4), point!(1, 4), point!(1, 3), point!(2, 3), point!(2, 2), point!(3, 2)]); |
| 437 | |
| 438 | // vertically right |
| 439 | let coords = supercover_line(point!(0.0, 0.0), point!(2.2, 3.3)); |
| 440 | assert_eq!(coords.as_slice(), &[point!(0, 0), point!(0, 1), point!(1, 1), point!(1, 2), point!(2, 2), point!(2, 3)]); |
| 441 | |
| 442 | // vertically left |
| 443 | let coords = supercover_line(point!(5.0, 0.0), point!(3.0, 3.0)); |
| 444 | assert_eq!(coords.as_slice(), &[point!(5, 0), point!(4, 0), point!(4, 1), point!(3, 1), point!(3, 2), point!(3, 3)]); |
| 445 | |
| 446 | // negative |
| 447 | let coords = supercover_line(point!(0.0, 0.0), point!(-3.0, -2.0)); |
| 448 | assert_eq!(coords.as_slice(), &[point!(-3, -2), point!(-2, -2), point!(-2, -1), point!(-1, -1), point!(-1, 0), point!(0, 0)]); |
| 449 | |
| 450 | // |
| 451 | let coords = supercover_line(point!(0.0, 0.0), point!(2.3, 1.1)); |
| 452 | assert_eq!(coords.as_slice(), &[point!(0, 0), point!(1, 0), point!(2, 0), point!(2, 1)]); |
| 453 | } |
| 454 | } |