Skip to content

Commit

Permalink
Merge 6a49a7a into 75a6e54
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Mar 22, 2020
2 parents 75a6e54 + 6a49a7a commit c129e3a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
35 changes: 34 additions & 1 deletion geo-types/src/rect.rs
@@ -1,4 +1,4 @@
use crate::{Coordinate, CoordinateType};
use crate::{polygon, Coordinate, CoordinateType, Polygon};

/// A bounded 2D quadrilateral whose area is defined by minimum and maximum `Coordinates`.
#[derive(PartialEq, Clone, Copy, Debug, Hash)]
Expand Down Expand Up @@ -73,6 +73,39 @@ impl<T: CoordinateType> Rect<T> {
self.max().y - self.min().y
}

/// Create a `Polygon` from the `Rect`.
///
/// # Examples
///
/// ```rust
/// use geo_types::{Coordinate, Rect, polygon};
///
/// let rect = Rect::new(
/// Coordinate { x: 0., y: 0. },
/// Coordinate { x: 10., y: 20. },
/// );
///
/// assert_eq!(
/// rect.to_polygon(),
/// polygon![
/// (x: 0., y: 0.),
/// (x: 0., y: 20.),
/// (x: 10., y: 20.),
/// (x: 10., y: 0.),
/// (x: 0., y: 0.),
/// ],
/// );
/// ```
pub fn to_polygon(self) -> Polygon<T> {
polygon![
(x: self.min.x, y: self.min.y),
(x: self.min.x, y: self.max.y),
(x: self.max.x, y: self.max.y),
(x: self.max.x, y: self.min.y),
(x: self.min.x, y: self.min.y),
]
}

fn assert_valid_bounds(min: Coordinate<T>, max: Coordinate<T>) {
assert!(
min.x <= max.x && min.y <= max.y,
Expand Down
29 changes: 28 additions & 1 deletion geo-types/src/triangle.rs
@@ -1,4 +1,4 @@
use crate::{Coordinate, CoordinateType, Line};
use crate::{polygon, Coordinate, CoordinateType, Line, Polygon};

/// A bounded 2D area whose three vertices are defined by `Coordinate`s.
#[derive(Copy, Clone, Debug, Hash)]
Expand All @@ -17,6 +17,33 @@ impl<T: CoordinateType> Triangle<T> {
Line::new(self.2, self.0),
]
}

/// Create a `Polygon` from the `Triangle`.
///
/// # Examples
///
/// ```rust
/// use geo_types::{Coordinate, Triangle, polygon};
///
/// let triangle = Triangle(
/// Coordinate { x: 0., y: 0. },
/// Coordinate { x: 10., y: 20. },
/// Coordinate { x: 20., y: -10. },
/// );
///
/// assert_eq!(
/// triangle.to_polygon(),
/// polygon![
/// (x: 0., y: 0.),
/// (x: 10., y: 20.),
/// (x: 20., y: -10.),
/// (x: 0., y: 0.),
/// ],
/// );
/// ```
pub fn to_polygon(self) -> Polygon<T> {
polygon![self.0, self.1, self.2, self.0]
}
}

impl<IC: Into<Coordinate<T>> + Copy, T: CoordinateType> From<[IC; 3]> for Triangle<T> {
Expand Down

0 comments on commit c129e3a

Please sign in to comment.