LCOV - code coverage report
Current view: top level - geometry - geometry.go Coverage Total Hit
Test: coverage.lcov Lines: 96.2 % 53 51
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package geometry
       2              : 
       3              : import (
       4              :         "math"
       5              : 
       6              :         "github.com/kijimaD/ruins/internal/consts"
       7              : )
       8              : 
       9              : // Distance は2点間のユークリッド距離を返す
      10              : func Distance(x0, y0, x1, y1 float64) float64 {
      11            1 :         dx := x0 - x1
      12            1 :         dy := y0 - y1
      13            1 :         return math.Sqrt(dx*dx + dy*dy)
      14            1 : }
      15              : 
      16              : // IsAdjacent は2点が隣接しているかを判定する。チェビシェフ距離が1以下で同一座標でない場合にtrueを返す
      17              : func IsAdjacent[T consts.Numeric](a, b consts.Coord[T]) bool {
      18            1 :         dx := Abs(a.X - b.X)
      19            1 :         dy := Abs(a.Y - b.Y)
      20            1 :         if dx == 0 && dy == 0 {
      21            1 :                 return false
      22            1 :         }
      23            1 :         return dx <= 1 && dy <= 1
      24              : }
      25              : 
      26              : // BresenhamLine はBresenhamアルゴリズムで2点間の座標列を返す。始点と終点は含まない。
      27              : // 純粋な整数グリッドのアルゴリズムなので、ドメイン単位でなく Coord[int] で扱う
      28              : func BresenhamLine(from, to consts.Coord[int]) []consts.Coord[int] {
      29            1 :         var points []consts.Coord[int]
      30            1 : 
      31            1 :         dx := Abs(to.X - from.X)
      32            1 :         dy := Abs(to.Y - from.Y)
      33            1 :         sx := 1
      34            1 :         if from.X > to.X {
      35            1 :                 sx = -1
      36            1 :         }
      37            1 :         sy := 1
      38            1 :         if from.Y > to.Y {
      39            0 :                 sy = -1
      40            0 :         }
      41            1 :         err := dx - dy
      42            1 : 
      43            1 :         cur := from
      44            1 :         for {
      45            1 :                 if cur != from && cur != to {
      46            1 :                         points = append(points, cur)
      47            1 :                 }
      48            1 :                 if cur == to {
      49            1 :                         break
      50              :                 }
      51            1 :                 e2 := 2 * err
      52            1 :                 if e2 > -dy {
      53            1 :                         err -= dy
      54            1 :                         cur.X += sx
      55            1 :                 }
      56            1 :                 if e2 < dx {
      57            1 :                         err += dx
      58            1 :                         cur.Y += sy
      59            1 :                 }
      60              :         }
      61              : 
      62            1 :         return points
      63              : }
      64              : 
      65              : // ChebyshevDistance は2点間のチェビシェフ距離を返す。距離はタイル数などの int
      66              : func ChebyshevDistance[T consts.Numeric](a, b consts.Coord[T]) int {
      67            1 :         dx := Abs(a.X - b.X)
      68            1 :         dy := Abs(a.Y - b.Y)
      69            1 :         if dx > dy {
      70            1 :                 return int(dx)
      71            1 :         }
      72            1 :         return int(dy)
      73              : }
      74              : 
      75              : // WithinRect は center から見て coord が縦横別の半径 ext の矩形に入るかを返す。境界を含む。
      76              : func WithinRect[T consts.Numeric](coord, center, ext consts.Coord[T]) bool {
      77            1 :         return Abs(coord.X-center.X) <= ext.X && Abs(coord.Y-center.Y) <= ext.Y
      78            1 : }
      79              : 
      80              : // Abs は絶対値を返す。int だけでなく consts.Tile/WorldPixel などの単位型でも使える
      81              : func Abs[T consts.Numeric](x T) T {
      82            1 :         if x < 0 {
      83            1 :                 return -x
      84            1 :         }
      85            1 :         return x
      86              : }
        

Generated by: LCOV version 2.0-1