Need some help with translate() and rotate()

Hello @adora,

You are rotating about the corner of the shape.
Translate it after the rotation to center it.

Example:


def setup():
    size(300, 300)

x = 0
def draw():
    global x
    background(255)
    
    translate(width/2, height/2) # (0, 0) is center of canvas
    
    strokeWeight(2);
    rotate(radians(x))
    #translate(-50, -25)         #uncomment this
    
    fill(255, 0, 0);
    noStroke()
    
    beginShape()
    rect(0, 0, 100, 50);
    fill(0);            #black
    circle(0, 0, 10);   #corner of rectangle   
    fill(255, 255, 0);  #yellow
    circle(50, 25, 10); # (50, 25) is center of rectangle    
    endShape()
    
    x = x+1

I did the same for this example with an image:
Help in Rotate Image in JAVA2D - #4 by glv

:)

1 Like