diff options
| author | 2026-11-08 02:51:49 +0000 | |
|---|---|---|
| committer | 2026-11-08 02:51:49 +0000 | |
| commit | 80893fa3e272d9792ba1206f7006e8defeeccb6e (patch) | |
| tree | 1867dba062a481bf59313b0fbb16f416834a11a3 /src/peano-curve.hs | |
| download | lab-02-80893fa3e272d9792ba1206f7006e8defeeccb6e.tar.gz | |
Diffstat (limited to 'src/peano-curve.hs')
| -rw-r--r-- | src/peano-curve.hs | 87 |
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 @@ | |||
| 1 | module PeanoCurve (peanoCurve) where | ||
| 2 | |||
| 3 | import System.Environment (getArgs) | ||
| 4 | import Text.Read (readMaybe) | ||
| 5 | |||
| 6 | type Point = (Rational, Rational) | ||
| 7 | type Points = [Point] | ||
| 8 | |||
| 9 | splitNorthEast :: Point -> Rational -> Points | ||
| 10 | splitNorthEast (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 | |||
| 21 | splitNorthWest :: Point -> Rational -> Points | ||
| 22 | splitNorthWest (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 | |||
| 33 | splitSouthEast :: Point -> Rational -> Points | ||
| 34 | splitSouthEast (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 | |||
| 45 | splitSouthWest :: Point -> Rational -> Points | ||
| 46 | splitSouthWest (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 | |||
| 57 | advance :: Points -> Rational -> Int -> Int -> Points | ||
| 58 | advance points _ index 0 = [points !! index] | ||
| 59 | advance points a 0 iterations = | ||
| 60 | let advanced = splitNorthEast (head points) a | ||
| 61 | in concat [advance advanced (a/3) index (iterations - 1) | index <- [0..8]] | ||
| 62 | advance 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 | ||
| 70 | advance 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 | |||
| 76 | peanoCurve :: Int -> Points | ||
| 77 | peanoCurve iterations = advance [(1/2, 1/2)] 1 0 iterations | ||
| 78 | |||
| 79 | main :: IO () | ||
| 80 | main = 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 "Никаких аргументов не было приведено." | ||
