Created
January 6, 2018 17:59
-
-
Save ayazhafiz/01520e6b6270af29c84174a27561206f to your computer and use it in GitHub Desktop.
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
struct Delta { | |
x: [i8; 4], | |
y: [i8; 4], | |
} | |
fn geometric_center(points: &[[f64; 2]]) -> [f64; 2] { | |
const DELTA: Delta = Delta { | |
x: [-1, 0, 1, 0], // movement in `x` direction | |
y: [0, 1, 0, -1], // movement in `y` direction | |
}; | |
const EPSILON: f64 = 0.001; // some acceptable margin | |
let arr_length = points.len() as f64; | |
let mut center = points.iter().fold([0., 0.], |avg, point| { | |
let x = avg[0] + (point[0] / arr_length); | |
let y = avg[1] + (point[1] / arr_length); | |
[x, y] | |
}); // starts off as the median | |
let mut step = 10.; // search radius | |
let mut score = cost(&points, ¢er); | |
while step > EPSILON { | |
let mut improved = false; | |
for i in 0..DELTA.x.len() { | |
let _center = [ | |
center[0] + DELTA.x[i] as f64 * step, | |
center[1] + DELTA.y[i] as f64 * step, | |
]; | |
let _score = cost(&points, &_center); | |
if _score < score { | |
center = _center; | |
score = _score; | |
improved = !improved; | |
break; | |
} | |
} | |
if !improved { | |
step /= 2.; | |
} | |
} | |
center | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment