diff options
Diffstat (limited to 'Fallen/Point.hs')
-rw-r--r-- | Fallen/Point.hs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/Fallen/Point.hs b/Fallen/Point.hs new file mode 100644 index 0000000..321f3b0 --- /dev/null +++ b/Fallen/Point.hs | |||
@@ -0,0 +1,58 @@ | |||
1 | module Fallen.Point | ||
2 | ( Point, | ||
3 | Direction(North, South, East, West), | ||
4 | directions, | ||
5 | distance, | ||
6 | stepInDirection, | ||
7 | opposite, | ||
8 | randomPoint, | ||
9 | dirToPoint | ||
10 | ) where | ||
11 | import System.Random | ||
12 | |||
13 | type Point = (Int, Int) | ||
14 | data Direction = North | South | East | West deriving (Show) | ||
15 | |||
16 | -- directions :: [Direction] | ||
17 | directions = [North, South, East, West] | ||
18 | |||
19 | -- distance :: Point -> Point -> Int | ||
20 | distance (x1, y1) (x2, y2) = ceiling . sqrt . fromIntegral $ (x1-x2)^2 + (y1-y2)^2 | ||
21 | |||
22 | -- stepInDirection :: Point -> Direction -> Point | ||
23 | stepInDirection (x, y) dir = case dir of | ||
24 | North -> (x, y-1) | ||
25 | South -> (x, y+1) | ||
26 | East -> (x+1, y) | ||
27 | West -> (x-1, y) | ||
28 | |||
29 | -- opposite :: Direction -> Direction | ||
30 | opposite dir = case dir of | ||
31 | North -> South | ||
32 | South -> North | ||
33 | East -> West | ||
34 | West -> East | ||
35 | |||
36 | -- randomPoint :: Int -> Int -> Int -> Int -> IO Point | ||
37 | randomPoint minX minY maxX maxY rg = do | ||
38 | let (x,rg1) = randomR (minX, maxX) rg :: (Int,StdGen) | ||
39 | let (y,rg2) = randomR (minY, maxY) rg1 :: (Int,StdGen) | ||
40 | ((x,y),rg2) | ||
41 | |||
42 | -- dirToPoint :: Point -> Point -> [Direction] | ||
43 | dirToPoint p1 p2 = horizDirToPoint p1 p2 ++ vertDirToPoint p1 p2 where | ||
44 | horizDirToPoint (x1,y1) (x2,y2) = case compare x1 x2 of | ||
45 | LT -> [East] | ||
46 | EQ -> [] | ||
47 | GT -> [West] | ||
48 | vertDirToPoint (x1,y1) (x2,y2) = case compare y1 y2 of | ||
49 | LT -> [North] | ||
50 | EQ -> [] | ||
51 | GT -> [South] | ||
52 | |||
53 | instance Eq Direction where | ||
54 | North == North = True | ||
55 | South == South = True | ||
56 | East == East = True | ||
57 | West == West = True | ||
58 | _ == _ = False | ||