forked from nature-of-code/noc-examples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jonathan Feinberg
committed
Apr 26, 2014
1 parent
d111d0e
commit dea6d62
Showing
16 changed files
with
452 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
chp1_vectors/NOC_1_10_motion101_acceleration/NOC_1_10_motion101_acceleration.pyde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
26 changes: 26 additions & 0 deletions
26
..._vectors/NOC_1_11_motion101_acceleration_array/NOC_1_11_motion101_acceleration_array.pyde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
chp1_vectors/NOC_1_11_motion101_acceleration_array/mover.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
32 changes: 32 additions & 0 deletions
32
chp1_vectors/NOC_1_1_bouncingball_novectors/NOC_1_1_bouncingball_novectors.pyde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
31 changes: 31 additions & 0 deletions
31
chp1_vectors/NOC_1_2_bouncingball_vectors/NOC_1_2_bouncingball_vectors.pyde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
22 changes: 22 additions & 0 deletions
22
chp1_vectors/NOC_1_3_vector_subtraction/NOC_1_3_vector_subtraction.pyde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
27 changes: 27 additions & 0 deletions
27
chp1_vectors/NOC_1_4_vector_multiplication/NOC_1_4_vector_multiplication.pyde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
26 changes: 26 additions & 0 deletions
26
chp1_vectors/NOC_1_5_vector_magnitude/NOC_1_5_vector_magnitude.pyde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
34 changes: 34 additions & 0 deletions
34
chp1_vectors/NOC_1_6_vector_normalize/NOC_1_6_vector_normalize.pyde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
20 changes: 20 additions & 0 deletions
20
chp1_vectors/NOC_1_8_motion101_acceleration/NOC_1_8_motion101_acceleration.pyde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
Oops, something went wrong.