Skip to content

Commit

Permalink
Merge #278 #280
Browse files Browse the repository at this point in the history
278: Allow constructing a Line from anything that's Into<Coordinate> r=frewsxcv a=frewsxcv

Extracted froom #244.

280: Utilize `hypot` helper method. r=frewsxcv a=frewsxcv



Co-authored-by: Corey Farwell <[email protected]>
  • Loading branch information
bors[bot] and frewsxcv committed Jun 20, 2018
3 parents b1531cf + e416f34 + 2a57e59 commit 0a48e70
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion geo-types/src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
return point.euclidean_distance(&end);
}
let s = ((start.y() - point.y()) * dx - (start.x() - point.x()) * dy) / (dx * dx + dy * dy);
s.abs() * (dx * dx + dy * dy).sqrt()
s.abs() * dx.hypot(dy)
}

impl<T> EuclideanDistance<T, Point<T>> for Point<T>
Expand Down
22 changes: 14 additions & 8 deletions geo-types/src/line.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {CoordinateType, Point};
use {Coordinate, CoordinateType, Point};

#[cfg(feature = "spade")]
use algorithms::{BoundingBox, EuclideanDistance};
Expand All @@ -23,17 +23,23 @@ where
/// # Examples
///
/// ```
/// use geo_types::{Point, Line};
/// use geo_types::{Coordinate, Line};
///
/// let line = Line::new(Point::new(0., 0.), Point::new(1., 2.));
/// let line = Line::new(
/// Coordinate { x: 0., y: 0. },
/// Coordinate { x: 1., y: 2. },
/// );
///
/// assert_eq!(line.start, Point::new(0., 0.));
/// assert_eq!(line.end, Point::new(1., 2.));
/// assert_eq!(line.start.0, Coordinate { x: 0., y: 0. });
/// assert_eq!(line.end.0, Coordinate { x: 1., y: 2. });
/// ```
pub fn new(start: Point<T>, end: Point<T>) -> Line<T> {
pub fn new<C>(start: C, end: C) -> Line<T>
where
C: Into<Coordinate<T>>,
{
Line {
start,
end
start: Point(start.into()),
end: Point(end.into()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/closest_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ mod tests {
fn line_string_with_single_element_behaves_like_line() {
let points = vec![(0.0, 0.0), (100.0, 100.0)];
let line_string: LineString<f32> = collect_points(&points);
let line = Line::new(points[0].into(), points[1].into());
let line = Line::new(points[0], points[1]);

fuzz_two_impls(line, line_string);
}
Expand Down

0 comments on commit 0a48e70

Please sign in to comment.