# Calculating Cartesian Neighbors Distance in Golang

## 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:

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```CartesianNeighborsDistance(3, 2, 1) -> {1.4142135624, 1.0}
CartesianNeighborsDistance(0, 0, 2) -> {1.0, 1.4142135624, 2.0, 2.2360679775, 2.8284271247}```Code language: plaintext (plaintext)```

## 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 := ; dx <= dy; dx++ {
squaredDistances[dx * dx + dy * dy] = struct{}{}
}
}
result := make([]float64, len(squaredDistances))
i :=
for k := range squaredDistances {
result[i] = math.Sqrt(float64(k))
i++
}
return result
}
```Code language: Go (go)```

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 := ; dx <= dy; dx++ {
squaredDistances[dx * dx + dy * dy] = struct{}{}
}
}
result := make([]float64, len(squaredDistances))
i :=
for k := range squaredDistances {
result[i] = math.Sqrt(float64(k))
i++
}
return result
}
```Code language: Go (go)```

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 := ; y <= x; y++ {
distSqrMap[x*x + y*y] = struct{}{}
}
}
for distSquared := range distSqrMap {
dists = append(dists, math.Sqrt(float64(distSquared)))
}
return
}
```Code language: Go (go)```

## 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})
})
})
```Code language: Go (go)```

See also  How to Check for Prime Numbers using Golang
Tags:
Subscribe
Notify of 