Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Python/ch1_vectors/NOC_1_10_motion101_acceleration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# ported to pyprocessing by Greg Meess

import pyprocessing as p
from random import randint,uniform

def setup():
p.size(400,400)
p.smooth()
global mover
mover = Mover()

def draw():
p.background(255)
mover.update()
mover.checkEdges()
mover.display()

class Mover:

topspeed = 5

def __init__(self):
self.location = p.PVector(width/2, height/2)
self.velocity = p.PVector(0, 0)

def update(self):
mouse = p.PVector(p.mouse.x,p.mouse.y)
acceleration = p.PVector.sub(mouse,self.location)
acceleration.normalize()
acceleration.mult(0.2)

self.velocity.add(acceleration)
# pyprocessing PVector has no 'limit' method
if self.velocity.x > self.topspeed: self.velocity.x = self.topspeed
if self.velocity.y > self.topspeed: self.velocity.y = self.topspeed
self.location.add(self.velocity)

def display(self):
p.stroke(0)
p.strokeWeight(2)
p.fill(127)
p.ellipse(self.location.x, self.location.y, 48, 48)

def checkEdges(self):

if self.location.x > width:
self.location.x = 0
elif self.location.x < 0:
self.location.x = width

if self.location.y > height:
self.location.y = 0
elif self.location.y < 0:
self.location.y = height

p.run()
60 changes: 60 additions & 0 deletions Python/ch1_vectors/NOC_1_11_motion101_acceleration_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# ported to pyprocessing by Greg Meess

import pyprocessing as p
from random import uniform

def setup():
p.size(600,600)
p.smooth()
global movers
movers = [Mover() for i in range(0,20)]

def draw():
p.background(255)
for mover in movers:
mover.update()
mover.checkEdges()
mover.display()

class Mover:

topspeed = 5

def __init__(self):
self.location = p.PVector(uniform(0,width), uniform(0,height))
self.velocity = p.PVector(0, 0)

def update(self):
mouse = p.PVector(p.mouse.x,p.mouse.y)
acceleration = p.PVector.sub(mouse,self.location)
acceleration.normalize()
acceleration.mult(0.2)

self.velocity.add(acceleration)
# pyprocessing PVector has no 'limit' method
if self.velocity.x > self.topspeed: self.velocity.x = self.topspeed
if self.velocity.y > self.topspeed: self.velocity.y = self.topspeed
self.location.add(self.velocity)

def display(self):
p.stroke(0)
p.strokeWeight(2)
p.fill(127)
p.ellipse(self.location.x, self.location.y, 48, 48)

def checkEdges(self):

if self.location.x > width:
self.location.x = 0
elif self.location.x < 0:
self.location.x = width

if self.location.y > height:
self.location.y = 0
elif self.location.y < 0:
self.location.y = height

p.run()
35 changes: 35 additions & 0 deletions Python/ch1_vectors/NOC_1_1_bouncingball_novectors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# ported to pyprocessing by Greg Meess

import pyprocessing as p

x = 100
y = 100
xspeed = 1.
yspeed = 3.3

def setup():
p.size(800,200)
p.smooth()
p.frameRate(60)

def draw():
global x,y,xspeed,yspeed
p.background(255)

x += xspeed
y += yspeed

if x > width or x < 0:
xspeed *= -1
if y > height or y < 0:
yspeed *= -1

p.stroke(0)
p.strokeWeight(2)
p.fill(127)
p.ellipse(x, y, 48, 48)

p.run()
38 changes: 38 additions & 0 deletions Python/ch1_vectors/NOC_1_2_bouncingball_vectors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# ported to pyprocessing by Greg Meess

# Example 1-2: Bouncing Ball, with PVector!

import pyprocessing as p

def setup():
p.size(800,200)
p.smooth()
p.background(255)

global location, velocity
location = p.PVector(100,100)
velocity = p.PVector(2.5,5)


def draw():
p.noStroke()
p.fill(255,10)
p.rect(0,0,width,height)

# add current speed to location
location.add(velocity)

if location.x > width or location.x < 0:
velocity.x *= -1
if location.y > height or location.y < 0:
velocity.y *= -1

# display circle at location
p.stroke(0)
p.fill(175)
p.ellipse(location.x, location.y, 16, 16)

p.run()
29 changes: 29 additions & 0 deletions Python/ch1_vectors/NOC_1_3_vector_multiplication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# ported to pyprocessing by Greg Meess

# Example 1-4: Vector multiplication
# Note: although using p.PVector is possible, here numpy arrays are substituted

import pyprocessing as p
from numpy import array

def setup():
p.size(800,200)
p.smooth()

def draw():
p.background(255)

mouse = array([p.mouse.x, p.mouse.y])
center = array([width/2, height/2])
mouse -= center
mouse *= 0.5

p.translate(width/2, height/2)
p.strokeWeight(2)
p.stroke(0)
p.line(0, 0, mouse[0], mouse[1])

p.run()
28 changes: 28 additions & 0 deletions Python/ch1_vectors/NOC_1_3_vector_subtraction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# ported to pyprocessing by Greg Meess

# Example 1-3: Vector subtraction
# Note: although using p.PVector is possible, here numpy arrays are substituted

import pyprocessing as p
import numpy as np

def setup():
p.size(400,400)
p.smooth()

def draw():
p.background(255)

mouse = np.array([p.mouse.x, p.mouse.y])
center = np.array([width/2, height/2])
mouse = mouse - center

p.translate(width/2, height/2)
p.strokeWeight(2)
p.stroke(0)
p.line(0, 0, mouse[0], mouse[1])

p.run()
29 changes: 29 additions & 0 deletions Python/ch1_vectors/NOC_1_4_vector_multiplication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# ported to pyprocessing by Greg Meess

# Example 1-4: Vector multiplication
# Note: although using p.PVector is possible, here numpy arrays are substituted

import pyprocessing as p
import numpy as np

def setup():
p.size(400,400)
p.smooth()

def draw():
p.background(255)

mouse = np.array([p.mouse.x, p.mouse.y])
center = np.array([width/2, height/2])
mouse -= center
mouse *= 0.5

p.translate(width/2, height/2)
p.strokeWeight(2)
p.stroke(0)
p.line(0, 0, mouse[0], mouse[1])

p.run()
33 changes: 33 additions & 0 deletions Python/ch1_vectors/NOC_1_5_vector_magnitude.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# ported to pyprocessing by Greg Meess

# Example 1-5: Vector magnitude
# Note: although using p.PVector is possible, here numpy arrays are substituted

import pyprocessing as p
import numpy as np

def setup():
p.size(400,400)
p.smooth()

def draw():
p.background(255)

mouse = np.array([p.mouse.x, p.mouse.y])
center = np.array([width/2, height/2])
mouse -= center

m = np.linalg.norm(mouse)
p.fill(0)
p.noStroke()
p.rect(0,0,m,10)

p.translate(width/2, height/2)
p.strokeWeight(2)
p.stroke(0)
p.line(0, 0, mouse[0], mouse[1])

p.run()
37 changes: 37 additions & 0 deletions Python/ch1_vectors/NOC_1_6_vector_normalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# ported to pyprocessing by Greg Meess

# Example 1-6: Demonstration of normalizing a vector (sets its length to 1)
# Note: although using p.PVector is possible, here numpy arrays are substituted

import pyprocessing as p
import numpy as np

def setup():
p.size(400,400)
p.smooth()

def draw():
p.background(255)

# vector that points to the mouse location
mouse = np.array([p.mouse.x, p.mouse.y], dtype=float)
# vector that points to the center of the window
center = np.array([width/2, height/2])
# subtract center from mouse, gives vector pointing from center to mouse
mouse -= center

# normalize the vector
mouse /= np.linalg.norm(mouse)
# multiply length
mouse *= 150

# draw the resulting vector
p.translate(width/2, height/2)
p.strokeWeight(2)
p.stroke(0)
p.line(0, 0, mouse[0], mouse[1])

p.run()
Loading