blob: 25f583e875206f087fbdce28dc9b23dedd56baac (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#[derive(Debug, Clone)]
pub struct Gene {
pub x: f64,
pub y: f64,
pub name: String,
}
impl Gene {
// Eucledian distance between two points
pub fn dist(&self, other: &Self) -> f64 {
((self.x - other.x).powi(2) + (self.y - other.y).powi(2)).sqrt()
}
}
#[test]
fn test_dist() {
let g1 = Gene { x: 4.0, y: 1.0, name: "".to_string() };
let g2 = Gene { x: 3.0, y: 0.0, name: "".to_string() };
assert_eq!(format!("{:.3}", g1.dist(&g2)), "1.414");
}
|