Skip to content

Instantly share code, notes, and snippets.

@villares
Created October 7, 2021 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save villares/b7ce2f73f6250cc8fe3c13677553bd7a to your computer and use it in GitHub Desktop.
Save villares/b7ce2f73f6250cc8fe3c13677553bd7a to your computer and use it in GitHub Desktop.
from random import choice
atoms = [] # lista de objetos
neutrons = []
def setup():
""" Define área de desenho e popula lista de particles """
size(800, 800) # área de desenho (width, height)
half_width, half_height = width / 2, height / 2
density = 200 # try 900
for _ in range(density):
new_atom = Particle(random(width),random(height))
atoms.append(new_atom)
def draw():
""" Limpa a tela, plot e atualiza particles """
background(0) # atualização do desenho, fundo preto
for atom in reversed(atoms):
atom.plot()
atom.update()
for neutron in neutrons:
neutron.plot()
neutron.update()
def mousePressed(): # click to add neutrons
neutrons.append( Particle(mouseX, mouseY, neutron=True))
class Particle():
""" Classe Particle, cor sorteada, velocidade sorteada """
def __init__(self, px, py, psiz=None, neutron=False):
self.x = px
self.y = py
if psiz:
self.siz = psiz
else:
self.siz = random(5, 10)
self.vx = random(-0.1,0.1) if not neutron else random(10, 15) * choice((-1, 1))
self.vy = random(-0.1,0.1) if not neutron else random(10, 15) * choice((-1, 1))
self.neutron = neutron
if neutron:
self.cor = color(255)
self.siz = 3
else:
self.cor = color(random(256), # R
random(256), # G
random(256), # B
200)
def plot(self):
""" Desenha círculo """
fill(self.cor)
circle(self.x, self.y, self.siz)
def update(self):
""" atualiza a posição do objeto e devolve do lado oposto se sair """
self.x += self.vx
self.y += self.vy
if self.neutron:
for other in reversed(atoms):
if dist(self.x, self.y, other.x, other.y) < 3:
atoms.remove(other)
neutrons.append( Particle(self.x, self.y, neutron=True))
neutrons.append( Particle(self.x, self.y, neutron=True))
fill(255)
circle(self.x, self.y, 100)
return
h = self.siz / 2
if self.x > width + h:
self.x = -h
if self.y > height + h:
self.y = -h
if self.x < -h:
self.x = width + h
if self.y < -h:
self.y = height + h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment