Calculating Cartesian Neighbors Distance in Golang

1 min read 388 words

The challenge

We have been searching for all the neighboring points in a Cartesian coordinate system. As we know each point in a coordinate system has eight neighboring points when we search it by range equal to 1, but now we will change the range by the third argument of our function (range is always greater than zero). For example, if range = 2, count of neighboring points = 24. In this challenge, a grid step is the same (= 1).

It is necessary to write a function that returns an array of unique distances between the given point and all neighboring points. You can round up the distance to 10 decimal places (as shown in the example). Distances inside the list don’t have to be sorted (any order is valid).

Examples:

CartesianNeighborsDistance(3, 2, 1) -> {1.4142135624, 1.0}
CartesianNeighborsDistance(0, 0, 2) -> {1.0, 1.4142135624, 2.0, 2.2360679775, 2.8284271247}

The solution in Golang

Option 1:

package solution
import "math"
func CartesianNeighborsDistance(x, y, r int) []float64 {
    squaredDistances := make(map[int]struct{})
    for dy := 1; dy <= r; dy++ {
        for dx := 0; dx <= dy; dx++ {
            squaredDistances[dx * dx + dy * dy] = struct{}{}
        }
    }
    result := make([]float64, len(squaredDistances))
    i := 0
    for k := range squaredDistances {
        result[i] = math.Sqrt(float64(k))
        i++
    }
    return result
}

Option 2:

package solution
import "math"
func CartesianNeighborsDistance(x, y, r int) []float64 {
    squaredDistances := make(map[int]struct{})
    for dy := 1; dy <= r; dy++ {
        for dx := 0; dx <= dy; dx++ {
            squaredDistances[dx * dx + dy * dy] = struct{}{}
        }
    }
    result := make([]float64, len(squaredDistances))
    i := 0
    for k := range squaredDistances {
        result[i] = math.Sqrt(float64(k))
        i++
    }
    return result
}

Option 3:

package solution
import "math"
func CartesianNeighborsDistance(x, y, r int) (dists []float64){
  distSqrMap := make(map[int]struct{})
  for x := 1; x <= r; x++ {
    for y := 0; y <= x; y++ {
      distSqrMap[x*x + y*y] = struct{}{}
    }
  }
  for distSquared := range distSqrMap {
    dists = append(dists, math.Sqrt(float64(distSquared)))
  }
  return
}

Test cases to validate our solution

package solution_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"      
)
func dotest(x, y, r int, exp []float64){
  var act = CartesianNeighborsDistance(x, y, r)
  var eq = AlmostEquals(SortedList(act), exp)
  Expect(eq).To(Equal("True"))
}
var _ = Describe("Tests", func() {     
   It("ExampleTest", func() {
     dotest(3, 2, 1, []float64{1.0, 1.4142135624})
   })
})
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags