Files
functional-programming/lab2/fern_with_mod.hs
2024-11-09 18:42:52 +03:00

34 lines
1.0 KiB
Haskell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Не работает, потому что важно именно в случайном порядке применять трансформации
type Point = (Float, Float)
transformation1 :: Point -> Point
transformation1 (_, y) = (0, 0.16 * y)
transformation2 :: Point -> Point
transformation2 (x, y) = (0.85 * x + 0.04 * y, -0.04 * x + 0.85 * y + 1.6)
transformation3 :: Point -> Point
transformation3 (x, y) = (0.2 * x - 0.26 * y, 0.23 * x + 0.22 * y + 1.6)
transformation4 :: Point -> Point
transformation4 (x, y) = (-0.15 * x + 0.28 * y, 0.26 * x + 0.24 * y + 0.44)
applyTransformation :: Point -> Int -> Point
applyTransformation (x, y) n
| r == 0 = transformation1 (x, y)
| r <= 85 = transformation2 (x, y)
| r <= 92 = transformation3 (x, y)
| otherwise = transformation4 (x, y)
where
r = n `mod` 100
barnsleyFern :: Point -> Int -> [Point]
barnsleyFern _ 0 = []
barnsleyFern point n = point : barnsleyFern (applyTransformation point n) (n - 1)
main :: IO ()
main = do
let fractal = barnsleyFern (0, 0) 1000
print fractal