From dea6d62b3e8c2586430b6d062e3c55d6addcef3c Mon Sep 17 00:00:00 2001 From: Jonathan Feinberg Date: Fri, 25 Apr 2014 22:07:51 -0400 Subject: [PATCH] Chapter 1 examples. --- .../NOC_1_10_motion101_acceleration.pyde | 21 +++++++++++ .../NOC_1_10_motion101_acceleration/mover.py | 36 ++++++++++++++++++ ...NOC_1_11_motion101_acceleration_array.pyde | 26 +++++++++++++ .../mover.py | 37 +++++++++++++++++++ .../NOC_1_1_bouncingball_novectors.pyde | 32 ++++++++++++++++ .../NOC_1_2_bouncingball_vectors.pyde | 31 ++++++++++++++++ .../NOC_1_3_vector_subtraction.pyde | 22 +++++++++++ .../NOC_1_4_vector_multiplication.pyde | 27 ++++++++++++++ .../NOC_1_5_vector_magnitude.pyde | 26 +++++++++++++ .../NOC_1_6_vector_normalize.pyde | 34 +++++++++++++++++ .../NOC_1_7_motion101/NOC_1_7_motion101.pyde | 20 ++++++++++ chp1_vectors/NOC_1_7_motion101/mover.py | 31 ++++++++++++++++ .../NOC_1_8_motion101_acceleration.pyde | 20 ++++++++++ .../NOC_1_8_motion101_acceleration/mover.py | 35 ++++++++++++++++++ .../NOC_1_9_motion101_acceleration.pyde | 18 +++++++++ .../NOC_1_9_motion101_acceleration/mover.py | 36 ++++++++++++++++++ 16 files changed, 452 insertions(+) create mode 100644 chp1_vectors/NOC_1_10_motion101_acceleration/NOC_1_10_motion101_acceleration.pyde create mode 100644 chp1_vectors/NOC_1_10_motion101_acceleration/mover.py create mode 100644 chp1_vectors/NOC_1_11_motion101_acceleration_array/NOC_1_11_motion101_acceleration_array.pyde create mode 100644 chp1_vectors/NOC_1_11_motion101_acceleration_array/mover.py create mode 100644 chp1_vectors/NOC_1_1_bouncingball_novectors/NOC_1_1_bouncingball_novectors.pyde create mode 100644 chp1_vectors/NOC_1_2_bouncingball_vectors/NOC_1_2_bouncingball_vectors.pyde create mode 100644 chp1_vectors/NOC_1_3_vector_subtraction/NOC_1_3_vector_subtraction.pyde create mode 100644 chp1_vectors/NOC_1_4_vector_multiplication/NOC_1_4_vector_multiplication.pyde create mode 100644 chp1_vectors/NOC_1_5_vector_magnitude/NOC_1_5_vector_magnitude.pyde create mode 100644 chp1_vectors/NOC_1_6_vector_normalize/NOC_1_6_vector_normalize.pyde create mode 100644 chp1_vectors/NOC_1_7_motion101/NOC_1_7_motion101.pyde create mode 100644 chp1_vectors/NOC_1_7_motion101/mover.py create mode 100644 chp1_vectors/NOC_1_8_motion101_acceleration/NOC_1_8_motion101_acceleration.pyde create mode 100644 chp1_vectors/NOC_1_8_motion101_acceleration/mover.py create mode 100644 chp1_vectors/NOC_1_9_motion101_acceleration/NOC_1_9_motion101_acceleration.pyde create mode 100644 chp1_vectors/NOC_1_9_motion101_acceleration/mover.py diff --git a/chp1_vectors/NOC_1_10_motion101_acceleration/NOC_1_10_motion101_acceleration.pyde b/chp1_vectors/NOC_1_10_motion101_acceleration/NOC_1_10_motion101_acceleration.pyde new file mode 100644 index 0000000..db6f8df --- /dev/null +++ b/chp1_vectors/NOC_1_10_motion101_acceleration/NOC_1_10_motion101_acceleration.pyde @@ -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() + diff --git a/chp1_vectors/NOC_1_10_motion101_acceleration/mover.py b/chp1_vectors/NOC_1_10_motion101_acceleration/mover.py new file mode 100644 index 0000000..c52ef7b --- /dev/null +++ b/chp1_vectors/NOC_1_10_motion101_acceleration/mover.py @@ -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) + diff --git a/chp1_vectors/NOC_1_11_motion101_acceleration_array/NOC_1_11_motion101_acceleration_array.pyde b/chp1_vectors/NOC_1_11_motion101_acceleration_array/NOC_1_11_motion101_acceleration_array.pyde new file mode 100644 index 0000000..a0026bb --- /dev/null +++ b/chp1_vectors/NOC_1_11_motion101_acceleration_array/NOC_1_11_motion101_acceleration_array.pyde @@ -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() + diff --git a/chp1_vectors/NOC_1_11_motion101_acceleration_array/mover.py b/chp1_vectors/NOC_1_11_motion101_acceleration_array/mover.py new file mode 100644 index 0000000..391770d --- /dev/null +++ b/chp1_vectors/NOC_1_11_motion101_acceleration_array/mover.py @@ -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) + diff --git a/chp1_vectors/NOC_1_1_bouncingball_novectors/NOC_1_1_bouncingball_novectors.pyde b/chp1_vectors/NOC_1_1_bouncingball_novectors/NOC_1_1_bouncingball_novectors.pyde new file mode 100644 index 0000000..87c9f6d --- /dev/null +++ b/chp1_vectors/NOC_1_1_bouncingball_novectors/NOC_1_1_bouncingball_novectors.pyde @@ -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) + diff --git a/chp1_vectors/NOC_1_2_bouncingball_vectors/NOC_1_2_bouncingball_vectors.pyde b/chp1_vectors/NOC_1_2_bouncingball_vectors/NOC_1_2_bouncingball_vectors.pyde new file mode 100644 index 0000000..e782010 --- /dev/null +++ b/chp1_vectors/NOC_1_2_bouncingball_vectors/NOC_1_2_bouncingball_vectors.pyde @@ -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) + diff --git a/chp1_vectors/NOC_1_3_vector_subtraction/NOC_1_3_vector_subtraction.pyde b/chp1_vectors/NOC_1_3_vector_subtraction/NOC_1_3_vector_subtraction.pyde new file mode 100644 index 0000000..14e79b2 --- /dev/null +++ b/chp1_vectors/NOC_1_3_vector_subtraction/NOC_1_3_vector_subtraction.pyde @@ -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) + diff --git a/chp1_vectors/NOC_1_4_vector_multiplication/NOC_1_4_vector_multiplication.pyde b/chp1_vectors/NOC_1_4_vector_multiplication/NOC_1_4_vector_multiplication.pyde new file mode 100644 index 0000000..23fd615 --- /dev/null +++ b/chp1_vectors/NOC_1_4_vector_multiplication/NOC_1_4_vector_multiplication.pyde @@ -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) + diff --git a/chp1_vectors/NOC_1_5_vector_magnitude/NOC_1_5_vector_magnitude.pyde b/chp1_vectors/NOC_1_5_vector_magnitude/NOC_1_5_vector_magnitude.pyde new file mode 100644 index 0000000..8bd658d --- /dev/null +++ b/chp1_vectors/NOC_1_5_vector_magnitude/NOC_1_5_vector_magnitude.pyde @@ -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) + diff --git a/chp1_vectors/NOC_1_6_vector_normalize/NOC_1_6_vector_normalize.pyde b/chp1_vectors/NOC_1_6_vector_normalize/NOC_1_6_vector_normalize.pyde new file mode 100644 index 0000000..c63ea7c --- /dev/null +++ b/chp1_vectors/NOC_1_6_vector_normalize/NOC_1_6_vector_normalize.pyde @@ -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) + diff --git a/chp1_vectors/NOC_1_7_motion101/NOC_1_7_motion101.pyde b/chp1_vectors/NOC_1_7_motion101/NOC_1_7_motion101.pyde new file mode 100644 index 0000000..591a76d --- /dev/null +++ b/chp1_vectors/NOC_1_7_motion101/NOC_1_7_motion101.pyde @@ -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() + diff --git a/chp1_vectors/NOC_1_7_motion101/mover.py b/chp1_vectors/NOC_1_7_motion101/mover.py new file mode 100644 index 0000000..9e716c2 --- /dev/null +++ b/chp1_vectors/NOC_1_7_motion101/mover.py @@ -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 + diff --git a/chp1_vectors/NOC_1_8_motion101_acceleration/NOC_1_8_motion101_acceleration.pyde b/chp1_vectors/NOC_1_8_motion101_acceleration/NOC_1_8_motion101_acceleration.pyde new file mode 100644 index 0000000..591a76d --- /dev/null +++ b/chp1_vectors/NOC_1_8_motion101_acceleration/NOC_1_8_motion101_acceleration.pyde @@ -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() + diff --git a/chp1_vectors/NOC_1_8_motion101_acceleration/mover.py b/chp1_vectors/NOC_1_8_motion101_acceleration/mover.py new file mode 100644 index 0000000..1ca3489 --- /dev/null +++ b/chp1_vectors/NOC_1_8_motion101_acceleration/mover.py @@ -0,0 +1,35 @@ +# The Nature of Code +# Daniel Shiffman +# http://natureofcode.com + + +class Mover(object): + + def __init__(self): + self.location = PVector(width / 2, height / 2) + self.velocity = PVector(0, 0) + self.acceleration = PVector(-0.001, 0.01) + self.topspeed = 10 + + def update(self): + self.velocity.add(self.acceleration) + self.velocity.limit(self.topspeed) + 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 + diff --git a/chp1_vectors/NOC_1_9_motion101_acceleration/NOC_1_9_motion101_acceleration.pyde b/chp1_vectors/NOC_1_9_motion101_acceleration/NOC_1_9_motion101_acceleration.pyde new file mode 100644 index 0000000..a9fe08a --- /dev/null +++ b/chp1_vectors/NOC_1_9_motion101_acceleration/NOC_1_9_motion101_acceleration.pyde @@ -0,0 +1,18 @@ +# 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() + diff --git a/chp1_vectors/NOC_1_9_motion101_acceleration/mover.py b/chp1_vectors/NOC_1_9_motion101_acceleration/mover.py new file mode 100644 index 0000000..c5d295f --- /dev/null +++ b/chp1_vectors/NOC_1_9_motion101_acceleration/mover.py @@ -0,0 +1,36 @@ +# The Nature of Code +# Daniel Shiffman +# http://natureofcode.com + + +class Mover(object): + + def __init__(self): + self.location = PVector(width / 2, height / 2) + self.velocity = PVector(0, 0) + self.topspeed = 6 + + def update(self): + self.acceleration = PVector.random2D() + self.acceleration.mult(random(2)) + self.velocity.add(self.acceleration) + self.velocity.limit(self.topspeed) + 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 +