Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
Rory McCann committed Aug 6, 2017
1 parent 8f02157 commit b7a01f5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/algorithm/coord_change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use num_traits::{Float};
use types::{Point, Polygon, LineString, Line, MultiPoint, MultiPolygon, MultiLineString};

pub trait CoordChange<T> {
fn coord_change(&self, func: &Fn(&(T, T)) -> (T, T)) -> Self where T: Float;

}

impl<T: Float> CoordChange<T> for Point<T> {
fn coord_change(&self, func: &Fn(&(T, T)) -> (T, T)) -> Point<T>
{
let new_point = func(&(self.0.x, self.0.y));
Point::new(new_point.0, new_point.1)
}
}

impl<T: Float> CoordChange<T> for LineString<T> {
fn coord_change(&self, func: &Fn(&(T, T)) -> (T, T)) -> Self
{
LineString(self.0.iter().map(|p| p.coord_change(func)).collect())
}
}

impl<T: Float> CoordChange<T> for Polygon<T> {
fn coord_change(&self, func: &Fn(&(T, T)) -> (T, T)) -> Self
{
Polygon::new(self.exterior.coord_change(func), self.interiors.iter().map(|l| l.coord_change(func)).collect())
}
}


mod test {
use super::*;

#[test]
fn point() {
let p = Point::new(10., 10.);
let new_p = p.coord_change(&|&(x, y)| (x+10., y+100.));
assert_eq!(new_p.x(), 20.);
assert_eq!(new_p.y(), 110.);
}

#[test]
fn linestring() {
let line1: LineString<f32> = LineString(vec![Point::new(0., 0.), Point::new(1., 2.).into()]);
let line2 = line1.coord_change(&|&(x, y)| (x+10., y-100.));
assert_eq!(line2.0[0], Point::new(10., -100.));
assert_eq!(line2.0[1], Point::new(11., -98.));

}

#[test]
fn polygon() {
let exterior = LineString(vec![Point::new(0., 0.), Point::new(1., 1.),
Point::new(1., 0.), Point::new(0., 0.)]);
let interiors = vec![LineString(vec![Point::new(0.1, 0.1), Point::new(0.9, 0.9),
Point::new(0.9, 0.1), Point::new(0.1, 0.1)])];
let p = Polygon::new(exterior, interiors);

let p2 = p.coord_change(&|&(x, y)| (x+10., y-100.));

let exterior2 = LineString(vec![Point::new(10., -100.), Point::new(11., -99.),
Point::new(11., -100.), Point::new(10., -100.)]);
let interiors2 = vec![LineString(vec![Point::new(10.1, -99.9), Point::new(10.9, -99.1),
Point::new(10.9, -99.9), Point::new(10.1, -99.)])];
let expected_p2 = Polygon::new(exterior2, interiors2);

assert_eq!(p2, expected_p2);
}
}
2 changes: 2 additions & 0 deletions src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ pub mod extremes;
pub mod rotate;
/// Translates a geometry along the given offsets.
pub mod translate;
/// Apply a function to all coordinates
pub mod coord_change;

0 comments on commit b7a01f5

Please sign in to comment.