From 80893fa3e272d9792ba1206f7006e8defeeccb6e Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 05:51:49 +0300 Subject: publish --- src/peano-curve.hs | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/peano-curve.hs (limited to 'src/peano-curve.hs') 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 @@ +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 "Никаких аргументов не было приведено." -- cgit v1.3.1