Skip to content

Commit

Permalink
Chapter 1 examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Feinberg committed Apr 26, 2014
1 parent d111d0e commit dea6d62
Show file tree
Hide file tree
Showing 16 changed files with 452 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com

from mover import Mover


def setup():
size(640, 360)
global mover
mover = Mover()


def draw():
background(255)

# Update the location
mover.update()
# Display the Mover
mover.display()

36 changes: 36 additions & 0 deletions chp1_vectors/NOC_1_10_motion101_acceleration/mover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# A Mover object


class Mover(object):

def __init__(self):
# The Mover tracks location, velocity, and acceleration
# Start in the center
self.location = PVector(width / 2, height / 2)
self.velocity = PVector(0, 0)
# The Mover's maximum speed
self.topspeed = 5

def update(self):
# Compute a vector that points from location to mouse
mouse = PVector(mouseX, mouseY)
acceleration = PVector.sub(mouse, self.location)
# Set magnitude of acceleration
acceleration.setMag(0.2)

# Velocity changes according to acceleration
self.velocity.add(acceleration)
# Limit the velocity by topspeed
self.velocity.limit(self.topspeed)
# Location changes by velocity
self.location.add(self.velocity)

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# Demonstration of the basics of motion with vector.
# A "Mover" object stores location, velocity, and acceleration as vectors
# The motion is controlled by affecting the acceleration (in this case
# towards the mouse)

from mover import Mover

moverCount = 20
movers = []


def setup():
size(640, 360)
for i in range(moverCount):
movers.append(Mover())


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

37 changes: 37 additions & 0 deletions chp1_vectors/NOC_1_11_motion101_acceleration_array/mover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com


class Mover(object):

def __init__(self):
# The Mover tracks location, velocity, and acceleration
# Start in the center
self.location = PVector(random(width), random(height))
self.velocity = PVector(0, 0)
# The Mover's maximum speed
self.topspeed = 5

def update(self):
# Compute a vector that points from location to mouse
mouse = PVector(mouseX, mouseY)
acceleration = PVector.sub(mouse, self.location)
# Set magnitude of acceleration
# acceleration.setMag(0.2)
acceleration.normalize()
acceleration.mult(0.2)

# Velocity changes according to acceleration
self.velocity.add(acceleration)
# Limit the velocity by topspeed
self.velocity.limit(self.topspeed)
# Location changes by velocity
self.location.add(self.velocity)

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# Example 1-1: Bouncing Ball, no vectors

x = 100
y = 100
xspeed = 2.5
yspeed = 2


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


def draw():
background(255)
# Add the current speed to the location.
global x, y, xspeed, yspeed
x = x + xspeed
y = y + yspeed
if (x > width) or (x < 0):
xspeed = xspeed * -1
if (y > height) or (y < 0):
yspeed = yspeed * -1
# Display circle at x location
stroke(0)
strokeWeight(2)
fill(127)
ellipse(x, y, 48, 48)

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# Example 1-2: Bouncing Ball, with PVector!


def setup():
size(200, 200)
background(255)
global location, velocity
location = PVector(100, 100)
velocity = PVector(2.5, 5)


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

# Add the current speed to the location.
global location, velocity
location.add(velocity)
if (location.x > width) or (location.x < 0):
velocity.x = velocity.x * -1
if (location.y > height) or (location.y < 0):
velocity.y = velocity.y * -1
# Display circle at x location
stroke(0)
fill(175)
ellipse(location.x, location.y, 16, 16)

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# Example 1-3: Vector subtraction


def setup():
size(640, 360)


def draw():
background(255)

mouse = PVector(mouseX, mouseY)
center = PVector(width / 2, height / 2)
mouse.sub(center)

translate(width / 2, height / 2)
strokeWeight(2)
stroke(0)
line(0, 0, mouse.x, mouse.y)

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# Example 1-4: Vector multiplication


def setup():
size(640, 360)
smooth()


def draw():
background(255)

mouse = PVector(mouseX, mouseY)
center = PVector(width / 2, height / 2)
mouse.sub(center)

# Multiplying a vector! The vector is now half its original size
# (multiplied by 0.5).
mouse.mult(0.5)

translate(width / 2, height / 2)
strokeWeight(2)
stroke(0)
line(0, 0, mouse.x, mouse.y)

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# Example 1-5: Vector magnitude


def setup():
size(640, 360)


def draw():
background(255)

mouse = PVector(mouseX, mouseY)
center = PVector(width / 2, height / 2)
mouse.sub(center)
m = mouse.mag()
fill(0)
noStroke()
rect(0, 0, m, 10)

translate(width / 2, height / 2)
stroke(0)
strokeWeight(2)
line(0, 0, mouse.x, mouse.y)

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com
# Demonstration of normalizing a vector.
# Normalizing a vector sets its length to 1.


def setup():
size(640, 360)


def draw():
background(255)

# A vector that points to the mouse location
mouse = PVector(mouseX, mouseY)
# A vector that points to the center of the window
center = PVector(width / 2, height / 2)
# Subtract center from mouse which results in a vector that points from
# center to mouse
mouse.sub(center)

# Normalize the vector
mouse.normalize()

# Multiply its length by 50
mouse.mult(150)

translate(width / 2, height / 2)
# Draw the resulting vector
stroke(0)
strokeWeight(2)
line(0, 0, mouse.x, mouse.y)

20 changes: 20 additions & 0 deletions chp1_vectors/NOC_1_7_motion101/NOC_1_7_motion101.pyde
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com

from mover import Mover


def setup():
size(640, 360)
global mover
mover = Mover()


def draw():
background(255)

mover.update()
mover.checkEdges()
mover.display()

31 changes: 31 additions & 0 deletions chp1_vectors/NOC_1_7_motion101/mover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com


class Mover(object):

def __init__(self):
self.location = PVector(random(width), random(height))
self.velocity = PVector(random(-2, 2), random(-2, 2))

def update(self):
self.location.add(self.velocity)

def display(self):
stroke(0)
strokeWeight(2)
fill(127)
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

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# The Nature of Code
# Daniel Shiffman
# http://natureofcode.com

from mover import Mover


def setup():
size(640, 360)
global mover
mover = Mover()


def draw():
background(255)

mover.update()
mover.checkEdges()
mover.display()

Loading

0 comments on commit dea6d62

Please sign in to comment.