Sync#

Demonstration of the Kuramoto model for synchronization. From https://synchrony.cc.

from proceso import Sketch


p5 = Sketch()
p5.describe("Ten white circles moving like fireflies on a dark blue background.")

bugs = []
num_bugs = 10
coupling: object


def setup():
    p5.create_canvas(720, 400)
    global coupling
    coupling = p5.create_slider(0, 10, 5)
    for _ in range(num_bugs):
        bug = Bug()
        bugs.append(bug)


def draw():
    p5.background("midnightblue")

    for bug in bugs:
        bug.sync()

    for bug in bugs:
        bug.update()
        bug.check_edges()
        bug.draw()


class Bug:
    def __init__(self):
        self.x = p5.width * 0.5
        self.y = p5.height * 0.5
        self.r = 5
        self.angle = p5.random(p5.TWO_PI)
        self.da_dt = 1
        self.dt = 0.01
        self.freq = p5.random(5, 10)

    def draw(self):
        a = p5.remap(self.angle % p5.TWO_PI, 0, p5.TWO_PI, 0, 255)
        p5.fill(255, a)
        p5.stroke(255, a)
        p5.circle(self.x, self.y, 2 * self.r)

    def update(self):
        self.x += p5.cos(self.angle)
        self.y += p5.sin(self.angle)
        self.angle += self.da_dt * self.dt
        self.da_dt = 0

    def check_edges(self):
        if self.x > p5.width + self.r:
            self.x = -self.r
        if self.x < -self.r:
            self.x = p5.width + self.r
        if self.y > p5.height + self.r:
            self.y = -self.r
        if self.y < -self.r:
            self.y = p5.height + self.r

    def sync(self):
        K_N = coupling.value() / num_bugs
        self.da_dt = self.freq
        for bug in bugs:
            self.da_dt += K_N * p5.sin(bug.angle - self.angle)


p5.run_sketch(setup=setup, draw=draw)

View sketch