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 "Никаких аргументов не было приведено."