1package main 2 3import "math" 4 5// The piece of a hull under a point, if any. Each piece's own outline decides, and 6// where two outlines overlap the nearer centre wins, so a click in the seam between 7// two pieces picks the one it is closest to. 8func hullPieceAt(p *player, x, y float64) int { 9 id, best := 0, math.MaxFloat64 10 for _, q := range p.Pieces { 11 wx, wy := hullToWorld(p, q.LX, q.LY) 12 if d := math.Hypot(wx-x, wy-y); d < best && pieceAt(q.Kind, wx, wy, p.H, x, y) { 13 best, id = d, q.ID 14 } 15 } 16 return id 17} 18 19func hullToWorld(p *player, lx, ly float64) (float64, float64) { 20 c, s := math.Cos(p.H), math.Sin(p.H) 21 return p.X + lx*c - ly*s, p.Y + lx*s + ly*c 22} 23 24func hullToLocal(p *player, x, y float64) (float64, float64) { 25 return inward(p.X, p.Y, p.H, x, y) 26} 27 28func wrap(a float64) float64 { return math.Atan2(math.Sin(a), math.Cos(a)) } 29 30func finite(f float64) bool { return !math.IsNaN(f) && !math.IsInf(f, 0) }