Skip to content

Commit b51547f

Browse files
committed
Factor trig lookups into util.py.
1 parent 266a760 commit b51547f

File tree

4 files changed

+54
-48
lines changed

4 files changed

+54
-48
lines changed

Whitney Artport - (Software) Structures/#003/B/Interpretation/Number_003_B_Ngan/Number_003_B_Ngan.pyde

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,26 @@ from circle import Circle
1515
from grid import Grid
1616

1717

18-
grid = None
18+
SketchWidth = 500
19+
SketchHeight = 500
20+
grid = Grid(TAU - PI / 3, SketchWidth, SketchHeight)
1921
NumCircles = 10
2022
circles = []
2123
counter = 0
22-
CirclePosition = 250
2324
Radius = 40
2425

2526

2627
def setup():
27-
global grid
28-
size(500, 500)
29-
# Init here because of sketch `width` & `height` being undefined prior to
30-
# this point.
31-
grid = Grid(TAU - PI / 3, 20, 3)
28+
size(SketchWidth, SketchHeight)
3229

3330

3431
def draw():
3532
global counter
3633
background(50)
3734
stroke(255, 255, 255, 50)
3835
if frameCount % 5 == 0 and counter < NumCircles:
39-
circles.append(Circle(CirclePosition, CirclePosition, Radius, counter))
36+
circles.append(Circle(SketchWidth / 2, SketchHeight / 2,
37+
Radius, counter))
4038
counter += 1
4139
grid.update()
4240
for circle in circles:

Whitney Artport - (Software) Structures/#003/B/Interpretation/Number_003_B_Ngan/circle.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
class Circle(object):
2-
TableLength = int((TAU * 100) + 1) # 628 - a full circle.
3-
sintable = [sin(i * 0.01) for i in range(TableLength)]
4-
costable = [cos(i * 0.01) for i in range(TableLength)]
1+
from util import getFunc
2+
from grid import Grid
3+
54

5+
class Circle(object):
66
def __init__(self, locX, locY, radius, index):
77
self.x = locX
88
self.y = locY
@@ -19,14 +19,14 @@ def __init__(self, locX, locY, radius, index):
1919
self.iny = 0
2020

2121
def getGrid(self, grid):
22-
sx = ceil((self.x - self.radius - grid.margin) / grid.gap)
23-
sy = ceil((self.y - self.radius - grid.margin) / grid.gap)
24-
numx = floor(self.diameter / grid.gap)
25-
numy = floor(self.diameter / grid.gap)
22+
sx = ceil((self.x - self.radius - Grid.MarginX) / Grid.GapX)
23+
sy = ceil((self.y - self.radius - Grid.MarginY) / Grid.GapY)
24+
numx = floor(self.diameter / Grid.GapX)
25+
numy = floor(self.diameter / Grid.GapY)
2626
for i in range(sx, sx + numx):
27-
if 0 <= i < grid.fieldSize:
27+
if 0 <= i < grid.fieldHeight:
2828
for k in range(sy, sy + numy):
29-
if 0 <= k < grid.fieldSize:
29+
if 0 <= k < grid.fieldWidth:
3030
if self.over:
3131
x, y = grid.getLocation(i, k)
3232
if (dist(self.x, self.y, x, y)
@@ -39,17 +39,17 @@ def getGrid(self, grid):
3939
self.over = False
4040

4141
def move(self, circles):
42-
angle = (Circle.getFunc('sin', self.speeds[0])
43-
- Circle.getFunc('cos', self.speeds[1]))
42+
angle = (getFunc('sin', self.speeds[0])
43+
- getFunc('cos', self.speeds[1]))
4444
self.speeds[0] += self.accels[0]
4545
self.speeds[1] += self.accels[1]
4646
self.speeds[2] += self.accels[2]
4747
if angle < 0:
4848
angle += TAU
4949
elif angle >= TAU:
5050
angle -= TAU
51-
self.x += Circle.getFunc('sin', angle)
52-
self.y += Circle.getFunc('cos', angle)
51+
self.x += getFunc('sin', angle)
52+
self.y += getFunc('cos', angle)
5353
self.checkBounds()
5454
self.checkOverlap(circles)
5555

@@ -85,22 +85,10 @@ def checkOverlap(self, circles):
8585

8686

8787
def repel(self, angle):
88-
self.x += Circle.getFunc('cos', angle) * 0.1
89-
self.y += Circle.getFunc('sin', angle) * 0.1
88+
self.x += getFunc('cos', angle) * 0.1
89+
self.y += getFunc('sin', angle) * 0.1
9090

9191
def setState(self, intersectX, intersectY):
9292
self.inx = intersectX
9393
self.iny = intersectY
9494
self.over = True
95-
96-
@staticmethod
97-
def getFunc(func, val):
98-
if val < 0:
99-
val += TAU
100-
if val >= TAU:
101-
val -= TAU
102-
val = min(TAU, max(0, val)) # 6.27
103-
if func == 'sin':
104-
return Circle.sintable[floor(val * 100)]
105-
elif func == 'cos':
106-
return Circle.costable[floor(val * 100)]
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
from circle import Circle
1+
from util import getFunc
22

33

44
class Grid(object):
55
'''A grid of values, capable of drawing a vector indicating direction.'''
6-
def __init__(self, value, margin, gap):
6+
MarginX = 20
7+
MarginY = 20
8+
GapX = 3
9+
GapY = 3
10+
MarginX2 = MarginX * 2
11+
MarginY2 = MarginY * 2
12+
def __init__(self, value, SketchWidth, SketchHeight):
713
self.value = value
8-
self.margin = margin
9-
self.marginSq = self.margin * 2
10-
self.gap = gap
11-
self.fieldSize = (width - self.marginSq) / self.gap
14+
self.fieldWidth = (SketchWidth - Grid.MarginX2) / Grid.GapX
15+
self.fieldHeight = (SketchHeight - Grid.MarginY2) / Grid.GapY
1216
self.field = [[self.value
13-
for _ in range(self.fieldSize)]
14-
for _ in range(self.fieldSize)]
17+
for _ in range(self.fieldHeight)]
18+
for _ in range(self.fieldWidth)]
1519

1620
def update(self):
1721
for i in range(len(self.field)):
1822
for k in range(len(self.field[0])):
1923
x, y = self.getLocation(i, k)
2024
line(x, y,
21-
x + 10 * Circle.getFunc('cos', self.field[i][k]),
22-
y + 10 * Circle.getFunc('sin', self.field[i][k]))
25+
x + 10 * getFunc('cos', self.field[i][k]),
26+
y + 10 * getFunc('sin', self.field[i][k]))
2327

24-
def getLocation(self, x, y):
25-
return x * self.gap + self.margin, y * self.gap + self.margin
28+
@staticmethod
29+
def getLocation(x, y):
30+
return x * Grid.GapX + Grid.MarginX, y * Grid.GapY + Grid.MarginY
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
TableLength = int((TAU * 100) + 1) # 628 - a full circle.
2+
sintable = [sin(i * 0.01) for i in range(TableLength)]
3+
costable = [cos(i * 0.01) for i in range(TableLength)]
4+
5+
6+
def getFunc(func, val):
7+
if val < 0:
8+
val += TAU
9+
if val >= TAU:
10+
val -= TAU
11+
val = min(TAU, max(0, val)) # 6.27
12+
if func == 'sin':
13+
return sintable[floor(val * 100)]
14+
elif func == 'cos':
15+
return costable[floor(val * 100)]

0 commit comments

Comments
 (0)