oldoc63 / learningDS

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

Creating oscillations with trigonometry #441

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

https://www.evernote.com/shard/s468/sh/7c528547-ecba-aa51-79cb-6e19f9dd8856/1ec8e82726bd3575aefef87433b9ed01

oldoc63 commented 1 year ago
oldoc63 commented 1 year ago
oldoc63 commented 1 year ago

Using trigonometry for oscillations and rotations

oldoc63 commented 1 year ago

Writing functions to draw polygons

def setup():
    size(600,600)
def draw():
    beginShape()
    vertex(100,100)
    vertex(100,200)
    vertex(200,200)
    vertex(200,100)
    vertex(150,50)
endShape(CLOSE)
oldoc63 commented 1 year ago

Drawing an hexagon with loops

def setup():
    size(600,600)
def draw():
    translate(width/2,height/2)
    beginShape()
    for i in range(6):
        vertex(100*cos(radians(60*i)),
100*sin(radians(60*i)))
    endShape(CLOSE)
oldoc63 commented 1 year ago

Drawing an Equilateral Triangle

def setup():
    size(600,600)

def draw():
    translate(width/2,height/2)
    polygon(3,100) #3 sides, vertices 100 units from the center

def polygon(sides,sz):
    '''draws a polygon given the number of sides and length from the center'''
    beginShape()
    for i in range(sides):
        step = radians(360/sides)
        vertex(sz*cos(i * step), sz*sin(i * step))
    endShape(CLOSE)
oldoc63 commented 1 year ago

Making Sine Waves

r1 = 100 #radius of big circle
r2 = 10 #radius of small circle
t = 0 #time variable

def setup():
    size(600,600)

def draw():
    global t
    background(200)
    #move to left-center of screen
    translate(width/4,height/2)
    noFill() #don't color in the circle
    stroke(0) #black outline
    ellipse(0,0,2*r1,2*r1)
    
    #circling ellipse:
    fill(255,0,0) #red
    y = r1*sin(t)
    x = r1*cos(t)
    ellipse(x,y,r2,r2)
    stroke(0,255,0) #green for the line
    line(x,y,200,y)
    fill(0,255,0) #green for the ellipse
    ellipse(200,y,10,10)
    t += 0.05
oldoc63 commented 1 year ago

Leaving a Trail

#CircleSineWave.pyde
r1 = 100 #radius of big circle
r2 = 10  #radius of small circle
t = 0 #time variable
circleList = []

def setup():
    size(600,600)

def draw():
    global t, circleList
    background(200)
    #move to left-center of screen
    translate(width/4,height/2)
    noFill() #don't color in the circle
    stroke(0) #black outline
    ellipse(0,0,2*r1,2*r1)

    #circling ellipse:
    fill(255,0,0) #red
    y = r1*sin(t)
    x = r1*cos(t)
    #add point to list:
    circleList.insert(0,y)
    ellipse(x,y,r2,r2)
    stroke(0,255,0) #green for the line
    line(x,y,200,y)
    fill(0,255,0) #green for the ellipse
    ellipse(200,y,10,10)

    if len(circleList)>300:
        circleList.remove(circleList[-1])

    #loop over circleList to leave a trail:
    for i,c in enumerate(circleList):
        #small circle for trail:
        ellipse(200+i,c,5,5)

    t += 0.05
oldoc63 commented 1 year ago

Creating a spirograph program

r1 = 300.0 #radius of big circle
r2 = 175.0 #radius of circle 2
r3 = 5.0 #radius of drawing "dot"

#location of big circle:
x1 = 0
y1 = 0

t = 0 #time variable

points = [] #empty list to put points in

def setup():
    size(600,600)

def draw():
    global r1,r2,x1,y1,t
    translate(width/2,height/2)
    background(255)
    noFill()
    #big circle
    stroke(0)
    ellipse(x1,y1,2*r1,2*r1)

    #circle 2
    x2 = (r1 - r2)*cos(t)
    y2 = (r1 - r2)*sin(t)
    ellipse(x2,y2,2*r2,2*r2)
    t += 0.05
oldoc63 commented 1 year ago
#from __future__ import division

#spirograph.pyde

r1 = 300.0 #radius of big circle
r2 = 105.0 #radius of circle 2
r3 = 5.0   #radius of drawing “dot”
#location of big circle:
x1 = 0
y1 = 0
t = 0 #time variable
points = [] #empty list to put points in
prop = 0.8

def setup():
    size(600,600)

def draw():
    global r1,r2,x1,y1,t,prop,points
    translate(width/2,height/2)
    background(255)
    noFill()
    #big circle
    stroke(0)
    ellipse(x1,y1,2*r1,2*r1)
    #circle 2
    x2 = (r1 - r2)*cos(t)
    y2 = (r1 - r2)*sin(t)

    ellipse(x2,y2,2*r2,2*r2)

    #drawing dot
    x3 = x2+prop*(r2 - r3)*cos(-((r1-r2)/r2)*t)
    y3 = y2+prop*(r2 - r3)*sin(-((r1-r2)/r2)*t)

    fill(255,0,0)
    ellipse(x3,y3,2*r3,2*r3)
    points.append([x3,y3])
    if len(points) > 2000: #if the list gets too long
        points.pop() #remove the first point
    for i,p in enumerate(points): #go through the points list
        if i < len(points)-1: #up to the next to last point
            stroke(255,0,0)# draw red lines between the points
            line(p[0],p[1],points[i+1][0],points[i+1][1])

    t += 0.05