Skip to content

Instantly share code, notes, and snippets.

@meganehouser
Created December 2, 2019 04:09
Show Gist options
  • Save meganehouser/7b0dd1a42aae32830d6bd47544d37086 to your computer and use it in GitHub Desktop.
Save meganehouser/7b0dd1a42aae32830d6bd47544d37086 to your computer and use it in GitHub Desktop.
stars
import itertools
stars = []
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def should_connect(self, p):
return ((self.x - p.x) ** 2 + (self.y - p.y) ** 2) < 10000
class Star:
def __init__(self, radius, x, y, direction, radian):
self.radius =radius
self.x = x
self.y = y
self.direction = direction
self.radian = radian
def update_radian(self):
self.radian += self.direction * (TWO_PI / 360) * 0.4
def get_point(self):
x = cos(self.radian) * self.radius + self.x
y = sin(self.radian) * self.radius + self.y
return Point(x, y)
@classmethod
def random(cls):
return cls(
radius=random(300),
x=random(700),
y=random(700),
direction= 1 if random(2) == 1 else -1,
radian=random(TWO_PI),
)
def draw():
clear()
for s in stars:
s.update_radian()
points = [s.get_point() for s in stars]
strokeWeight(5)
for p in points:
point(p.x, p.y)
pairs = itertools.combinations(points, 2)
strokeWeight(1)
for (p1, p2) in pairs:
if p1.should_connect(p2):
line(p1.x, p1.y, p2.x, p2.y)
def setup():
size(700, 700)
background(20, 20, 20)
smooth()
strokeCap(ROUND)
stroke(200, 200, 200, 150)
global stars
stars = [Star.random() for _ in range(100)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment