graph theory project (particles.js mimic)

This commit is contained in:
Xevion
2019-10-16 13:28:02 -05:00
parent 3aa7f92c35
commit 261f05cee2
3 changed files with 68 additions and 1 deletions

View File

@@ -0,0 +1,63 @@
import random
class MovingPoint:
def __init__(self, x, y):
self.x, self.y = x, y
self.xSpeed, self.ySpeed = random.randint(-5, 5), random.randint(-5, 5)
self.rotation = random.randint(0, 360)
def tick(self):
def rnd():
lower, upper, minimum, maximum = -5, 5, -5, 5
self.xSpeed = self.xSpeed + random.choice([_ for _ in range(lower, upper)])
self.ySpeed = self.ySpeed + random.choice([_ for _ in range(lower, upper)])
self.xSpeed = max(min(self.xSpeed, maximum), minimum)
self.ySpeed = max(min(self.ySpeed, maximum), minimum)
if self.x >= width:
rnd()
self.x = 0
elif self.x <= 0:
self.x = width
rnd()
if self.y >= height:
self.y = 0
rnd()
elif self.y <= 0:
self.y = height
rnd()
self.x, self.y = self.x + self.xSpeed, self.y + self.ySpeed
def render(self):
def lowest(mpoint, points):
dists = []
for point in points:
dists.append((dist(self.x, self.y, point[0], point[1])))
tupled = zip(points, dists)
tupled.sort(key=lambda x: x[1])
return [x[0] for x in tupled[1:3]]
def dist(x1, y1, x2, y2):
return sqrt((abs(x1 - x2) ** 2) + (abs(y1 - y2) ** 2))
ellipse(self.x, self.y, 5, 5)
global MovingPoints
for point in lowest((self.x, self.y), [(MV.x, MV.y) for MV in MovingPoints]):
line(self.x, self.y, point[0], point[1])
resetMatrix()
def setup():
size(500, 500)
frameRate(15)
global MovingPoints
MovingPoints = []
for _ in range(70):
MovingPoints.append(MovingPoint(random.randint(0, width), random.randint(0, height)))
def draw():
global MovingPoints
background(204)
for MP in MovingPoints:
MP.tick()
MP.render()

View File

@@ -0,0 +1,2 @@
mode=Python
mode.id=jycessing.mode.PythonMode

View File

@@ -15,3 +15,5 @@ Other sketches without any specific purpose. If enough fit into a specific categ
- **DynamicLines** - A very simple line drawing application.
- **FlipFlopChain** - A simple project displaying how one would count in binary.
- **Graph-Theory** - A simple project closely mimicing the particle.js library.