Search code examples
pythonprocessing

Continuously spin a matrix of objects in Processing


I have a bunch of lines that draw a composite circle. I want to make this composite circle spin continuously but can't figure it out. To further explain what I want, it will be kinda like a loading spinner.

def setup():
    size(640, 640)
    frameRate(30)

def draw():
    background(0)
    stroke(100, 100, 100)
    strokeWeight(4)
    translate(width/2, height/2)

    # I want to rotate/make this composite circle spin continuously
    make_circle()


def make_circle():
    inner_radius = width / 8
    line_length = width / 3
    rx = sqrt(2) * inner_radius
    ry = rx

    steps = 10
    pushMatrix()
    for dtheta in range(0, 360, steps):
        dtheta = radians(dtheta)
        coss = cos(dtheta)
        sinn = sin(dtheta)
        
        line(inner_radius * coss, inner_radius * sinn, line_length * coss, line_length * sinn)
    popMatrix()

Solution

  • You need to rotate the complete shape. Add an angle variable in global namespace. Increment the angle inn every frame and rotate the shape by the angle (by default the unit of the angle is radians):

    angle = 0
    
    def draw():
        global angle
        
        background(0)
        stroke(100, 100, 100)
        strokeWeight(4)
        translate(width/2, height/2)
        
        rotate(radians(angle))
        angle += 1
        make_circle()