aboutsummaryrefslogtreecommitdiff
path: root/src/peano-curve.hs
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 02:51:49 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 02:51:49 +0000
commit80893fa3e272d9792ba1206f7006e8defeeccb6e (patch)
tree1867dba062a481bf59313b0fbb16f416834a11a3 /src/peano-curve.hs
downloadlab-02-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/peano-curve.hs')
-rw-r--r--src/peano-curve.hs87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/peano-curve.hs b/src/peano-curve.hs
new file mode 100644
index 0000000..54461bd
--- /dev/null
+++ b/src/peano-curve.hs
@@ -0,0 +1,87 @@
1module PeanoCurve (peanoCurve) where
2
3import System.Environment (getArgs)
4import Text.Read (readMaybe)
5
6type Point = (Rational, Rational)
7type Points = [Point]
8
9splitNorthEast :: Point -> Rational -> Points
10splitNorthEast (x, y) a =
11 [(x - a/3, y - a/3),
12 (x - a/3, y ),
13 (x - a/3, y + a/3),
14 (x, y + a/3),
15 (x, y ),
16 (x, y - a/3),
17 (x + a/3, y - a/3),
18 (x + a/3, y ),
19 (x + a/3, y + a/3)]
20
21splitNorthWest :: Point -> Rational -> Points
22splitNorthWest (x, y) a =
23 [(x + a/3, y - a/3),
24 (x + a/3, y ),
25 (x + a/3, y + a/3),
26 (x, y + a/3),
27 (x, y ),
28 (x, y - a/3),
29 (x - a/3, y - a/3),
30 (x - a/3, y ),
31 (x - a/3, y + a/3)]
32
33splitSouthEast :: Point -> Rational -> Points
34splitSouthEast (x, y) a =
35 [(x - a/3, y + a/3),
36 (x - a/3, y ),
37 (x - a/3, y - a/3),
38 (x, y - a/3),
39 (x, y ),
40 (x, y + a/3),
41 (x + a/3, y + a/3),
42 (x + a/3, y ),
43 (x + a/3, y - a/3)]
44
45splitSouthWest :: Point -> Rational -> Points
46splitSouthWest (x, y) a =
47 [(x + a/3, y + a/3),
48 (x + a/3, y ),
49 (x + a/3, y - a/3),
50 (x, y - a/3),
51 (x, y ),
52 (x, y + a/3),
53 (x - a/3, y + a/3),
54 (x - a/3, y ),
55 (x - a/3, y - a/3)]
56
57advance :: Points -> Rational -> Int -> Int -> Points
58advance points _ index 0 = [points !! index]
59advance points a 0 iterations =
60 let advanced = splitNorthEast (head points) a
61 in concat [advance advanced (a/3) index (iterations - 1) | index <- [0..8]]
62advance points a index 1 =
63 let (cx, cy) = (points !! index)
64 (lx, ly) = last $ advance points a (index - 1) 1
65 (dx, dy) = (cx - lx, cy - ly)
66 in if dy > 0 && dx < 0 then splitNorthWest (cx, cy) a
67 else if dy > 0 && dx > 0 then splitNorthEast (cx, cy) a
68 else if dy < 0 && dx < 0 then splitSouthWest (cx, cy) a
69 else splitSouthEast (cx, cy) a
70advance points a index iterations =
71 let advanced = concat [advance points a i 1 | i <- [0..index]]
72 from = 9*index
73 to = from + 8
74 in concat [advance advanced (a/3) i (iterations - 1) | i <- [from..to]]
75
76peanoCurve :: Int -> Points
77peanoCurve iterations = advance [(1/2, 1/2)] 1 0 iterations
78
79main :: IO ()
80main = do
81 args <- getArgs
82 case args of
83 (arg:_) -> do
84 case readMaybe arg :: Maybe Int of
85 Just i -> print $ map (\n -> peanoCurve n) [1..i]
86 Nothing -> error "Первый аргумент не является допустимым числом."
87 [] -> error "Никаких аргументов не было приведено."