-
-
Notifications
You must be signed in to change notification settings - Fork 359
[Haskell] Barnsley Fern #851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Data.Array (Array, bounds, elems, listArray, (!)) | ||
import Data.List (intercalate) | ||
import System.Random | ||
|
||
data Point = Point Double Double | ||
|
||
chaosGame :: RandomGen g => g -> Int -> Array Int (Double, (Point -> Point)) -> [Point] | ||
chaosGame g n hutchinson = take n points | ||
where | ||
(x, g') = random g | ||
(y, g'') = random g' | ||
|
||
cumulProbabilities = scanl1 (+) $ map fst $ elems hutchinson | ||
to_choice x = length $ takeWhile (x >) cumulProbabilities | ||
|
||
jiegillet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
picks = map to_choice $ randomRs (0, 1) g'' | ||
step = fmap snd hutchinson | ||
|
||
points = Point x y : zipWith (step !) picks points | ||
|
||
affine :: (Double, Double, Double, Double) -> (Double, Double) -> Point -> Point | ||
affine (xx, xy, yx, yy) (a, b) (Point x y) = Point (a + xx * x + xy * y) (b + yx * x + yy * y) | ||
|
||
showPoint :: Point -> String | ||
showPoint (Point x y) = show x ++ "\t" ++ show y | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. {- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I usually try to stay away from |
||
main :: IO () | ||
main = do | ||
g <- newStdGen | ||
let barnsley = | ||
listArray | ||
(0, 3) | ||
[ (0.01, affine (0, 0, 0, 0.16) (0, 0)), | ||
(0.85, affine (0.85, 0.04, -0.04, 0.85) (0, 1.6)), | ||
(0.07, affine (0.2, -0.26, 0.23, 0.22) (0, 1.6)), | ||
(0.07, affine (-0.15, 0.28, 0.26, 0.24) (0, 0.44)) | ||
] | ||
points = chaosGame g 100000 barnsley | ||
|
||
writeFile "out.dat" $ intercalate "\n" $ map showPoint points |
Uh oh!
There was an error while loading. Please reload this page.