Skip to content
Merged
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ debug/

# Work in progress
examples/Generative Gestaltung
examples/Nature of Code
sketches/
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
This is a recreation of the Nature of Code example found at:
https://github.com/nature-of-code/noc-2-processing-port/blob/main/chapter0/Example_0_1_Random_Walk/Example_0_1_Random_Walk.pde

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Example 0-1: Random Walk
"""

from pycreative.app import Sketch

class Example_0_1_Random_Walk(Sketch):
def setup(self):
self.size(640, 360)
self.walker_x = self.width // 2
self.walker_y = self.height // 2
self.background(255) # White background

def draw(self):
self.stroke(0) # Black stroke
self.stroke_weight(1)
self.point(self.walker_x, self.walker_y)
choice = int(self.random(4))
if choice == 0:
self.walker_x += 1
elif choice == 1:
self.walker_x -= 1
elif choice == 2:
self.walker_y += 1
else:
self.walker_y -= 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
This is a recreation of the Nature of Code example found at:
https://github.com/nature-of-code/noc-2-processing-port/blob/main/chapter0/Example_0_2_Random_Distribution/Example_0_2_Random_Distribution.pde

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Example 0-2: Random Distribution
"""

from pycreative.app import Sketch

class Example_0_2_Random_Distribution(Sketch):
def setup(self) -> None:
self.size(640, 360)
self.total = 20
self.random_counts = [0] * self.total

def draw(self) -> None:
self.background(255)
index = int(self.random(self.total))
self.random_counts[index] += 1

# Draw a rectangle to graph results
self.stroke(0)
self.stroke_weight(2)
self.fill(127)
w = self.width / len(self.random_counts)

for x in range(len(self.random_counts)):
rc = self.random_counts[x]
self.rect(x * w, self.height - rc, w - 1, rc)
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
This is a recreation of the Nature of Code example found at:
https://github.com/nature-of-code/noc-2-processing-port/blob/main/chapter0/Example_0_3_Random_Walk_Tends_To_Right/Example_0_3_Random_Walk_Tends_To_Right.pde

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Example 0-3: Random Walk Tends to Right
"""

from pycreative.app import Sketch


class Example_0_3_Random_Walk_Tends_To_Right(Sketch):
def setup(self):
self.size(640, 360)
self.background(255)
self.walker = Walker(self, int(self.width / 2), int(self.height / 2))

def draw(self):
self.walker.update()
self.walker.draw()

class Walker():
def __init__(self, sketch, x=0, y=0):
self.sketch = sketch
self.x = x
self.y = y

def update(self):
r = self.sketch.random()
# A 40% of moving to the right!
if r < 0.4:
self.x += 1
elif r < 0.6:
self.x -= 1
elif r < 0.8:
self.y += 1
else:
self.y -= 1
self.x = self.sketch.constrain(self.x, 0, self.sketch.width - 1)
self.y = self.sketch.constrain(self.y, 0, self.sketch.height - 1)

def draw(self):
self.sketch.stroke(0)
self.sketch.point(self.x, self.y)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
This is a recreation of the Nature of Code example found at:
https://github.com/nature-of-code/noc-2-processing-port/blob/main/chapter0/Example_0_4_Gaussian_Distribution/Example_0_4_Gaussian_Distribution.pde

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Example 0-4: Gaussian Distribution
"""

from pycreative.app import Sketch


class Example_0_4_Gaussian_Distribution(Sketch):
def setup(self):
self.size(640, 360)
self.background(255)

def draw(self):
# A normal distribution with mean 320 and standard deviation 60
x = self.random_gaussian() * 60 + 320
self.no_stroke()
self.fill(1, 10)
self.circle(x, 120, 16)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
This is a recreation of the Nature of Code example found at:
https://github.com/nature-of-code/noc-2-processing-port/blob/main/chapter0/Example_0_5_Accept_Reject_Distribution/Example_0_5_Accept_Reject_Distribution.pde

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Example 0-5: Accept-Reject Distribution
"""

from pycreative.app import Sketch


class Example_0_5_Accept_Reject_Distribution(Sketch):
def setup(self) -> None:
self.size(640, 360)
self.total = 20
self.random_counts = [0] * self.total

def draw(self) -> None:
self.background(255)
index = int(self.accept_reject() * len(self.random_counts))
self.random_counts[index] += 1

# Draw a rectangle to graph results
self.stroke(0)
self.stroke_weight(2)
self.fill(127)
w = self.width / len(self.random_counts)

for x in range(len(self.random_counts)):
rc = self.random_counts[x]
self.rect(x * w, self.height - rc, w - 1, rc)

def accept_reject(self) -> float:
# An algorithm for picking a random number based on monte carlo method
# Here probability is determined by formula y = x
while True:
# Pick a random value.
r1 = self.random(1)
# Assign a probability.
probability = r1
# Pick a second random value.
r2 = self.random(1)

# Does it qualify? If so, we’re done!
if r2 < probability:
return r1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
This is a recreation of the Nature of Code example found at:
https://github.com/nature-of-code/noc-2-processing-port/blob/main/chapter0/Example_0_6_Perlin_Noise_Walker/Example_0_6_Perlin_Noise_Walker.pde

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Example 0-6: Perlin Noise Walker
"""

from pycreative.app import Sketch


class Example_0_6_Perlin_Noise_Walker(Sketch):
def setup(self):
self.size(640, 360)
self.background(255)
self.walker = Walker(self)

def draw(self):
self.walker.update()
self.walker.draw()

class Walker:
def __init__(self, sketch):
self.sketch = sketch
# Keep original Processing offsets but add a tiny fractional offset
# to avoid starting at an exact lattice boundary which can produce
# symmetric/biased derivatives in some Perlin implementations.
self.tx = 0.0
self.ty = 10000.1
self.x = 0
self.y = 0

def update(self):
# x- and y-position mapped from noise
self.x = self.sketch.map(self.sketch.noise(self.tx), 0, 1, 0, self.sketch.width)
self.y = self.sketch.map(self.sketch.noise(self.ty), 0, 1, 0, self.sketch.height)

# Move forward through time.
self.tx += 0.01
self.ty += 0.01

def draw(self):
self.sketch.stroke_weight(2)
self.sketch.fill(127)
self.sketch.stroke(0)
self.sketch.ellipse(self.x, self.y, 48)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
This is a recreation of the Nature of Code example found at:
https://github.com/nature-of-code/noc-2-processing-port/blob/main/chapter1/Example_1_1_Bouncing_Ball_No_Vectors/Example_1_1_Bouncing_Ball_No_Vectors.pde

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Example 1-1: Bouncing Ball, no PVector!
"""

from pycreative.app import Sketch


class Example_1_1_Bouncing_Ball_No_Vectors(Sketch):
def setup(self):
self.size(640, 360)
self.x = 100
self.y = 100
self.xspeed = 2.5
self.yspeed = 2

def draw(self):
self.background(255)

# Move the ball according to its speed.
self.x = self.x + self.xspeed
self.y = self.y + self.yspeed

# Check for bouncing.
if self.x > self.width or self.x < 0:
self.xspeed = self.xspeed * -1
if self.y > self.height or self.y < 0:
self.yspeed = self.yspeed * -1

self.stroke(0)
self.fill(127)
self.stroke_weight(2)
# Draw the ball at the position (x,y).
self.circle(self.x, self.y, 48)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
This is a recreation of the Nature of Code example found at:
https://github.com/nature-of-code/noc-2-processing-port/blob/main/chapter1/Example_1_2_Bouncing_Ball_Vectors/Example_1_2_Bouncing_Ball_Vectors.pde

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Example 1-2: Bouncing Ball, with PVector!
"""

from pycreative.app import Sketch

class Example_1_2_Bouncing_Ball_Vectors(Sketch):
def setup(self):
self.size(640, 360)
self.position = self.pvector(100, 100)
self.velocity = self.pvector(2.5, 2)

def draw(self):
self.background(255)
self.position.add(self.velocity)

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

self.stroke(0)
self.fill(127)
self.stroke_weight(2)
self.circle(self.position.x, self.position.y, 48)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
This is a recreation of the Nature of Code example found at:
https://github.com/nature-of-code/noc-2-processing-port/blob/main/chapter1/Example_1_3_Vector_Subtraction/Example_1_3_Vector_Subtraction.pde

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Example 1-3: Vector subtraction
"""

from pycreative.app import Sketch


class Example_1_3_Vector_Subtraction(Sketch):
def setup(self):
self.size(640, 360)

def draw(self):
self.background(255)

mouse = self.pvector(self.mouse_x, self.mouse_y)
center = self.pvector(self.width / 2, self.height / 2)

self.stroke_weight(4)
self.stroke(200)
self.line(0, 0, mouse.x, mouse.y)
self.line(0, 0, center.x, center.y)

mouse.sub(center)

self.stroke(0)
self.translate(self.width / 2, self.height / 2)
self.line(0, 0, mouse.x, mouse.y)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
This is a recreation of the Nature of Code example found at:
https://github.com/nature-of-code/noc-2-processing-port/blob/main/chapter1/Example_1_4_Vector_Multiplication/Example_1_4_Vector_Multiplication.pde

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com

// Example 1-4: Vector Multiplication
"""

from pycreative.app import Sketch


class Example_1_4_Vector_Multiplication(Sketch):
def setup(self):
self.size(640, 360)

def draw(self):
self.background(255)

mouse = self.pvector(self.mouse_x, self.mouse_y)
center = self.pvector(self.width / 2, self.height / 2)
mouse.sub(center)

self.translate(self.width / 2, self.height / 2)
self.stroke_weight(2)
self.stroke(200)
self.line(0, 0, mouse.x, mouse.y)

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

self.stroke(0)
self.stroke_weight(4)
self.line(0, 0, mouse.x, mouse.y)
Loading