Commit a23d9312 authored by Romain Casati's avatar Romain Casati
Browse files

Adding p5 examples.

parent 8a9077f1
{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"from p5 import *","execution_count":1,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"numBalls = 13\nspring = 0.05\ngravity = 0.03\nfriction = -0.9\nballs = []\n\ndef setup():\n createCanvas(720, 400)\n update_variables() # force width/height update\n for i in range(numBalls):\n ball = Ball(random(width), random(height),\n random(10, 100), i, balls)\n balls.append(ball)\n noStroke()\n fill(255, 204)\n\ndef draw():\n background(0)\n for ball in balls:\n ball.collide()\n ball.move()\n ball.display()\n\nclass Ball(object):\n def __init__(self, xin, yin, din, idin, oin):\n self.x = xin\n self.y = yin\n self.vx = 0\n self.vy = 0\n self.diameter = din\n self.id = idin\n self.others = oin\n\n def collide(self):\n for i in range(self.id + 1, numBalls):\n dx = self.others[i].x - self.x\n dy = self.others[i].y - self.y\n distance = sqrt(dx * dx + dy * dy)\n minDist = self.others[i].diameter / 2 + self.diameter / 2;\n if distance < minDist:\n angle = atan2(dy, dx)\n targetX = self.x + cos(angle) * minDist\n targetY = self.y + sin(angle) * minDist\n ax = (targetX - self.others[i].x) * spring\n ay = (targetY - self.others[i].y) * spring\n self.vx -= ax;\n self.vy -= ay;\n self.others[i].vx += ax\n self.others[i].vy += ay\n\n def move(self):\n self.vy += gravity\n self.x += self.vx\n self.y += self.vy\n if self.x + self.diameter / 2 > width:\n self.x = width - self.diameter / 2\n self.vx *= friction\n elif self.x - self.diameter / 2 < 0:\n self.x = self.diameter / 2\n self.vx *= friction\n if self.y + self.diameter / 2 > height:\n self.y = height - self.diameter / 2\n self.vy *= friction\n elif self.y - self.diameter / 2 < 0:\n self.y = self.diameter / 2\n self.vy *= friction\n\n def display(self):\n ellipse(self.x, self.y, self.diameter, self.diameter)","execution_count":2,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"run()","execution_count":3,"outputs":[{"output_type":"display_data","data":{"application/javascript":"element.append(window.domNodeBus.pop(0));"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"celltoolbar":"Slideshow"},"nbformat":4,"nbformat_minor":2}
``` python
from p5 import *
```
%% Cell type:code id: tags:
``` python
numBalls = 13
spring = 0.05
gravity = 0.03
friction = -0.9
balls = []
def setup():
createCanvas(720, 400)
update_variables() # force width/height update
for i in range(numBalls):
ball = Ball(random(width), random(height),
random(10, 100), i, balls)
balls.append(ball)
noStroke()
fill(255, 204)
def draw():
background(0)
for ball in balls:
ball.collide()
ball.move()
ball.display()
class Ball(object):
def __init__(self, xin, yin, din, idin, oin):
self.x = xin
self.y = yin
self.vx = 0
self.vy = 0
self.diameter = din
self.id = idin
self.others = oin
def collide(self):
for i in range(self.id + 1, numBalls):
dx = self.others[i].x - self.x
dy = self.others[i].y - self.y
distance = sqrt(dx * dx + dy * dy)
minDist = self.others[i].diameter / 2 + self.diameter / 2;
if distance < minDist:
angle = atan2(dy, dx)
targetX = self.x + cos(angle) * minDist
targetY = self.y + sin(angle) * minDist
ax = (targetX - self.others[i].x) * spring
ay = (targetY - self.others[i].y) * spring
self.vx -= ax;
self.vy -= ay;
self.others[i].vx += ax
self.others[i].vy += ay
def move(self):
self.vy += gravity
self.x += self.vx
self.y += self.vy
if self.x + self.diameter / 2 > width:
self.x = width - self.diameter / 2
self.vx *= friction
elif self.x - self.diameter / 2 < 0:
self.x = self.diameter / 2
self.vx *= friction
if self.y + self.diameter / 2 > height:
self.y = height - self.diameter / 2
self.vy *= friction
elif self.y - self.diameter / 2 < 0:
self.y = self.diameter / 2
self.vy *= friction
def display(self):
ellipse(self.x, self.y, self.diameter, self.diameter)
```
%% Cell type:code id: tags:
``` python
run()
```
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
```
{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"from p5.instance import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def setup(sketch):\n sketch.createCanvas(200, 200)\n sketch.background(160)\n\ndef draw(sketch):\n sketch.fill(\"blue\")\n sketch.background(200)\n radius = sketch.sin(sketch.frameCount / 60) * 50 + 50\n sketch.ellipse(100, 100, radius, radius)\n\nrun(setup, draw)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw(sketch):\n sketch.fill(\"red\")\n sketch.background(200)\n radius = sketch.sin(sketch.frameCount / 60) * 50 + 50\n sketch.ellipse(100, 100, radius, radius)\n\nrun(setup, draw)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"celltoolbar":"Slideshow"},"nbformat":4,"nbformat_minor":2}
``` python
from p5.instance import *
```
%% Cell type:code id: tags:
``` python
def setup(sketch):
sketch.createCanvas(200, 200)
sketch.background(160)
def draw(sketch):
sketch.fill("blue")
sketch.background(200)
radius = sketch.sin(sketch.frameCount / 60) * 50 + 50
sketch.ellipse(100, 100, radius, radius)
run(setup, draw)
```
%% Cell type:code id: tags:
``` python
def draw(sketch):
sketch.fill("red")
sketch.background(200)
radius = sketch.sin(sketch.frameCount / 60) * 50 + 50
sketch.ellipse(100, 100, radius, radius)
run(setup, draw)
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
from p5 import *
```
%% Cell type:code id: tags:
``` python
def setup():
createCanvas(200, 200)
background(160)
def draw():
fill("blue")
background(200)
radius = sin(frameCount / 60) * 50 + 50
ellipse(100, 100, radius, radius)
run()
```
%% Cell type:code id: tags:
``` python
def setup():
createCanvas(600,600)
noStroke()
rectMode(CENTER)
def draw():
colorMode(HSB, 100)
h = map(mouseY, 0, 600, 0, 100)
background(h, 100, 100)
fill(100-h, 100, 100)
rect(300, 300, mouseX + 1, mouseX + 1)
run()
```
%% Cell type:code id: tags:
``` python
t = 0
def setup():
createCanvas(600, 600)
stroke(200, 40, 40)
strokeWeight(3)
fill(200, 40, 40)
def draw():
global t
background(245, 10)
xAngle = map(mouseX, 0, width, -4 * PI, 4 * PI, True)
yAngle = map(mouseY, 0, height, -4 * PI, 4 * PI, True)
for x in range(0, width, 60):
for y in range(0, height, 60):
angle = xAngle * (x / width) + yAngle * (y / height)
myX = x + 20 * cos(2 * PI * t + angle)
myY = y + 20 * sin(2 * TWO_PI * t + angle)
ellipse(myX, myY, 10)
t = t + 0.01
run()
```
%% Cell type:code id: tags:
``` python
stop()
```
%% Cell type:code id: tags:
``` python
def setup():
createCanvas(900, 900)
stroke(27, 27, 27, 10)
strokeWeight(2)
def draw():
push()
translate(width / 2, height / 2)
v = createVector(2*random()-1, 2*random()-1)
v.normalize()
v.mult(random(100, 400))
line(0, 0, v.x, v.y)
pop()
run()
```
%% Cell type:code id: tags:
``` python
theta = None
def branch(h):
# Each branch will be 2/3rds the size of the previous one
h *= 0.66;
# All recursive functions must have an exit condition!!!!
# Here, ours is when the length of the branch is 2 pixels or less
if h > 2:
push() # Save the current state of transformation (i.e. where are we now)
rotate(theta) # Rotate by theta
line(0, 0, 0, -h) # Draw the branch
translate(0, -h) # Move to the end of the branch
branch(h) # Ok, now call myself to draw two new branches!!
pop() # Whenever we get back here, we "pop" in order to restore the previous matrix state
# Repeat the same thing, only branch off to the "left" this time!
push()
rotate(-theta)
line(0, 0, 0, -h)
translate(0, -h)
branch(h)
pop()
def setup():
createCanvas(710, 400)
def draw():
global theta
background(255)
frameRate(30)
stroke(0)
# Let's pick an angle 0 to 90 degrees based on the mouse position
a = (mouseX / width) * 90
# Convert it to radians
theta = radians(a)
# Start the tree from the bottom of the screen
translate(width / 2,height)
# Draw a line 120 pixels
line(0, 0, 0, -120)
# Move to the end of that line
translate(0, -120)
# Start the recursive branching!
branch(120)
run()
```
%% Cell type:code id: tags:
``` python
stop()
```
%% Cell type:code id: tags:
``` python
def setup():
createCanvas(710, 400, WEBGL)
def draw():
background(255)
normalMaterial()
push()
rotateZ(frameCount * 0.01)
rotateX(frameCount * 0.01)
rotateY(frameCount * 0.01)
torus(120, 40)
pop()
run()
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment