about summary refs log tree commit diff stats
path: root/Fallen/Point.hs
blob: 321f3b00cdecbe907aeb292009c9d1ff32ff97fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Fallen.Point
( Point,
  Direction(North, South, East, West),
  directions,
  distance,
  stepInDirection,
  opposite,
  randomPoint,
  dirToPoint
) where
  import System.Random
  
  type Point = (Int, Int)
  data Direction = North | South | East | West deriving (Show)
  
  -- directions :: [Direction]
  directions = [North, South, East, West]
  
  -- distance :: Point -> Point -> Int
  distance (x1, y1) (x2, y2) = ceiling . sqrt . fromIntegral $ (x1-x2)^2 + (y1-y2)^2
  
  -- stepInDirection :: Point -> Direction -> Point
  stepInDirection (x, y) dir = case dir of
    North -> (x, y-1)
    South -> (x, y+1)
    East -> (x+1, y)
    West -> (x-1, y)
  
  -- opposite :: Direction -> Direction
  opposite dir = case dir of
    North -> South
    South -> North
    East -> West
    West -> East
  
  -- randomPoint :: Int -> Int -> Int -> Int -> IO Point
  randomPoint minX minY maxX maxY rg = do
    let (x,rg1) = randomR (minX, maxX) rg :: (Int,StdGen)
    let (y,rg2) = randomR (minY, maxY) rg1  :: (Int,StdGen)
    ((x,y),rg2)
  
  -- dirToPoint :: Point -> Point -> [Direction]
  dirToPoint p1 p2 = horizDirToPoint p1 p2 ++ vertDirToPoint p1 p2 where
    horizDirToPoint (x1,y1) (x2,y2) = case compare x1 x2 of
      LT -> [East]
      EQ -> []
      GT -> [West]
    vertDirToPoint (x1,y1) (x2,y2) = case compare y1 y2 of
      LT -> [North]
      EQ -> []
      GT -> [South]
  
  instance Eq Direction where
    North == North = True
    South == South = True
    East == East = True
    West == West = True
    _ == _ = False