Skip to content

Commit 7fd0c89

Browse files
committed
- Complete refactor.
- Works, but it seems as though it's drawing far too many lines. Also performance...
1 parent 46c594a commit 7fd0c89

File tree

2 files changed

+79
-71
lines changed

2 files changed

+79
-71
lines changed

nightSky/nightSky.pyde

+16-67
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,31 @@
11
# http://patakk.tumblr.com / nightSky
22
from point import Point
3-
# from line import Line
43

54

5+
SketchWidth = 700
6+
SketchHeight = 300
7+
Point.HalfWidth = SketchWidth / 2
8+
Point.HalfHeight = SketchHeight / 2
9+
Point.HalfDist = dist(0, 0, Point.HalfWidth, Point.HalfHeight)
10+
MagicNumber = dist(0, 0, 960, 540)
11+
NumPoints = (500.0 * Point.HalfDist / MagicNumber)
12+
Point.Limit = (160.0 * Point.HalfDist / MagicNumber)
613
points = []
7-
lines = []
8-
NumPoints = 0
9-
Limit = 0
10-
HalfWidth = 0
11-
HalfHeight = 0
12-
HalfDist = 0
13-
I_Dont_Know_What_This_Is = dist(0, 0, 960, 540)
14+
1415

1516
def setup():
16-
global points, HalfWidth, HalfHeight, HalfDist, NumPoints, Limit
17-
size(700, 300)
17+
global points
18+
size(SketchWidth, SketchHeight)
1819
background(0)
1920
smooth()
2021
strokeWeight(0.8)
21-
HalfWidth = width / 2
22-
HalfHeight = height / 2
23-
HalfDist = dist(0, 0, HalfWidth, HalfHeight)
24-
NumPoints = (500.0 * HalfDist / I_Dont_Know_What_This_Is)
25-
Limit = (160.0 * HalfDist / I_Dont_Know_What_This_Is)
26-
# n = 0
27-
# noiseSeed(5)
28-
# randomSeed(1200)
29-
# makePoint()
30-
points = [makePoint() for _ in range(NumPoints)]
31-
for point1 in points:
32-
x1 = point1.startX
33-
y1 = point1.startY
34-
for point2 in points:
35-
x2 = point2.startX
36-
y2 = point2.startY
37-
if dist(x1, y1, x2, y2) < Limit / 3:
38-
lines.append(PVector(points.index(point1),
39-
points.index(point2)))
40-
41-
def makePoint():
42-
while True:
43-
randX = random(HalfWidth * 0.74) + random(40)
44-
randY = random(HalfHeight * 0.84) + random(40)
45-
angle = random(2 * PI)
46-
startX = randX * cos(angle)
47-
startY = randY * sin(angle)
48-
dx = map(startX,
49-
0, HalfWidth, 0, 1.15)
50-
dy = map(startY,
51-
0, HalfHeight, 0, 1.35)
52-
prob = pow(2.72, -(dx**2 * 2 + dy**2 * 2))
53-
if random(1) < prob:
54-
return(Point(startX, startY))
22+
points = [Point(index) for index in range(NumPoints)]
23+
for _point in points:
24+
_point.setLines(points)
5525

5626

5727
def draw():
5828
background(0)
59-
translate(HalfWidth, HalfHeight)
29+
translate(Point.HalfWidth, Point.HalfHeight)
6030
for _point in points:
61-
_point.update(frameCount)
62-
for _line in lines:
63-
x1 = points[int(_line.x)].x
64-
y1 = points[int(_line.x)].y
65-
x2 = points[int(_line.y)].x
66-
y2 = points[int(_line.y)].y
67-
plusX = x1 + x2 / 2
68-
plusY = y1 + y2 / 2
69-
mouseWrapX = mouseX - HalfWidth
70-
mouseWrapY = mouseY - HalfHeight
71-
amp = (map(dist(plusX, plusY, 0, 0),
72-
0, HalfDist, 2, 8))
73-
distance = (map(noise(plusX * 0.03, plusY * 0.03),
74-
0, 1, 5, Limit / 2))
75-
if dist(plusX, plusY, mouseWrapX, mouseWrapY) < Limit:
76-
distance = (distance * map(dist(plusX, plusY, mouseWrapX, mouseWrapY),
77-
0, Limit, amp, 1))
78-
if dist(x1, y1, x2, y2) < distance:
79-
opacity = map(dist(x1, y1, x2, y2),
80-
0, distance, 85, 0)
81-
stroke(255, opacity)
82-
line(x1, y1, x2, y2)
31+
_point.update(frameCount, points)

nightSky/point.py

+63-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
class Point(object):
2-
def __init__(self, startX, startY):
3-
self.startX = startX
4-
self.startY = startY
2+
HalfWidth = 0
3+
HalfHeight = 0
4+
HalfDist = 0
5+
Limit = 0
6+
7+
def __init__(self, index):
8+
self.index = index
9+
self.startX = 0
10+
self.startY = 0
511
self.rnd = random(5, 30)
612
self.diameter = map(self.rnd, 5, 30, 0.5, 2)
713
if random(1) > 0.5:
@@ -11,8 +17,39 @@ def __init__(self, startX, startY):
1117
self.phi = random(360)
1218
self.x = 0
1319
self.y = 0
20+
self.lines = []
21+
self.makePoint()
22+
23+
def makePoint(self):
24+
while True:
25+
randX = random(Point.HalfWidth * 0.74) + random(40)
26+
randY = random(Point.HalfHeight * 0.84) + random(40)
27+
angle = random(2 * PI)
28+
initX = randX * cos(angle)
29+
initY = randY * sin(angle)
30+
dx = map(initX,
31+
0, Point.HalfWidth, 0, 1.15)
32+
dy = map(initY,
33+
0, Point.HalfHeight, 0, 1.35)
34+
prob = pow(2.72, -(dx**2 * 2 + dy**2 * 2))
35+
if random(1) < prob:
36+
self.startX = initX
37+
self.startY = initY
38+
return
39+
40+
def setLines(self, others):
41+
# for other in others:
42+
# if (self is not other
43+
# and dist(self.x, self.y, other.x, other.y) < Limit / 3):
44+
# lines.append(PVector(self.index,
45+
# other.index))
46+
thirdLimit = Point.Limit / 3
47+
self.lines = [other.index
48+
for other in others
49+
if (self is not other
50+
and dist(self.x, self.y, other.x, other.y) < thirdLimit)]
1451

15-
def update(self, time):
52+
def update(self, time, others):
1653
if self.rt:
1754
self.x = self._calc(self.startX, cos, time)
1855
self.y = self._calc(self.startY, sin, time)
@@ -22,7 +59,29 @@ def update(self, time):
2259
noStroke()
2360
fill(255)
2461
ellipse(self.x, self.y, self.diameter, self.diameter)
62+
self.drawLines(others)
2563

64+
def drawLines(self, others):
65+
halfLimit = Point.Limit / 2
66+
for other in others:
67+
if (self is not other
68+
and other.index in self.lines):
69+
plusX = self.x + other.x * 0.5
70+
plusY = self.y + other.y * 0.5
71+
mouseWrapX = mouseX - Point.HalfWidth
72+
mouseWrapY = mouseY - Point.HalfHeight
73+
amp = (map(dist(plusX, plusY, 0, 0),
74+
0, Point.HalfDist, 2, 8))
75+
distance = (map(noise(plusX * 0.03, plusY * 0.03),
76+
0, 1, 5, halfLimit))
77+
if dist(plusX, plusY, mouseWrapX, mouseWrapY) < Point.Limit:
78+
distance = (distance * map(dist(plusX, plusY, mouseWrapX, mouseWrapY),
79+
0, Point.Limit, amp, 1))
80+
if dist(self.x, self.y, other.x, other.y) < distance:
81+
opacity = map(dist(self.x, self.y, other.x, other.y),
82+
0, distance, 85, 0)
83+
stroke(255, opacity)
84+
line(self.x, self.y, other.x, other.y)
2685

2786
def _calc(self, coord, func, time):
2887
return coord + self.rnd * func(radians(time * self.diameter + self.phi))

0 commit comments

Comments
 (0)