Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from georust/point
Initial implementation of Point
- Loading branch information
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod point; | ||
|
||
#[test] | ||
fn it_works() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::num::Float; | ||
|
||
#[derive(Clone)] | ||
struct Point { | ||
lng: f64, | ||
lat: f64, | ||
} | ||
|
||
impl Point { | ||
pub fn distance(self, point: Point) -> f64 { | ||
let lng_sqrd = (point.lng - self.lng).powi(2); | ||
let lat_sqrd = (point.lat - self.lat).powi(2); | ||
(lng_sqrd + lat_sqrd).sqrt() | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_distance() { | ||
let point1 = Point {lng: 9., lat: 3.}; | ||
let point2 = Point {lng: 6., lat: -1.}; | ||
let distance = 5.; | ||
assert_eq!(distance, point1.clone().distance(point2.clone())); | ||
assert_eq!(distance, point2.clone().distance(point1.clone())); | ||
} |