Sliced Sphere



  • Here's a little example that draws an animated sliced sphere, using fake shadow and sin() and cos() to give the illusion of 3D.

    def ellipse(center, wr, hr):
        cx, cy = center
        oval(cx - wr, cy - hr, wr * 2, hr * 2)
    
    canvasSize = 800
    radius = 0.9 * canvasSize / 2
    numEllipses = 12
    numFrames = 40
    sphereAngle = radians(20) # make radians from degrees
    ellipseHeight = sin(sphereAngle)
    heightCompensation = cos(sphereAngle)
    
    for frame in range(numFrames):
        t = (0.25 + frame / numFrames) % 1.0
        newPage(canvasSize, canvasSize)
        frameDuration(1/20)
        rect(0, 0, canvasSize, canvasSize)
        translate(canvasSize/2, canvasSize/2)
        fill(1)
        shadow((0, -canvasSize * 0.03), canvasSize * 0.03,
               (0, 0, 0, 0.6))
    
        for i in range(numEllipses):
            f = (i + (1 - t)) / (numEllipses)
            circleY = 2 * (f - 0.5)
            wr = radius * cos(asin(circleY))
            y = heightCompensation * radius * circleY
            ellipse((0, y), wr, ellipseHeight * wr)
    
    saveImage("SlicedSphere.gif")
    

    0_1517651812685_SlicedSphere-clean.png

    Somehow I failed to embed an animated gif in the post. Here's the animation: http://dailydrawbot.tumblr.com/post/170450438649/sliced-sphere


Log in to reply
 

Looks like your connection to DrawBot Forum was lost, please wait while we try to reconnect.