Актуальный код в fern.hs

This commit is contained in:
2024-11-14 19:53:56 +03:00
parent ee1bcd7551
commit b8b5dd1d2b
2 changed files with 34 additions and 32 deletions

View File

@@ -1,4 +1,4 @@
import System.Random (randomRIO) import System.Random (StdGen, mkStdGen, randomR)
type Point = (Float, Float) type Point = (Float, Float)
@@ -21,22 +21,20 @@ applyTransformation point random
| random < 0.93 = transformation3 point | random < 0.93 = transformation3 point
| otherwise = transformation4 point | otherwise = transformation4 point
genNextPoint :: Point -> IO Point genNextPoint :: StdGen -> Point -> (Point, StdGen)
genNextPoint point = do genNextPoint gen point =
random <- randomRIO (0.0, 1.0 :: Float) let (random, newGen) = randomR (0.0, 1.0 :: Float) gen
return $ applyTransformation point random in (applyTransformation point random, newGen)
barnsleyFern :: Point -> Int -> IO [Point] barnsleyFern :: StdGen -> Point -> Int -> [Point]
barnsleyFern _ 0 = return [] barnsleyFern _ _ 0 = []
barnsleyFern startPoint n = do barnsleyFern gen startPoint n =
x' <- genNextPoint startPoint let (nextPoint, newGen) = genNextPoint gen startPoint
xs <- barnsleyFern x' (n - 1) in startPoint : barnsleyFern newGen nextPoint (n - 1)
return (startPoint : xs)
n = 1000
randomSeed = 17
main :: IO () main :: IO ()
main = do main = print $ barnsleyFern (mkStdGen randomSeed) (0, 0) n
putStrLn "Укажите количество шагов рекурсии:"
input <- getLine
let n = read input :: Int
fractal <- barnsleyFern (0, 0) n
print fractal

View File

@@ -1,4 +1,6 @@
import System.Random (StdGen, mkStdGen, randomR) -- Не стоит в первой же лабе использовать монады, можно и без них
import System.Random (randomRIO)
type Point = (Float, Float) type Point = (Float, Float)
@@ -21,20 +23,22 @@ applyTransformation point random
| random < 0.93 = transformation3 point | random < 0.93 = transformation3 point
| otherwise = transformation4 point | otherwise = transformation4 point
genNextPoint :: StdGen -> Point -> (Point, StdGen) genNextPoint :: Point -> IO Point
genNextPoint gen point = genNextPoint point = do
let (random, newGen) = randomR (0.0, 1.0 :: Float) gen random <- randomRIO (0.0, 1.0 :: Float)
in (applyTransformation point random, newGen) return $ applyTransformation point random
barnsleyFern :: StdGen -> Point -> Int -> [Point] barnsleyFern :: Point -> Int -> IO [Point]
barnsleyFern _ _ 0 = [] barnsleyFern _ 0 = return []
barnsleyFern gen startPoint n = barnsleyFern startPoint n = do
let (nextPoint, newGen) = genNextPoint gen startPoint x' <- genNextPoint startPoint
in startPoint : barnsleyFern newGen nextPoint (n - 1) xs <- barnsleyFern x' (n - 1)
return (startPoint : xs)
n = 1000
randomSeed = 17
main :: IO () main :: IO ()
main = print $ barnsleyFern (mkStdGen randomSeed) (0, 0) n main = do
putStrLn "Укажите количество шагов рекурсии:"
input <- getLine
let n = read input :: Int
fractal <- barnsleyFern (0, 0) n
print fractal