Created
August 22, 2014 16:00
-
-
Save kasbah/d0170cd0d4807d18f2a6 to your computer and use it in GitHub Desktop.
A binary fractal tree -- with only constant inputs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--license: CC-0 | |
--see: https://creativecommons.org/publicdomain/zero/1.0/ | |
--Elm version: http://share-elm.com/sprout/53ee4e81e4b07afa6f9844c9 | |
import FRP.Helm | |
import qualified FRP.Helm.Window as Window | |
import qualified FRP.Helm.Mouse as Mouse | |
data Branch = Branch { branch_x1 :: Double | |
, branch_y1 :: Double | |
, branch_x2 :: Double | |
, branch_y2 :: Double | |
, branch_w :: Double | |
, branch_a :: Double | |
, branch_l :: Int | |
, branch_len :: Double | |
} | |
branch :: Branch | |
-> (Double -> Double -> Double) | |
-> Double | |
-> Double | |
-> Branch | |
branch (Branch x1 y1 x2 y2 w a l len) fn delta_a sc = Branch | |
{ branch_x1 = x2 | |
, branch_y1 = y2 | |
, branch_x2 = x2 - len * sc * cos (degrees (fn a delta_a)) | |
, branch_y2 = y2 - len * sc * sin (degrees (fn a delta_a)) | |
, branch_w = w * 0.75 | |
, branch_a = fn a delta_a | |
, branch_l = l + 1 | |
, branch_len = len * sc | |
} | |
drawBranch :: Branch -> Form | |
drawBranch (Branch x1 y1 x2 y2 w _ _ _) = | |
traced (defaultLine {lineWidth = w, lineColor=red}) | |
$ path [(x1,y1),(x2,y2)] | |
branches :: Branch | |
-> (Double -> Double -> Double) | |
-> Double | |
-> Double | |
-> [Branch] | |
branches p@(Branch _ _ _ _ _ _ l _) fn delta_a sc = | |
if l < 10 | |
then let relTrunk = branch p fn delta_a sc | |
in relTrunk : (branches relTrunk (+) delta_a sc) | |
++ (branches relTrunk (-) delta_a sc) | |
else [] | |
trunk :: Branch | |
trunk = Branch | |
{ branch_x1 = 0 | |
, branch_y1 = 0 | |
, branch_x2 = 0 | |
, branch_y2 = -50 | |
, branch_w = 10 | |
, branch_a = 90 | |
, branch_l = 0 | |
, branch_len = 50 | |
} | |
tree :: Double -> Double -> [Branch] | |
tree delta_a sc = | |
(branches trunk (+) delta_a sc) | |
++ [trunk] | |
++ (branches trunk (-) delta_a sc) | |
fraction1 eng = | |
(\x w -> fromIntegral x / fromIntegral w) | |
<~ Mouse.x ~~ Window.width eng | |
fraction2 eng = | |
(\y h -> 1.0 - fromIntegral y / fromIntegral h) | |
<~ Mouse.y ~~ Window.height eng | |
scene frac1 frac2 w h = | |
centeredCollage w h $ map drawBranch $ tree (90 * frac1) frac2 | |
main :: IO () | |
main = do | |
engine <- startup defaultConfig | |
run engine $ scene | |
<~ constant 0.1 | |
~~ constant 0.8 | |
~~ constant 800 | |
~~ constant 600 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment