Browse Source

Changing distance calculation to use that odd impl

master
Khaled Nassar 4 years ago
parent
commit
c6be62e98e
3 changed files with 18 additions and 29 deletions
  1. +17
    -11
      strategies/common.go
  2. +1
    -1
      strategies/linker.go
  3. +0
    -17
      strategies/simple.go

+ 17
- 11
strategies/common.go View File

@@ -13,21 +13,27 @@ const (
Enemy StarAffiliation = 2
)

func flightTurnDistance(x, y int, star *models.Star) int {
pointDistance := distanceBetweenPoints(x, y, star.X, star.Y)

if pointDistance == 0 {
return 0
func calculateDistanceCeiling(source *models.Star, target *models.Star) int {
squareDist := ((source.X - target.X) * (source.X - target.X)) + ((source.Y - target.Y) * (source.Y - target.Y))

lb := 1
ub := 300 * 2
for lb < ub {
mb := (lb + ub) / 2
if mb*mb >= squareDist {
ub = mb
} else {
lb = mb + 1
}
}

return int(math.Ceil(pointDistance / 10))
return lb
}

func distanceBetweenPoints(x1, y1, x2, y2 int) float64 {
// Is it the same point? The distance is defined as 0
if x1 == x2 && y1 == y2 {
return 0
func flightTurnDistance(source, target *models.Star) int {
if source.Idx == target.Idx {
return 0;
}

return math.Sqrt(math.Pow(float64(x1-x2), 2) + math.Pow(float64(y1-y2), 2))
return int(math.Ceil(float64(calculateDistanceCeiling(source, target)) / 10.0))
}

+ 1
- 1
strategies/linker.go View File

@@ -40,7 +40,7 @@ func (ls *LinkerStrategy) Execute(stars []models.Star, flights []models.Flight,
continue
}

flightDistance = flightTurnDistance(src.X, src.Y, &dst)
flightDistance = flightTurnDistance(&src, &dst)

friendlyDistanceMatrix[src][dst] = flightDistance
}

+ 0
- 17
strategies/simple.go View File

@@ -73,20 +73,3 @@ func (s *SimpleStrategy) Execute(stars []models.Star, flights []models.Flight, l

fmt.Println("done")
}

func calculateDistanceCeiling(source *models.Star, target *models.Star) int {
squareDist := ((source.X - target.X) * (source.X - target.X)) + ((source.Y - target.Y) * (source.Y - target.Y))

lb := 1
ub := 300 * 2
for lb < ub {
mb := (lb + ub) / 2
if mb*mb >= squareDist {
ub = mb
} else {
lb = mb + 1
}
}

return lb
}

Loading…
Cancel
Save