-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.rs
67 lines (57 loc) · 1.59 KB
/
point.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::graph::Coordinate;
#[derive(PartialEq, Default, Clone, Debug)]
pub struct Point {
pub x: i64,
pub y: i64,
}
impl Point {
pub fn new(x: f64, y: f64) -> Self {
Self {
x: if x < 0.0 { 0 } else { x as i64 },
y: if y < 0.0 { 0 } else { y as i64 },
}
}
pub fn new_i64(x: i64, y: i64) -> Self {
Self {
x: if x < 0 { 0 } else { x },
y: if y < 0 { 0 } else { y },
}
}
#[cfg(test)]
pub(crate) fn distance(&self, point: &Self) -> f64 {
return (((self.x - point.x).pow(2) + (self.y - point.y).pow(2)) as f64).sqrt();
}
pub(crate) fn distance_coor(&self, coordinate: &Coordinate) -> f64 {
return ((self.x as f64 - coordinate.x).powf(2.0)
+ (self.y as f64 - coordinate.y).powf(2.0))
.sqrt();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new() {
let point = Point::new(-1.5, 6.6);
assert_eq!(Point { x: 0, y: 6 }, point);
}
#[test]
fn new_i64() {
let point = Point::new_i64(-2, 6);
assert_eq!(Point { x: 0, y: 6 }, point);
}
#[test]
fn distance() {
let point_1 = Point::new_i64(3, 6);
let point_2 = Point::new_i64(7, 10);
let distance = 32_f64.sqrt();
assert_eq!(point_1.distance(&point_2), distance);
}
#[test]
fn distance_coor() {
let point = Point::new_i64(3, 6);
let coordinate = Coordinate { x: 5.5, y: 8.5 };
let distance = 12.5_f64.sqrt();
assert_eq!(point.distance_coor(&coordinate), distance);
}
}