oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Transforming shapes with geometry #440

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

Drawing a Circle

oldoc63 commented 1 year ago

Specifying location using coordinates

oldoc63 commented 1 year ago

Transformation functions

oldoc63 commented 1 year ago

Translating objects with translate()

oldoc63 commented 1 year ago

Rotate objects with rotate()

oldoc63 commented 1 year ago

Drawing a circle of circles

oldoc63 commented 1 year ago

Drawing a circle of squares

oldoc63 commented 1 year ago

Animating Objects: Creating the t variable

oldoc63 commented 1 year ago

Rotating the individual squares

oldoc63 commented 1 year ago

Saving orientation with pushmatrix() and popmatrix()

oldoc63 commented 1 year ago

Rotating around the center

oldoc63 commented 1 year ago

Creating an interactive rainbow grid

Drawing a grid of objects

def setup():
    size(600,600)
def draw():
    #set background white
    background(255)
    for x in range(20):
        for y in range(20):
            rect(30*x,30*y,25,25)
oldoc63 commented 1 year ago

Adding the rainbow color to objects

def setup():
    size(600,600)
    rectMode(CENTER)
    colorMode(HSB)
def draw():
    #set background black 
    background(0)
    translate(20,20)
    for x in range(30):
        for y in range(30):
            d = dist(30*x,30*y,mouseX,mouseY)
            fill(0.5*d,255,255)
            rect(30*x,30*y,25,25)
oldoc63 commented 1 year ago

Drawing complex patterns using triangles

def setup():
    size(600,600)
    rectMode(CENTER)
t = 0
def draw():
    global t
    translate(width/2,height/2)
    rotate(radians(t))
    tri(200) #draw the equilateral triangle
    t += 0.5
def tri(length):
    '''Draws an equilateral triangle
      around center of triangle'''
    triangle(0,-length,
-length*sqrt(3)/2, length/2,
length*sqrt(3)/2, length/2)
oldoc63 commented 1 year ago

Drawing multiple rotating triangles

def setup():
    size(600,600)
    rectMode(CENTER)

t = 0

def draw():
    global t
    background(255)#white
    translate(width/2,height/2)
    for i in range(90):
        #space the triangles evenly
        #around the circle
        rotate(radians(360/90))
        pushMatrix() #save this orientation
        #go to circumference of circle
        translate(200,0)
        #spin each triangle
        rotate(radians(t))
        #draw the triangle
        tri(100)
        #return to saved orientation
        popMatrix()
    t += 0.5

def tri(length):
    noFill() #makes the triangle transparent
    triangle(0,-length,
-length*sqrt(3)/2, length/2,
length*sqrt(3)/2, length/2)
oldoc63 commented 1 year ago

Phase-shifting the rotation

def setup():
    size(600,600)
    rectMode(CENTER)
    colorMode(HSB) #for the rainbow colors
    strokeWeight(2) #a little thicker line

t = 0

def draw():
    global t
    background(255)#white
    translate(width/2,height/2)
    for i in range(90):
        #space the triangles evenly
        #around the circle
        rotate(radians(360/90))
        pushMatrix() #save this orientation
        #go to circumference of circle
        translate(200,0)
        #add color to each triangle
        stroke(2*i,255,255)
        #spin each triangle
        rotate(radians(t+2*i*360/90))
        #draw the triangle
        tri(100)
        #return to saved orientation
        popMatrix()
    t += 0.5
def tri(length):
    noFill() #makes the triangle transparent
    triangle(0,-length,
             -length*sqrt(3)/2, length/2,
             length*sqrt(3)/2, length/2)