Code Sketch


Emergent behaviour
By: Anay Narendra Kamat
Category: Math
// Inspired from a facebook post
// Recreated in Kojo by Anay Kamat

cleari

setBackground(black)

def mainOuterCircle = Picture.circle(160).thatsFilledWith(red)
def circle = Picture.circle(10).thatsFilledWith(white)
def line = Picture.line(320,1).thatsTranslated(-160, 0).thatsStrokeColored(black)

animateWithState(0) { state =>
    erasePictures()
    mainOuterCircle.draw
    Stream.from(0).map(_*20).take((state / 200)+1).take(10).foreach { angle =>
        def lineAngle = 0 - angle
        def lineAngleInRadians = lineAngle.toRadians
        def positionOnLine = 150*Math.cos((state+180-lineAngle).toRadians)        
        circle.thatsTranslated(positionOnLine*Math.cos(lineAngleInRadians), positionOnLine*Math.sin(lineAngleInRadians)).draw
        if (state < 200*10){
            line.thatsRotated(lineAngle).draw
        }        
    }
    state+3
}