aboutsummaryrefslogtreecommitdiff
path: root/src/peano-curve.hs
blob: 54461bd7b5f786b985c8f8ef7e9002298f4aeb8e (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module PeanoCurve (peanoCurve) where

import System.Environment (getArgs)
import Text.Read (readMaybe)

type Point = (Rational, Rational)
type Points = [Point]

splitNorthEast :: Point -> Rational -> Points
splitNorthEast (x, y) a =
  [(x - a/3, y - a/3),
   (x - a/3, y      ),
   (x - a/3, y + a/3),
   (x,       y + a/3),
   (x,       y      ),
   (x,       y - a/3),
   (x + a/3, y - a/3),
   (x + a/3, y      ),
   (x + a/3, y + a/3)]

splitNorthWest :: Point -> Rational -> Points
splitNorthWest (x, y) a =
  [(x + a/3, y - a/3),
   (x + a/3, y      ),
   (x + a/3, y + a/3),
   (x,       y + a/3),
   (x,       y      ),
   (x,       y - a/3),
   (x - a/3, y - a/3),
   (x - a/3, y      ),
   (x - a/3, y + a/3)]

splitSouthEast :: Point -> Rational -> Points
splitSouthEast (x, y) a =
  [(x - a/3, y + a/3),
   (x - a/3, y      ),
   (x - a/3, y - a/3),
   (x,       y - a/3),
   (x,       y      ),
   (x,       y + a/3),
   (x + a/3, y + a/3),
   (x + a/3, y      ),
   (x + a/3, y - a/3)]

splitSouthWest :: Point -> Rational -> Points
splitSouthWest (x, y) a =
  [(x + a/3, y + a/3),
   (x + a/3, y      ),
   (x + a/3, y - a/3),
   (x,       y - a/3),
   (x,       y      ),
   (x,       y + a/3),
   (x - a/3, y + a/3),
   (x - a/3, y      ),
   (x - a/3, y - a/3)]

advance :: Points -> Rational -> Int -> Int -> Points
advance points _ index 0 = [points !! index]
advance points a 0 iterations =
  let advanced = splitNorthEast (head points) a
  in concat [advance advanced (a/3) index (iterations - 1) | index <- [0..8]]
advance points a index 1 =
  let (cx, cy) = (points !! index)
      (lx, ly) = last $ advance points a (index - 1) 1
      (dx, dy) = (cx - lx, cy - ly)
  in   if dy > 0 && dx < 0 then splitNorthWest (cx, cy) a
  else if dy > 0 && dx > 0 then splitNorthEast (cx, cy) a
  else if dy < 0 && dx < 0 then splitSouthWest (cx, cy) a
  else                          splitSouthEast (cx, cy) a
advance points a index iterations =
  let advanced = concat [advance points a i 1 | i <- [0..index]]
      from = 9*index
      to = from + 8
  in concat [advance advanced (a/3) i (iterations - 1) | i <- [from..to]]

peanoCurve :: Int -> Points
peanoCurve iterations = advance [(1/2, 1/2)] 1 0 iterations

main :: IO ()
main = do
  args <- getArgs
  case args of
    (arg:_) -> do
      case readMaybe arg :: Maybe Int of
        Just i -> print $ map (\n -> peanoCurve n) [1..i]
        Nothing  -> error "Первый аргумент не является допустимым числом."
    [] -> error "Никаких аргументов не было приведено."