During your TSP you continually call sqrt while calculating distances. This is fairly wasteful, since you don't actually care about the root until the end of the function.
func dist2(lhs Point, rhs Point) float64 {
dx, dy := lhs.x - rhs.x, lhs.y - rhs.y
return (dx*dx) + (dy * dy)
}
func find_closest_2(points []Point) (float64, *Point, *Point, error) {
if len(points) < 2 {
return 0, nil, nil, errors.New("must be at least 2 points")
}
var first, second *Point
var shortest_dist float64 = math.MaxFloat64
for lhs := 0; lhs < len(points) - 1; lhs++ {
for rhs := lhs + 1; rhs < len(points); rhs++ {
d2 := dist2(lhs, rhs)
if d2 < shortest_dist {
first, second = &lhs, &rhs
shortest_dist = d2
}
}
}
return math.sqrt(shortest_dist), first, second, nil
}