geo_types/geometry/point.rs
1use crate::{point, Coord, CoordFloat, CoordNum};
2
3use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
4
5/// A single point in 2D space.
6///
7/// # Semantics
8///
9/// The _interior_ of the point is itself (a singleton set),
10/// and its _boundary_ is empty. A point is _valid_ if and
11/// only if the `Coord` is valid.
12///
13/// # Creating a Point
14///
15/// There are many ways to construct a point.
16/// ```
17/// use geo_types::{coord, point, Point};
18///
19/// let p1 = Point::new(0., 1.);
20///
21/// let p2 = point! { x: 1000.0, y: 2000.0 };
22///
23/// let p3: Point = (0., 1.).into();
24///
25/// let c = coord! { x: 10., y: 20. };
26/// let p4: Point = c.into();
27/// ```
28///
29/// See the `From` impl section for a complete list of conversions.
30///
31/// ## Coordinate order for geographic points
32///
33/// For geographic points, typically `x` corresponds to longitude and `y` to latitude.
34///
35/// Geographic methods in the [`geo`](https://crates.io/crates/geo) crate expect this common
36/// lon/lat order, but different conventions exist in other coordinate systems,
37/// notably EPSG:4326, which uses lat/lon ordering.
38/// ```
39/// use geo_types::{coord, point, Point};
40///
41/// let lon = 179.9;
42/// let lat = 45.0;
43/// let geographic_point = Point::new(lon, lat);
44/// ```
45///
46#[derive(Eq, PartialEq, Clone, Copy, Hash, Default)]
47#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
48pub struct Point<T: CoordNum = f64>(pub Coord<T>);
49
50impl<T: CoordNum> From<Coord<T>> for Point<T> {
51 fn from(x: Coord<T>) -> Self {
52 Point(x)
53 }
54}
55
56impl<T: CoordNum> From<(T, T)> for Point<T> {
57 fn from(coords: (T, T)) -> Self {
58 Point::new(coords.0, coords.1)
59 }
60}
61
62impl<T: CoordNum> From<[T; 2]> for Point<T> {
63 fn from(coords: [T; 2]) -> Self {
64 Point::new(coords[0], coords[1])
65 }
66}
67
68impl<T: CoordNum> From<Point<T>> for (T, T) {
69 fn from(point: Point<T>) -> Self {
70 point.0.into()
71 }
72}
73
74impl<T: CoordNum> From<Point<T>> for [T; 2] {
75 fn from(point: Point<T>) -> Self {
76 point.0.into()
77 }
78}
79
80impl<T: CoordNum> Point<T> {
81 /// Creates a new point.
82 ///
83 /// # Examples
84 ///
85 /// ```
86 /// use geo_types::Point;
87 ///
88 /// let p = Point::new(1.234, 2.345);
89 ///
90 /// assert_eq!(p.x(), 1.234);
91 /// assert_eq!(p.y(), 2.345);
92 /// ```
93 pub fn new(x: T, y: T) -> Self {
94 point! { x: x, y: y }
95 }
96
97 /// Returns the x/horizontal component of the point.
98 ///
99 /// Typically, `x` is the horizontal position, or longitude for geographic coordinates,
100 /// but its interpretation can vary across coordinate systems.
101 ///
102 /// # Examples
103 ///
104 /// ```
105 /// use geo_types::Point;
106 ///
107 /// let p = Point::new(1.234, 2.345);
108 ///
109 /// assert_eq!(p.x(), 1.234);
110 /// ```
111 pub fn x(self) -> T {
112 self.0.x
113 }
114
115 /// Sets the x/horizontal component of the point.
116 ///
117 /// Typically, `x` is the horizontal position, or longitude for geographic coordinates,
118 /// but its interpretation can vary across coordinate systems.
119 ///
120 /// # Examples
121 ///
122 /// ```
123 /// use geo_types::Point;
124 ///
125 /// let mut p = Point::new(1.234, 2.345);
126 /// p.set_x(9.876);
127 ///
128 /// assert_eq!(p.x(), 9.876);
129 /// ```
130 pub fn set_x(&mut self, x: T) -> &mut Self {
131 self.0.x = x;
132 self
133 }
134
135 /// Returns a mutable reference to the x/horizontal component of the point
136 ///
137 /// Typically, `x` is the horizontal position, or longitude for geographic coordinates,
138 /// but its interpretation can vary across coordinate systems.
139 ///
140 /// # Examples
141 ///
142 /// ```
143 /// use approx::assert_relative_eq;
144 /// use geo_types::Point;
145 /// let mut p = Point::new(1.234, 2.345);
146 /// let mut p_x = p.x_mut();
147 /// *p_x += 1.0;
148 /// assert_relative_eq!(p.x(), 2.234);
149 /// ```
150 pub fn x_mut(&mut self) -> &mut T {
151 &mut self.0.x
152 }
153
154 /// Returns the y/vertical component of the point.
155 ///
156 /// Typically, `y` is the vertical position, or latitude for geographic coordinates,
157 /// but its interpretation can vary across coordinate systems.
158 ///
159 /// # Examples
160 ///
161 /// ```
162 /// use geo_types::Point;
163 ///
164 /// let p = Point::new(1.234, 2.345);
165 ///
166 /// assert_eq!(p.y(), 2.345);
167 /// ```
168 pub fn y(self) -> T {
169 self.0.y
170 }
171
172 /// Sets the y/vertical component of the point.
173 ///
174 /// Typically, `y` is the vertical position, or latitude for geographic coordinates,
175 /// but its interpretation can vary across coordinate systems.
176 ///
177 /// # Examples
178 ///
179 /// ```
180 /// use geo_types::Point;
181 ///
182 /// let mut p = Point::new(1.234, 2.345);
183 /// p.set_y(9.876);
184 ///
185 /// assert_eq!(p.y(), 9.876);
186 /// ```
187 pub fn set_y(&mut self, y: T) -> &mut Self {
188 self.0.y = y;
189 self
190 }
191
192 /// Returns a mutable reference to the x/horizontal component of the point
193 ///
194 /// Typically, `y` is the vertical position, or latitude for geographic coordinates,
195 /// but its interpretation can vary across coordinate systems.
196 ///
197 /// # Examples
198 ///
199 /// ```
200 /// use approx::assert_relative_eq;
201 /// use geo_types::Point;
202 /// let mut p = Point::new(1.234, 2.345);
203 /// let mut p_y = p.y_mut();
204 /// *p_y += 1.0;
205 /// assert_relative_eq!(p.y(), 3.345);
206 /// ```
207 pub fn y_mut(&mut self) -> &mut T {
208 &mut self.0.y
209 }
210
211 /// Returns a tuple that contains the x/horizontal & y/vertical component of the point.
212 ///
213 /// # Examples
214 ///
215 /// ```
216 /// use geo_types::Point;
217 ///
218 /// let mut p = Point::new(1.234, 2.345);
219 /// let (x, y) = p.x_y();
220 ///
221 /// assert_eq!(y, 2.345);
222 /// assert_eq!(x, 1.234);
223 /// ```
224 pub fn x_y(self) -> (T, T) {
225 (self.0.x, self.0.y)
226 }
227 /// Returns the longitude/horizontal component of the point.
228 ///
229 /// # Examples
230 ///
231 /// ```
232 /// use geo_types::Point;
233 ///
234 /// let p = Point::new(1.234, 2.345);
235 ///
236 /// assert_eq!(p.x(), 1.234);
237 /// ```
238 #[deprecated = "use `Point::x` instead, it's less ambiguous"]
239 pub fn lng(self) -> T {
240 self.x()
241 }
242
243 /// Sets the longitude/horizontal component of the point.
244 ///
245 /// # Examples
246 ///
247 /// ```
248 /// use geo_types::Point;
249 ///
250 /// let mut p = Point::new(1.234, 2.345);
251 /// #[allow(deprecated)]
252 /// p.set_lng(9.876);
253 ///
254 /// assert_eq!(p.x(), 9.876);
255 /// ```
256 #[deprecated = "use `Point::set_x` instead, it's less ambiguous"]
257 pub fn set_lng(&mut self, lng: T) -> &mut Self {
258 self.set_x(lng)
259 }
260
261 /// Returns the latitude/vertical component of the point.
262 ///
263 /// # Examples
264 ///
265 /// ```
266 /// use geo_types::Point;
267 ///
268 /// let p = Point::new(1.234, 2.345);
269 ///
270 /// assert_eq!(p.y(), 2.345);
271 /// ```
272 #[deprecated = "use `Point::y` instead, it's less ambiguous"]
273 pub fn lat(self) -> T {
274 self.y()
275 }
276 /// Sets the latitude/vertical component of the point.
277 ///
278 /// # Examples
279 ///
280 /// ```
281 /// use geo_types::Point;
282 ///
283 /// let mut p = Point::new(1.234, 2.345);
284 /// #[allow(deprecated)]
285 /// p.set_lat(9.876);
286 ///
287 /// assert_eq!(p.y(), 9.876);
288 /// ```
289 #[deprecated = "use `Point::set_y` instead, it's less ambiguous"]
290 pub fn set_lat(&mut self, lat: T) -> &mut Self {
291 self.set_y(lat)
292 }
293}
294
295impl<T: CoordNum> Point<T> {
296 /// Returns the dot product of the two points:
297 /// `dot = x1 * x2 + y1 * y2`
298 ///
299 /// # Examples
300 ///
301 /// ```
302 /// use geo_types::{point, Point};
303 ///
304 /// let point = point! { x: 1.5, y: 0.5 };
305 /// let dot = point.dot(point! { x: 2.0, y: 4.5 });
306 ///
307 /// assert_eq!(dot, 5.25);
308 /// ```
309 pub fn dot(self, other: Self) -> T {
310 self.x() * other.x() + self.y() * other.y()
311 }
312
313 /// Returns the cross product of 3 points. A positive value implies
314 /// `self` → `point_b` → `point_c` is counter-clockwise, negative implies
315 /// clockwise.
316 ///
317 /// # Note on Robustness
318 ///
319 /// This function is **not** robust against floating-point errors.
320 /// The [`geo`](https://docs.rs/geo) crate
321 /// offers robust predicates for standard numeric types using the
322 /// [`Kernel`](https://docs.rs/geo/algorithm/kernels/trait.Kernel.html)
323 /// trait, and these should be preferred if possible.
324 ///
325 /// # Examples
326 ///
327 /// ```
328 /// use geo_types::point;
329 ///
330 /// let point_a = point! { x: 1., y: 2. };
331 /// let point_b = point! { x: 3., y: 5. };
332 /// let point_c = point! { x: 7., y: 12. };
333 ///
334 /// let cross = point_a.cross_prod(point_b, point_c);
335 ///
336 /// assert_eq!(cross, 2.0)
337 /// ```
338 pub fn cross_prod(self, point_b: Self, point_c: Self) -> T {
339 (point_b.x() - self.x()) * (point_c.y() - self.y())
340 - (point_b.y() - self.y()) * (point_c.x() - self.x())
341 }
342}
343
344impl<T: CoordFloat> Point<T> {
345 /// Converts the (x,y) components of Point to degrees
346 ///
347 /// # Example
348 /// ```
349 /// use geo_types::Point;
350 ///
351 /// let p = Point::new(1.234, 2.345);
352 /// let (x, y): (f32, f32) = p.to_degrees().x_y();
353 /// assert_eq!(x.round(), 71.0);
354 /// assert_eq!(y.round(), 134.0);
355 /// ```
356 pub fn to_degrees(self) -> Self {
357 let (x, y) = self.x_y();
358 let x = x.to_degrees();
359 let y = y.to_degrees();
360 Point::new(x, y)
361 }
362
363 /// Converts the (x,y) components of Point to radians
364 ///
365 /// # Example
366 /// ```
367 /// use geo_types::Point;
368 ///
369 /// let p = Point::new(180.0, 341.5);
370 /// let (x, y): (f32, f32) = p.to_radians().x_y();
371 /// assert_eq!(x.round(), 3.0);
372 /// assert_eq!(y.round(), 6.0);
373 /// ```
374 pub fn to_radians(self) -> Self {
375 let (x, y) = self.x_y();
376 let x = x.to_radians();
377 let y = y.to_radians();
378 Point::new(x, y)
379 }
380}
381
382impl<T> Neg for Point<T>
383where
384 T: CoordNum + Neg<Output = T>,
385{
386 type Output = Self;
387
388 /// Returns a point with the x and y components negated.
389 ///
390 /// # Examples
391 ///
392 /// ```
393 /// use geo_types::Point;
394 ///
395 /// let p = -Point::new(-1.25, 2.5);
396 ///
397 /// assert_eq!(p.x(), 1.25);
398 /// assert_eq!(p.y(), -2.5);
399 /// ```
400 fn neg(self) -> Self::Output {
401 Point::from(-self.0)
402 }
403}
404
405impl<T: CoordNum> Add for Point<T> {
406 type Output = Self;
407
408 /// Add a point to the given point.
409 ///
410 /// # Examples
411 ///
412 /// ```
413 /// use geo_types::Point;
414 ///
415 /// let p = Point::new(1.25, 2.5) + Point::new(1.5, 2.5);
416 ///
417 /// assert_eq!(p.x(), 2.75);
418 /// assert_eq!(p.y(), 5.0);
419 /// ```
420 fn add(self, rhs: Self) -> Self::Output {
421 Point::from(self.0 + rhs.0)
422 }
423}
424
425impl<T: CoordNum> AddAssign for Point<T> {
426 /// Add a point to the given point and assign it to the original point.
427 ///
428 /// # Examples
429 ///
430 /// ```
431 /// use geo_types::Point;
432 ///
433 /// let mut p = Point::new(1.25, 2.5);
434 /// p += Point::new(1.5, 2.5);
435 ///
436 /// assert_eq!(p.x(), 2.75);
437 /// assert_eq!(p.y(), 5.0);
438 /// ```
439 fn add_assign(&mut self, rhs: Self) {
440 self.0 = self.0 + rhs.0;
441 }
442}
443
444impl<T: CoordNum> Sub for Point<T> {
445 type Output = Self;
446
447 /// Subtract a point from the given point.
448 ///
449 /// # Examples
450 ///
451 /// ```
452 /// use geo_types::Point;
453 ///
454 /// let p = Point::new(1.25, 3.0) - Point::new(1.5, 2.5);
455 ///
456 /// assert_eq!(p.x(), -0.25);
457 /// assert_eq!(p.y(), 0.5);
458 /// ```
459 fn sub(self, rhs: Self) -> Self::Output {
460 Point::from(self.0 - rhs.0)
461 }
462}
463
464impl<T: CoordNum> SubAssign for Point<T> {
465 /// Subtract a point from the given point and assign it to the original point.
466 ///
467 /// # Examples
468 ///
469 /// ```
470 /// use geo_types::Point;
471 ///
472 /// let mut p = Point::new(1.25, 2.5);
473 /// p -= Point::new(1.5, 2.5);
474 ///
475 /// assert_eq!(p.x(), -0.25);
476 /// assert_eq!(p.y(), 0.0);
477 /// ```
478 fn sub_assign(&mut self, rhs: Self) {
479 self.0 = self.0 - rhs.0;
480 }
481}
482
483impl<T: CoordNum> Mul<T> for Point<T> {
484 type Output = Self;
485
486 /// Scaler multiplication of a point
487 ///
488 /// # Examples
489 ///
490 /// ```
491 /// use geo_types::Point;
492 ///
493 /// let p = Point::new(2.0, 3.0) * 2.0;
494 ///
495 /// assert_eq!(p.x(), 4.0);
496 /// assert_eq!(p.y(), 6.0);
497 /// ```
498 fn mul(self, rhs: T) -> Self::Output {
499 Point::from(self.0 * rhs)
500 }
501}
502
503impl<T: CoordNum> MulAssign<T> for Point<T> {
504 /// Scaler multiplication of a point in place
505 ///
506 /// # Examples
507 ///
508 /// ```
509 /// use geo_types::Point;
510 ///
511 /// let mut p = Point::new(2.0, 3.0);
512 /// p *= 2.0;
513 ///
514 /// assert_eq!(p.x(), 4.0);
515 /// assert_eq!(p.y(), 6.0);
516 /// ```
517 fn mul_assign(&mut self, rhs: T) {
518 self.0 = self.0 * rhs
519 }
520}
521
522impl<T: CoordNum> Div<T> for Point<T> {
523 type Output = Self;
524
525 /// Scaler division of a point
526 ///
527 /// # Examples
528 ///
529 /// ```
530 /// use geo_types::Point;
531 ///
532 /// let p = Point::new(2.0, 3.0) / 2.0;
533 ///
534 /// assert_eq!(p.x(), 1.0);
535 /// assert_eq!(p.y(), 1.5);
536 /// ```
537 fn div(self, rhs: T) -> Self::Output {
538 Point::from(self.0 / rhs)
539 }
540}
541
542impl<T: CoordNum> DivAssign<T> for Point<T> {
543 /// Scaler division of a point in place
544 ///
545 /// # Examples
546 ///
547 /// ```
548 /// use geo_types::Point;
549 ///
550 /// let mut p = Point::new(2.0, 3.0);
551 /// p /= 2.0;
552 ///
553 /// assert_eq!(p.x(), 1.0);
554 /// assert_eq!(p.y(), 1.5);
555 /// ```
556 fn div_assign(&mut self, rhs: T) {
557 self.0 = self.0 / rhs
558 }
559}
560
561#[cfg(any(feature = "approx", test))]
562mod approx_integration {
563 use super::*;
564 use approx::{AbsDiffEq, RelativeEq, UlpsEq};
565
566 impl<T> RelativeEq for Point<T>
567 where
568 T: CoordNum + RelativeEq<Epsilon = T>,
569 {
570 #[inline]
571 fn default_max_relative() -> Self::Epsilon {
572 T::default_max_relative()
573 }
574
575 /// Equality assertion within a relative limit.
576 ///
577 /// # Examples
578 ///
579 /// ```
580 /// use geo_types::Point;
581 ///
582 /// let a = Point::new(2.0, 3.0);
583 /// let b = Point::new(2.0, 3.01);
584 ///
585 /// approx::assert_relative_eq!(a, b, max_relative=0.1)
586 /// ```
587 #[inline]
588 fn relative_eq(
589 &self,
590 other: &Self,
591 epsilon: Self::Epsilon,
592 max_relative: Self::Epsilon,
593 ) -> bool {
594 self.0.relative_eq(&other.0, epsilon, max_relative)
595 }
596 }
597
598 impl<T> AbsDiffEq for Point<T>
599 where
600 T: CoordNum + AbsDiffEq<Epsilon = T>,
601 {
602 type Epsilon = T::Epsilon;
603
604 #[inline]
605 fn default_epsilon() -> Self::Epsilon {
606 T::default_epsilon()
607 }
608
609 /// Equality assertion with an absolute limit.
610 ///
611 /// # Examples
612 ///
613 /// ```
614 /// use geo_types::Point;
615 ///
616 /// let a = Point::new(2.0, 3.0);
617 /// let b = Point::new(2.0, 3.0000001);
618 ///
619 /// approx::assert_relative_eq!(a, b, epsilon=0.1)
620 /// ```
621 #[inline]
622 fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
623 self.0.abs_diff_eq(&other.0, epsilon)
624 }
625 }
626
627 impl<T> UlpsEq for Point<T>
628 where
629 T: CoordNum + UlpsEq<Epsilon = T>,
630 {
631 fn default_max_ulps() -> u32 {
632 T::default_max_ulps()
633 }
634
635 fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
636 self.0.ulps_eq(&other.0, epsilon, max_ulps)
637 }
638 }
639}
640
641#[cfg(feature = "rstar_0_8")]
642// These are required for rstar RTree
643impl<T> ::rstar_0_8::Point for Point<T>
644where
645 T: ::num_traits::Float + ::rstar_0_8::RTreeNum,
646{
647 type Scalar = T;
648
649 const DIMENSIONS: usize = 2;
650
651 fn generate(generator: impl Fn(usize) -> Self::Scalar) -> Self {
652 Point::new(generator(0), generator(1))
653 }
654
655 fn nth(&self, index: usize) -> Self::Scalar {
656 match index {
657 0 => self.0.x,
658 1 => self.0.y,
659 _ => unreachable!(),
660 }
661 }
662 fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
663 match index {
664 0 => &mut self.0.x,
665 1 => &mut self.0.y,
666 _ => unreachable!(),
667 }
668 }
669}
670
671#[cfg(feature = "rstar_0_9")]
672impl<T> ::rstar_0_9::Point for Point<T>
673where
674 T: ::num_traits::Float + ::rstar_0_9::RTreeNum,
675{
676 type Scalar = T;
677
678 const DIMENSIONS: usize = 2;
679
680 fn generate(mut generator: impl FnMut(usize) -> Self::Scalar) -> Self {
681 Point::new(generator(0), generator(1))
682 }
683
684 fn nth(&self, index: usize) -> Self::Scalar {
685 match index {
686 0 => self.0.x,
687 1 => self.0.y,
688 _ => unreachable!(),
689 }
690 }
691 fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
692 match index {
693 0 => &mut self.0.x,
694 1 => &mut self.0.y,
695 _ => unreachable!(),
696 }
697 }
698}
699
700#[cfg(feature = "rstar_0_10")]
701impl<T> ::rstar_0_10::Point for Point<T>
702where
703 T: ::num_traits::Float + ::rstar_0_10::RTreeNum,
704{
705 type Scalar = T;
706
707 const DIMENSIONS: usize = 2;
708
709 fn generate(mut generator: impl FnMut(usize) -> Self::Scalar) -> Self {
710 Point::new(generator(0), generator(1))
711 }
712
713 fn nth(&self, index: usize) -> Self::Scalar {
714 match index {
715 0 => self.0.x,
716 1 => self.0.y,
717 _ => unreachable!(),
718 }
719 }
720 fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
721 match index {
722 0 => &mut self.0.x,
723 1 => &mut self.0.y,
724 _ => unreachable!(),
725 }
726 }
727}
728
729#[cfg(feature = "rstar_0_11")]
730impl<T> ::rstar_0_11::Point for Point<T>
731where
732 T: ::num_traits::Float + ::rstar_0_11::RTreeNum,
733{
734 type Scalar = T;
735
736 const DIMENSIONS: usize = 2;
737
738 fn generate(mut generator: impl FnMut(usize) -> Self::Scalar) -> Self {
739 Point::new(generator(0), generator(1))
740 }
741
742 fn nth(&self, index: usize) -> Self::Scalar {
743 match index {
744 0 => self.0.x,
745 1 => self.0.y,
746 _ => unreachable!(),
747 }
748 }
749 fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
750 match index {
751 0 => &mut self.0.x,
752 1 => &mut self.0.y,
753 _ => unreachable!(),
754 }
755 }
756}
757
758#[cfg(feature = "rstar_0_12")]
759impl<T> ::rstar_0_12::Point for Point<T>
760where
761 T: ::num_traits::Float + ::rstar_0_12::RTreeNum,
762{
763 type Scalar = T;
764
765 const DIMENSIONS: usize = 2;
766
767 fn generate(mut generator: impl FnMut(usize) -> Self::Scalar) -> Self {
768 Point::new(generator(0), generator(1))
769 }
770
771 fn nth(&self, index: usize) -> Self::Scalar {
772 match index {
773 0 => self.0.x,
774 1 => self.0.y,
775 _ => unreachable!(),
776 }
777 }
778 fn nth_mut(&mut self, index: usize) -> &mut Self::Scalar {
779 match index {
780 0 => &mut self.0.x,
781 1 => &mut self.0.y,
782 _ => unreachable!(),
783 }
784 }
785}
786
787impl<T: CoordNum> AsRef<Coord<T>> for Point<T> {
788 fn as_ref(&self) -> &Coord<T> {
789 &self.0
790 }
791}
792
793#[cfg(test)]
794mod test {
795 use super::*;
796
797 use approx::{AbsDiffEq, RelativeEq};
798
799 #[test]
800 fn test_abs_diff_eq() {
801 let delta = 1e-6;
802 let p = Point::new(1.0, 1.0);
803
804 let p_x = Point::new(1.0 - delta, 1.0);
805 assert!(p.abs_diff_eq(&p_x, 1e-2));
806 assert!(p.abs_diff_ne(&p_x, 1e-12));
807
808 let p_y = Point::new(1.0, 1.0 + delta);
809 assert!(p.abs_diff_eq(&p_y, 1e-2));
810 assert!(p.abs_diff_ne(&p_y, 1e-12));
811
812 let p_xy = Point::new(1.0 + delta, 1.0 - delta);
813 assert!(p.abs_diff_eq(&p_xy, 1e-2));
814 assert!(p.abs_diff_ne(&p_xy, 1e-12));
815
816 let p_inf = Point::new(f64::INFINITY, 1.);
817 assert!(p.abs_diff_ne(&p_inf, 1e-2));
818 }
819
820 #[test]
821 fn test_relative_eq() {
822 let delta = 1e-6;
823 let p = Point::new(1.0, 1.0);
824
825 let p_x = Point::new(1.0 - delta, 1.0);
826 assert!(p.relative_eq(&p_x, 1e-2, 1e-2));
827 assert!(p.relative_ne(&p_x, 1e-12, 1e-12));
828
829 let p_y = Point::new(1.0, 1.0 + delta);
830 assert!(p.relative_eq(&p_y, 1e-2, 1e-2));
831 assert!(p.relative_ne(&p_y, 1e-12, 1e-12));
832
833 let p_xy = Point::new(1.0 + delta, 1.0 - delta);
834 assert!(p.relative_eq(&p_xy, 1e-2, 1e-2));
835 assert!(p.relative_ne(&p_xy, 1e-12, 1e-12));
836
837 let p_inf = Point::new(f64::INFINITY, 1.);
838 assert!(p.relative_ne(&p_inf, 1e-2, 1e-2));
839 }
840}