Brian Carbone
10/28/2019, 2:53 PMval Array<Coordinate>.centroid
get() = fold(mutableMapOf<Double, Double>()) { acc, coord -> acc.also { it[coord.x] = coord.y } }.run {
Coordinate(keys.average(), values.average())
}
Is there a better way to get the average coordinate from an array of coordinates? (Coordinate is a simple data class holding two doubles, x and y)Dominaezzz
10/28/2019, 3:13 PMval Array<Coordinate>.centroid
get() = associateBy({ it.x }, { it.y }).run {
Coordinate(keys.average(), values.average())
}
Brian Carbone
10/28/2019, 3:15 PMBrian Carbone
10/28/2019, 3:15 PMDominaezzz
10/28/2019, 3:16 PMval Array<Coordinate>.centroid
get() = Coordinate(map { it.x }.average(), map { it.y }.average())
Sadly there's no averageBy
.Brian Carbone
10/28/2019, 3:17 PMBrian Carbone
10/28/2019, 3:18 PM