Skip to content

Commit

Permalink
Area can work on some non-Float geometries (e.g. Rect<Integer>)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rory McCann committed Nov 5, 2018
1 parent 4a9225a commit 1efd87a
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions geo/src/algorithm/area.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use num_traits::Float;
use {Line, LineString, MultiPolygon, Polygon, Rect, Triangle};
use {Line, LineString, MultiPolygon, Polygon, Rect, Triangle, CoordinateType};

use algorithm::winding_order::twice_signed_ring_area;

/// Calculation of the area.
pub trait Area<T>
where
T: Float,
T: CoordinateType,
{
/// Signed area of a geometry.
///
Expand Down Expand Up @@ -44,7 +44,7 @@ where

impl<T> Area<T> for Line<T>
where
T: Float,
T: CoordinateType,
{
fn area(&self) -> T {
T::zero()
Expand Down Expand Up @@ -77,7 +77,7 @@ where

impl<T> Area<T> for Rect<T>
where
T: Float,
T: CoordinateType
{
fn area(&self) -> T {
(self.max.x - self.min.x) * (self.max.y - self.min.y)
Expand Down Expand Up @@ -121,11 +121,17 @@ mod test {
}
#[test]
fn rectangle_test() {
let rect = Rect {
let rect1: Rect<f32> = Rect {
min: Coordinate { x: 10., y: 30. },
max: Coordinate { x: 20., y: 40. },
};
assert_relative_eq!(rect.area(), 100.);
assert_relative_eq!(rect1.area(), 100.);

let rect2: Rect<i32> = Rect {
min: Coordinate { x: 10, y: 30 },
max: Coordinate { x: 20, y: 40 },
};
assert_eq!(rect2.area(), 100);
}
#[test]
fn area_polygon_inner_test() {
Expand Down

0 comments on commit 1efd87a

Please sign in to comment.