Skip to content

Instantly share code, notes, and snippets.

@CSchank
Last active January 28, 2024 21:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CSchank/26596747f572921c256656d8a2ba95ab to your computer and use it in GitHub Desktop.
Save CSchank/26596747f572921c256656d8a2ba95ab to your computer and use it in GitHub Desktop.
-- Copy to macoutreach.rocks animation slot
myShapes model =
[
myFunction 5 -- replace 5 with different number for different depth
]
myFunction n =
if n <= 0 then
group []
else
group
[
]
-- Copy to macoutreach.rocks animation slot
-- loosely based on https://www.youtube.com/watch?v=SSdyo5BbRvM
myShapes model =
[
fallingSquares 20 -- turn up 20 for more recursion!
]
fallingSquares n =
if n == 0 then
group []
else
group
[
square (5 * toFloat n)
|> outlined (solid 1) black
|> rotate (degrees (toFloat (n - 1) * 4))
, fallingSquares (n-1)
]
-- Copy to macoutreach.rocks animation slot
myShapes model =
[
scaledSierpinski 5 -- turn up 5 for more recursion! Might get laggy...
]
-- accounts for the scaling
scaledSierpinski n =
sierpinski n
|> scale (2 ^ (-(toFloat n)))
sierpinski n =
if n == 0 then
group []
else
let size = 60 * (2 ^ toFloat n)
-- determine colour based on n
colour = if modBy 3 n == 2 then green else if modBy 3 n == 1 then lightBlue else black
in
group
[
triangle size
|> filled colour
|> rotate (degrees (-30))
, sierpinski (n-1)
|> move (0, size / 2)
, sierpinski (n-1)
|> move (-size / 2 * sin (degrees -60), -size / 2 * cos (degrees -60))
, sierpinski (n-1)
|> move ( size / 2 * sin (degrees -60), -size / 2 * cos (degrees -60))
]
myShapes model =
[ myBackground
, simpleSnowflake
]
simpleSnowflake =
let
divide level =
let
hex = ngon 6 level
|> filled (rgba 255 255 255 0.5)
in
if level > 4 then
let
children = divide (level * 0.75)
|> move (1.4 * level,0)
rotated angle = children |> rotate (degrees angle)
in
( hex ::
List.map rotated [0,60,120,180,240,300] )
|> group
else
group []
in
divide 7.5
snowflake =
let
oneBranch = branch 30
rotateBranch angle = oneBranch |> rotate (degrees angle)
in
List.map rotateBranch [0,60,120,180,240,300]
|> group
branch level =
if level > 1 then
let
newLevel = level * (0.4 + 0.1 * sin level)
outerBranch = branch newLevel
innerBranch = branch (0.5 * newLevel)
in
group
[ ngon 6 1
|> outlined (solid 1) (rgba 255 255 255 0.5)
|> scaleX level
|> move (level * 0.5, 0)
, outerBranch
|> rotate (degrees 60)
|> move (0.66*level,0)
, outerBranch
|> rotate (degrees -60)
|> move (0.66*level,0)
, innerBranch
|> rotate (degrees 60)
|> move (0.33*level,0)
, innerBranch
|> rotate (degrees -60)
|> move (0.33*level,0)
]
else
group []
myBackground = square 200 |> filled (rgb 50 50 255)
-- Copy to macoutreach.rocks animation slot
myShapes model =
[
spinner model.time 6 -- turn up 6 for more recursion! Might get laggy...
]
spinner time n =
if n < 3 then
group []
else
let
shadeOfRed = rgb (255-255/(toFloat n)) 0 0
rainbow =
case n + 3 of
10 -> red
9 -> orange
8 -> yellow
7 -> green
6 -> lightBlue
5 -> blue
4 -> purple
3 -> pink
otherwise -> black
colour = shadeOfRed
in
group
[
ngon n 10
|> filled colour
|> move (30, 30)
, ngon n 10
|> filled colour
|> move (-30,30)
, ngon n 10
|> filled colour
|> move (30, -30)
, ngon n 10
|> filled colour
|> move (-30, -30)
, spinner time (n-1)
|> move (30, 30)
, spinner time (n-1)
|> move (-30, 30)
, spinner time (n-1)
|> move (30, -30)
, spinner time (n-1)
|> move (-30, -30)
] |> scale (toFloat n/6)
|> rotate (degrees (50*time))
-- Copy to macoutreach.rocks animation slot
myShapes model =
let animateTree = modBy 13 (round (model.time*3))
in
[
tree 11 -- replace 11 with different number for different depth or with animateTree to get animation
|> move (0,-95)
|> scale 0.6
]
tree n =
if n == 0 then
group []
else
let
angle = 20
weirdRainbow = hsl (degrees 44) 0.6 (0.32 + toFloat n)
colour = darkBrown
in
group
[
rect 3 40
|> filled colour
|> move(0,20)
|> rotate (degrees angle)
, rect 3 40
|> filled colour
|> move(0,20)
|> rotate (degrees -angle)
, tree (n-1)
|> rotate (degrees -angle)
|> move (40*sin(degrees angle), 40*cos(degrees angle))
, tree (n-1)
|> rotate (degrees angle)
|> move (-40*sin(degrees angle), 40*cos(degrees angle))
] |> scale (1 - 1/(toFloat n + 1))
@CSchank
Copy link
Author

CSchank commented Mar 28, 2020

Code from today:

-- Copy to macoutreach.rocks animation slot

myShapes model =
  [
    myFunction 10 -- replace 5 with different number for different depth
  ]
  
myFunction n = 
  if n == 0 then
    group []
  else
    let
      colour = rgb (255-20*toFloat n) 0 (255-20*toFloat n)
    in
    group
      [
        square (10 * toFloat n) 
          |> outlined (solid 1) colour
          |> rotate (degrees (10 * toFloat (n-1)))
      , myFunction (n-1)
      ]

@CSchank
Copy link
Author

CSchank commented Oct 22, 2020

Code from October 22nd, 2020 for a rainbow Sierpinski's triangle:

-- Copy to macoutreach.rocks animation slot

myShapes model =
  [
    myFunction 60 model.time -- replace 5 with different number for different depth
  ]
  
myFunction n t = 
  if n <= 1 then
    group []
  else
    group
      [
        triangle n 
          |> filled (hsl (degrees (n*5 + 50*t)) 1 0.5)
          |> addOutline (solid 0.5) black
          |> rotate (degrees (5*t))
      , myFunction (n / 2) t
          |> move (0, n / 2)
      , myFunction (n / 2) t
          |> move (0, n / 2)
          |> rotate (degrees 120)
      , myFunction (n / 2) t
          |> move (0, n / 2)
          |> rotate (degrees -120)
      ]

@CSchank
Copy link
Author

CSchank commented Oct 23, 2020

Isometric "snowflake":

-- Copy to macoutreach.rocks animation slot

myShapes model =
  [
    myFunction 5 -- replace 5 with different number for different depth
  ]
  
myFunction n = 
  if n <= 0 then
    group []
  else
    let
      colour = rgb (255-20*toFloat n) 0 (255-20*toFloat n)
      size = toFloat (2 ^ n)
    in
    group
      [
        ngon 6 (2 ^ toFloat n)
          |> outlined (solid 1) (rgb (toFloat n*50) 200 100)
          |> rotate (degrees -30)
      , myFunction (n - 1)
          |> move (0, size/2+size/4)
          |> rotate (degrees 60)
      , myFunction (n - 1)
          |> move (0, size/2+size/4)
          |> rotate (degrees 180)
      , myFunction (n - 1)
          |> move (0, size/2+size/4)
          |> rotate (degrees -60)
      ]

@CSchank
Copy link
Author

CSchank commented Oct 23, 2020

Disco line snowflake

-- Copy to macoutreach.rocks animation slot

myShapes model =
  [
    myFunction 5 model.time -- replace 5 with different number for different depth
  ]
  
myFunction n t = 
  let 
    size = 2 ^ n * 2
    colour = hsl ((18 - n) * 20 + t) 1 0.5
  in
  if n <= 0 then
    group []
  else
    group
      [
        rect 0.5 size
          |> filled colour
          |> move (0, size / 2)
      , rect 0.5 size
          |> filled colour
          |> move (0, size / 2)
          |> rotate (degrees 72)
      , rect 0.5 size
          |> filled colour
          |> move (0, size / 2)
          |> rotate (degrees 144)
      , rect 0.5 size
          |> filled colour
          |> move (0, size / 2)
          |> rotate (degrees 216)
      , rect 0.5 size
          |> filled colour
          |> move (0, size / 2)
          |> rotate (degrees 288)
      , myFunction (n - 1) t
          |> move (0, size / 2)
      , myFunction (n - 1) t
          |> move (0, size / 2)
          |> rotate (degrees 72)
      , myFunction (n - 1) t
          |> move (0, size / 2)
          |> rotate (degrees 144)
      , myFunction (n - 1) t
          |> move (0, size / 2)
          |> rotate (degrees 216)
      , myFunction (n - 1) t
          |> move (0, size / 2)
          |> rotate (degrees 288)
      ] |> rotate (degrees 5*t)

@CSchank
Copy link
Author

CSchank commented Oct 23, 2020

Weird rainbow:

-- Copy to macoutreach.rocks animation slot

myShapes model =
  let
    n = 7
  in
  [
    myFunction n -- replace 5 with different number for different depth
      |> scale (70 / (2 ^ n))
      |> move (0, -25)
  ]
  
myFunction n = 
  let 
    size = 2 ^ toFloat n
    colour = hsl (degrees (toFloat n * 50)) 1 0.5
  in
  if n <= 0 then
    group []
  else
    group
      [
        wedge size 0.5 
          |> outlined (solid 1) colour
          |> rotate (degrees 90)
      , myFunction (n - 1)
          |> move (-size/2,0)
          |> scaleX -1
      , myFunction (n - 1)
          |> move (size/2,0)
          |> scaleX -1
      ]

@CSchank
Copy link
Author

CSchank commented Aug 26, 2021

Rotating falling squares

-- Your shapes go here!
-- You can use model.time to animate things :)
myShapes model =
  [
    myFunction model 10
  ]

myFunction model n =
  if n <= 0 then
    group []
  else
    group
      [
        square (10 * toFloat n)
          |> outlined (solid 2) black
          |> rotate (degrees (20 * toFloat n))
          |> rotate (degrees (toFloat n * 10 * model.time))
      , myFunction model (n - 1)
      ]

@CSchank
Copy link
Author

CSchank commented Aug 26, 2021

Rainbow circle spiral

image

-- Your shapes go here!
-- You can use model.time to animate things :)
myShapes model =
  [
    myFunction 200
  ]

myFunction n = 
  let 
    col = hsl (degrees <| toFloat n) 1 0.8
  in
    if n == 0 then
      group []
    else
    group
      [
        circle (toFloat n)
          |> outlined (solid 0.5) col
          |> move (0, toFloat n)
          |> rotate (degrees (2 * toFloat n))
      , myFunction (n-1)
      ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment