kopia lustrzana https://github.com/villares/sketch-a-day
main
rodzic
62dc6c246d
commit
57238976af
|
|
@ -0,0 +1,103 @@
|
|||
|
||||
|
||||
class Particle:
|
||||
|
||||
F = 10
|
||||
|
||||
def __init__(self, position, PS):
|
||||
self.acceleration = PVector(0, 0)
|
||||
self.velocity = PVector(random(-1, 1) / 5, random(-1, 1) / 5)
|
||||
self.position = position.copy()
|
||||
self.timeToLive = 255.0
|
||||
self.mass = random(5, 50)
|
||||
self.PS = PS
|
||||
|
||||
def run(self):
|
||||
self.update()
|
||||
self.display()
|
||||
|
||||
def applyForce(self, force):
|
||||
# print self.acceleration
|
||||
self.acceleration += force / self.mass
|
||||
|
||||
def update(self):
|
||||
for p in self.PS.particles:
|
||||
if p is self:
|
||||
break
|
||||
else:
|
||||
dir = self.position - p.position
|
||||
# Distance between objects
|
||||
d = dir.mag()
|
||||
# Normalize direction vector
|
||||
dir.normalize()
|
||||
# Keep distance within a reasonable range
|
||||
d = constrain(d, 1, 100)
|
||||
# Repelling force is inversely proportional to distance
|
||||
force = 1 * self.F / (d * d)
|
||||
# print force
|
||||
force = constrain(force, 0, 50)
|
||||
if force > 10:
|
||||
print force
|
||||
# Get force vector -= 1: magnitude * direction
|
||||
dir *= -force
|
||||
|
||||
self.applyForce(dir)
|
||||
p.applyForce(dir * -1)
|
||||
|
||||
|
||||
self.velocity += self.acceleration
|
||||
self.position += self.velocity
|
||||
self.acceleration.setMag(0)
|
||||
# # self.timeToLive -= 1
|
||||
self.velocity *= .95
|
||||
|
||||
if self.position.x > width + 50: self.position.x = -50
|
||||
if self.position.x < -50: self.position.x = width + 50
|
||||
if self.position.y > height + 50: self.position.y = -50
|
||||
if self.position.y < -50: self.position.y = height + 50
|
||||
|
||||
def display(self):
|
||||
# stroke(0, 0, 0, self.timeToLive)
|
||||
# strokeWeight(2)
|
||||
noStroke()
|
||||
fill(self.mass * 5, 200, 200, 255)#self.timeToLive)
|
||||
ellipse(self.position.x, self.position.y, self.mass, self.mass)
|
||||
|
||||
def isDead(self):
|
||||
if self.timeToLive < 0.0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
class ParticleSystem():
|
||||
|
||||
def __init__(self, position):
|
||||
self.origin = position
|
||||
self.particles = []
|
||||
|
||||
def addParticle(self, pos=None):
|
||||
pos = pos or self.origin
|
||||
self.particles.append(Particle(pos, self))
|
||||
|
||||
def applyForce(self, force):
|
||||
for p in self.particles:
|
||||
p.applyForce(force)
|
||||
|
||||
def applyGravity(self):
|
||||
for p in self.particles:
|
||||
p.applyForce(gravity * p.mass)
|
||||
|
||||
def applyRepeller(self, r):
|
||||
for p in self.particles:
|
||||
force = r.calculateRepelForce(p)
|
||||
p.applyForce(force)
|
||||
|
||||
def run(self, force=None):
|
||||
for p in reversed(self.particles):
|
||||
p.run()
|
||||
|
||||
if force is not None:
|
||||
self.applyForce(force)
|
||||
|
||||
if p.isDead():
|
||||
self.particles.remove(p)
|
||||
Plik binarny nie jest wyświetlany.
|
Po Szerokość: | Wysokość: | Rozmiar: 4.2 MiB |
|
|
@ -0,0 +1,38 @@
|
|||
add_library('video') # import processing.video.*
|
||||
add_library('opencv_processing') # import gab.opencv.*
|
||||
|
||||
|
||||
from particles import ParticleSystem
|
||||
|
||||
def setup():
|
||||
global video, opencv, particles
|
||||
size(400, 400)
|
||||
video = Capture(this, 640/2, 480/2)
|
||||
opencv = OpenCV(this, 640/2, 480/2)
|
||||
video.start()
|
||||
|
||||
particles = ParticleSystem(PVector(width / 2, 50))
|
||||
# repeller = Repeller(width / 2, 280, power=20000)
|
||||
colorMode(HSB)
|
||||
for _ in range(20):
|
||||
particles.addParticle(PVector(random(width), random(height)))
|
||||
|
||||
|
||||
|
||||
def draw():
|
||||
background(0)
|
||||
opencv.loadImage(video)
|
||||
opencv.calculateOpticalFlow()
|
||||
ave_flow = PVector().set(opencv.getAverageFlow())
|
||||
print ave_flow
|
||||
if not ave_flow.x != ave_flow.x:
|
||||
particles.run(ave_flow)
|
||||
|
||||
stroke(255)
|
||||
scale(1.5, 2)
|
||||
opencv.drawOpticalFlow()
|
||||
|
||||
|
||||
|
||||
def captureEvent(c):
|
||||
c.read()
|
||||
|
|
@ -21,6 +21,12 @@
|
|||
|
||||
---
|
||||
|
||||

|
||||
|
||||
[sketch_2020_07_31a](https://github.com/villares/sketch-a-day/tree/master/2020/sketch_2020_07_31a) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
||||
|
||||
---
|
||||
|
||||

|
||||
|
||||
[sketch_2020_07_30d](https://github.com/villares/sketch-a-day/tree/master/2020/sketch_2020_07_30d) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue