aboutsummaryrefslogtreecommitdiff
path: root/physics
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-07-24 15:05:22 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-07-24 15:05:22 +0200
commit337dee61ada57e1d44079822abc437b8fc168843 (patch)
tree1a87534523882d8fc1d862df017396dbdd376f3b /physics
parente6e4ce18083ae5552563eab3291ac1642371d025 (diff)
refactor: Extract closest
Diffstat (limited to 'physics')
-rw-r--r--physics/geometry.go18
1 files changed, 17 insertions, 1 deletions
diff --git a/physics/geometry.go b/physics/geometry.go
index 1902fdf..9949466 100644
--- a/physics/geometry.go
+++ b/physics/geometry.go
@@ -13,7 +13,7 @@ func Distance(p1, p2 Point) float64 {
}
func Length(p Point) float64 {
- return Distance(p, Point{X:0,Y:0})
+ return Distance(p, Point{X: 0, Y: 0})
}
func Normalize(p Point) Point {
@@ -28,3 +28,19 @@ func AdjacentPoints(from Point) []Point {
from.Add(Point{X: 100, Y: 0}),
}
}
+
+func Closest(from Point, candidates []Point, moveMap MoveMap) (Point, int) {
+ var closest Point
+ closestDistance := math.MaxInt
+ for _, dest := range candidates {
+ path, ok := SearchPath(from, dest, moveMap)
+ if !ok {
+ continue
+ }
+ if len(path) < closestDistance {
+ closest = dest
+ closestDistance = len(path)
+ }
+ }
+ return closest, closestDistance
+}