Skip to content

Commit dbf4649

Browse files
committed
Cleanup, comments.
1 parent 2cc11e5 commit dbf4649

File tree

3 files changed

+124
-120
lines changed

3 files changed

+124
-120
lines changed

Whitney Artport - (Software) Structures/#003/A/Interpretation/Number_003_A_Hodgin/Number_003_A_Hodgin.pde

+12-15
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ int yMid = yStage/2; // y midpoint of applet
2424
int totalCircles = 100; // total number of circles
2525
Circle[] circle; // Circle object array
2626

27-
float gravity; // Strength of gravitational pull
27+
float gravity = .075;; // Strength of gravitational pull
2828
float xGrav; // x point of center of gravity
2929
float yGrav; // y point of center of gravity
3030
float xGravOffset;
3131

3232
float angleOffset;
33-
float initRadius;
34-
float maxDistance;
33+
float initRadius = 150;;
34+
float maxDistance = 150;;
3535

3636
color bgColor;
3737

@@ -65,11 +65,8 @@ void draw(){
6565
}
6666

6767
void createCircles(){
68-
gravity = .075;
69-
maxDistance = 150;
7068
angleOffset = random(360);
7169
circle = new Circle[totalCircles];
72-
initRadius = 150;
7370
for (int i=0; i<totalCircles; i++){
7471
float initAngle = i * 3.6 + angleOffset + random(10);
7572
float initTheta = (-((initAngle) * PI))/180;
@@ -124,7 +121,7 @@ class Circle
124121
float cxv; // Collision velocity along x axis
125122
float cyv; // Collision velocity along y axis
126123

127-
float gAngle; // Angle to gravity center in degrees
124+
float gAngle; // Angle to gravity xcenter in degrees
128125
float gTheta; // Angle to gravity center in radians
129126
float gxv; // Gravity velocity along x axis
130127
float gyv; // Gravity velocity along y axis
@@ -179,8 +176,8 @@ class Circle
179176
hasCollided[i] = true;
180177
circle[i].hasCollided[index] = true;
181178

182-
angles[i] = findAngle(x,y,circle[i].x,circle[i].y);
183-
thetas[i] = (-(angles[i] * PI))/180.0;
179+
angles[i] = findAngle(x,y,circle[i].x,circle[i].y); // degrees
180+
thetas[i] = (-(angles[i] * PI))/180.0; // degrees -> -radians
184181
cxv += cos(thetas[i]) * ((circle[i].r + r)/2.0);
185182
cyv += sin(thetas[i]) * ((circle[i].r + r)/2.0);
186183
numCollisions += 1;
@@ -203,8 +200,8 @@ class Circle
203200
if (hasCollided[i] && i != index){
204201
distances[i] = findDistance(x, y, circle[i].x, circle[i].y);
205202
if (distances[i] < maxDistance){
206-
angles[i] = findAngle(x,y,circle[i].x,circle[i].y);
207-
thetas[i] = (-(angles[i] * PI))/180.0;
203+
angles[i] = findAngle(x,y,circle[i].x,circle[i].y); // degrees
204+
thetas[i] = (-(angles[i] * PI))/180.0; // degrees -> -radians
208205
cxv += cos(thetas[i]) * (circle[i].r/8.0);
209206
cyv += sin(thetas[i]) * (circle[i].r/8.0);
210207
numConnections += 1;
@@ -227,8 +224,8 @@ class Circle
227224

228225

229226
void applyGravity(){
230-
gAngle = findAngle(x,y,xMid,yMid);
231-
gTheta = (-(gAngle * PI))/180;
227+
gAngle = findAngle(x,y,xMid,yMid); // degrees
228+
gTheta = (-(gAngle * PI))/180; // degrees -> -radians
232229
gxv = cos(gTheta) * gravity;
233230
gyv = sin(gTheta) * gravity;
234231
xv += gxv;
@@ -296,7 +293,7 @@ float findAngle(float x1, float y1, float x2, float y2){
296293
float xd = x1 - x2;
297294
float yd = y1 - y2;
298295

299-
float t = atan2(yd,xd);
300-
float a = (180 + (-(180 * t) / PI));
296+
float t = atan2(yd,xd); // radians
297+
float a = (180 + (-(180 * t) / PI)); // radians -> 180 + -degrees
301298
return a;
302299
}

Whitney Artport - (Software) Structures/#003/A/Interpretation/Number_003_A_Hodgin/Number_003_A_Hodgin.pyde

+22-25
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@ from circle import Circle
1717
# ****************************************************************************
1818
# INITIALIZE VARIABLES
1919
# ****************************************************************************
20-
sketchX = 600 # self.x dimension of applet
21-
sketchY = 600 # self.y dimension of applet
22-
sketchXMid = sketchX / 2 # self.x midpoint of applet
23-
sketchYMid = sketchY / 2 # self.y midpoint of applet
24-
Circle.TotalCircles = 100 # total number of circles
25-
Circle.Gravity = 0.075 # Strength of gravitational pull
26-
circles = [Circle(i, 0, 0, 0, 0) # Circle object list
27-
for i in range(Circle.TotalCircles)]
28-
20+
sketchX = 600 # x dimension of sketch.
21+
sketchY = 600 # y dimension of sketch.
22+
sketchXMid = sketchX / 2 # x midpoint of sketch.
23+
sketchYMid = sketchY / 2 # y midpoint of sketch.
24+
TotalCircles = 100 # Total number of circles.
25+
Circle.BgColor = color(255)
26+
Circle.FgColor = color(0)
27+
circles = None
2928

3029
# ****************************************************************************
3130
# SETUP FUNCTION
3231
# ****************************************************************************
3332
def setup():
34-
size(600, 600)
35-
background(255)
33+
size(sketchX, sketchY)
34+
background(Circle.BgColor)
3635
smooth()
3736
colorMode(RGB, 255)
3837
ellipseMode(RADIUS)
@@ -45,28 +44,26 @@ def setup():
4544
# MAIN LOOP FUNCTION
4645
# ****************************************************************************
4746
def draw():
48-
background(255)
47+
background(Circle.BgColor)
4948
for circle in circles:
5049
circle.behave(circles, sketchXMid, sketchYMid)
5150

5251

5352
def createCircles():
5453
angleOffset = random(360)
55-
for circle in circles:
56-
circle.xPos, circle.yPos = initCirclePos(circle.index, angleOffset,
57-
random(10))
58-
circle.xv = 0
59-
circle.yv = 0
54+
initRadius = 150
55+
circles = []
56+
for i in range(TotalCircles):
57+
xPos, yPos = initCirclePos(i, angleOffset, initRadius)
58+
circles.append(Circle(i, xPos, yPos, 0, 0, TotalCircles))
6059

6160

62-
def initCirclePos(index, angleOffset, rand):
63-
initRadius = 150
64-
initAngle = index * 3.6 + angleOffset + rand
65-
initTheta = (-((initAngle) * PI)) / 180
66-
initxv = cos(initTheta) * initRadius
67-
inityv = sin(initTheta) * initRadius
68-
return sketchXMid + initxv, sketchYMid + inityv
61+
def initCirclePos(index, angleOffset, initRadius):
62+
initTheta = radians(index * 3.6 + angleOffset + random(10))
63+
initXVel = cos(initTheta) * initRadius
64+
initYVel = sin(initTheta) * initRadius
65+
return sketchXMid + initXVel, sketchYMid + initYVel
6966

7067

71-
def mouseReleased():
68+
def mousePressed():
7269
createCircles()
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
class Circle(object):
2-
NumCollisions = 0 # Number of collisions in one frame
3-
NumConnections = 0 # Total number of collisions
4-
GravityAngle = 1 # Angle to gravity center in degrees
5-
GravityTheta = 1 # Angle to gravity center in radians
6-
TotalCircles = 0
7-
Gravity = 0
8-
CXV = 0
9-
CYV = 0
2+
# Angle to gravity center (in radians).
3+
GravityTheta = 0
4+
# Strength of gravitational pull.
5+
Gravity = 0.075
6+
# Collision velocity along x axis.
7+
xCollVel = 0
8+
# Collision velocity along y axis.
9+
yCollVel = 0
10+
BgColor = color(0)
11+
FgColor = color(0)
12+
MaxDistance = 150
1013

11-
def __init__(self, index, x, y, xv, yv):
14+
def __init__(self, index, x, y, xVel, yVel, TotalCircles):
15+
# Circle global ID.
1216
self.index = index
17+
# Circle x position.
1318
self.x = x
19+
# Circle y position.
1420
self.y = y
21+
# Circle radius.
1522
self.radius = 2
16-
self.xv = xv
17-
self.yv = yv
18-
self.mightCollide = [1] * Circle.TotalCircles
19-
self.hasCollided = [1] * Circle.TotalCircles
20-
self.distances = [1] * Circle.TotalCircles
21-
self.angles = [1] * Circle.TotalCircles
22-
self.thetas = [1] * Circle.TotalCircles
23+
# Current velocity along x-axis.
24+
self.xVel = xVel
25+
# Current velocity along y-axis.
26+
self.yVel = yVel
27+
# Storage for collisions which might happen.
28+
self.mightCollide = [0] * TotalCircles
29+
# Storage for collisions which happened.
30+
self.didCollide = [0] * TotalCircles
31+
# Storage for the distances between circles.
32+
self.distances = [0] * TotalCircles
33+
# Storage for the angle (in radians) between two connected circles.
34+
self.thetas = [0] * TotalCircles
35+
# Number of collisions in one frame.
36+
self.numCollisions = 0
37+
# Total number of collisions.
38+
self.numConnections = 0
2339

2440
def behave(self, circles, sketchXMid, sketchYMid):
2541
self.move()
@@ -28,7 +44,7 @@ def behave(self, circles, sketchXMid, sketchYMid):
2844
self.areWeConnected(circles)
2945
self.applyGravity(sketchXMid, sketchYMid)
3046
self.render(circles)
31-
self.reset()
47+
self.resetCollisions()
3248

3349
def areWeClose(self, others):
3450
for other in others:
@@ -47,80 +63,69 @@ def areWeColliding(self, others):
4763
other.x, other.y)
4864
if (self.distances[other.index]
4965
< (self.radius + other.radius) * 1.1):
50-
self.hasCollided[other.index] = True
51-
other.hasCollided[self.index] = True
52-
self.angles[other.index] = Circle.findAngle(self.x, self.y,
53-
other.x, other.y)
54-
self.thetas[other.index] = (-(self.angles[other.index] * PI)) / 180.0
55-
Circle.CXV += (cos(self.thetas[other.index])
56-
* ((other.radius + self.radius) / 2.0))
57-
Circle.CYV += (sin(self.thetas[other.index])
58-
* ((other.radius + self.radius) / 2.0))
59-
Circle.NumCollisions += 1
60-
if Circle.NumCollisions > 0:
61-
self.xv = -Circle.CXV / Circle.NumCollisions
62-
self.yv = -Circle.CYV / Circle.NumCollisions
63-
Circle.CXV = 0.0
64-
Circle.CYV = 0.0
66+
self.didCollide[other.index] = True
67+
other.didCollide[self.index] = True
68+
self.thetas[other.index] = self.findAngle(other.x, other.y)
69+
Circle.xCollVel += (cos(self.thetas[other.index])
70+
* ((other.radius + self.radius) / 2.0))
71+
Circle.yCollVel += (sin(self.thetas[other.index])
72+
* ((other.radius + self.radius) / 2.0))
73+
self.numCollisions += 1
74+
if self.numCollisions > 0:
75+
self.xVel = -Circle.xCollVel / self.numCollisions
76+
self.yVel = -Circle.yCollVel / self.numCollisions
77+
Circle.xCollVel = 0.0
78+
Circle.yCollVel = 0.0
6579

6680
def areWeConnected(self, others):
67-
maxDistance = 150
6881
for other in others:
69-
if (self.hasCollided[other.index]
70-
and self.index != other.index):
82+
if (self.didCollide[other.index]
83+
and other.index != self.index):
7184
self.distances[other.index] = dist(self.x, self.y,
7285
other.x, other.y)
73-
if self.distances[other.index] < maxDistance:
74-
self.angles[other.index] = Circle.findAngle(self.x, self.y,
75-
other.x, other.y)
76-
self.thetas[other.index] = (-(self.angles[other.index] * PI)) / 180.0
77-
Circle.CXV += cos(self.thetas[other.index]) * (self.radius / 8.0)
78-
Circle.CYV += sin(self.thetas[other.index]) * (self.radius / 8.0)
79-
Circle.NumConnections += 1
86+
if self.distances[other.index] < Circle.MaxDistance:
87+
self.thetas[other.index] = self.findAngle(other.x, other.y)
88+
Circle.xCollVel += (cos(self.thetas[other.index])
89+
* (other.radius / 8.0))
90+
Circle.yCollVel += (sin(self.thetas[other.index])
91+
* (other.radius / 8.0))
92+
self.numConnections += 1
8093
else:
81-
self.hasCollided[other.index] = False
82-
other.hasCollided[self.index] = False
83-
if Circle.NumConnections > 0:
84-
self.xv += (Circle.CXV / Circle.NumConnections) / 4.0
85-
self.yv += (Circle.CYV / Circle.NumConnections) / 4.0
86-
Circle.CXV = 0.0
87-
Circle.CYV = 0.0
88-
self.radius = Circle.NumConnections * .85 + 2
94+
self.didCollide[other.index] = False
95+
other.didCollide[self.index] = False
96+
if self.numConnections > 0:
97+
self.xVel += (Circle.xCollVel / self.numConnections) / 4.0
98+
self.yVel += (Circle.yCollVel / self.numConnections) / 4.0
99+
Circle.xCollVel = 0.0
100+
Circle.yCollVel = 0.0
101+
self.radius = self.numConnections * 0.85 + 2
89102

90103
def applyGravity(self, sketchXMid, sketchYMid):
91-
Circle.GravityAngle = Circle.findAngle(self.x, self.y,
92-
sketchXMid, sketchYMid)
93-
Circle.GravityTheta = (-(Circle.GravityAngle * PI)) / 180
94-
self.xv += cos(Circle.GravityTheta) * Circle.Gravity
95-
self.yv += sin(Circle.GravityTheta) * Circle.Gravity
104+
Circle.GravityTheta = self.findAngle(sketchXMid, sketchYMid)
105+
self.xVel += cos(Circle.GravityTheta) * Circle.Gravity
106+
self.yVel += sin(Circle.GravityTheta) * Circle.Gravity
96107

97108
def move(self):
98-
self.x += self.xv
99-
self.y += self.yv
109+
self.x += self.xVel
110+
self.y += self.yVel
100111

101112
def render(self, others):
102113
noStroke()
103-
fill(0, 25)
104-
ellipse(self.x, self.y, self.radius, self.radius)
105-
fill(0 + self.radius * 10, 50)
106-
ellipse(self.x, self.y, self.radius * 0.5, self.radius * 0.5)
107-
fill(0 + self.radius * 10)
108-
ellipse(self.x, self.y, self.radius * 0.3, self.radius * 0.3)
109-
if Circle.NumCollisions > 0:
114+
self.drawMe(0, 25, 1)
115+
self.drawMe(10, 50, 0.5)
116+
self.drawMe(10, 255, 0.3)
117+
if self.numCollisions > 0:
110118
noStroke()
111-
fill(0, 25)
112-
ellipse(self.x, self.y, self.radius, self.radius)
113-
fill(0, 55)
114-
ellipse(self.x, self.y, self.radius * 0.85, self.radius * 0.85)
115-
fill(0)
116-
ellipse(self.x, self.y, self.radius * 0.7, self.radius * 0.7)
119+
self.drawMe(0, 25, 1)
120+
self.drawMe(0, 55, 0.85)
121+
self.drawMe(0, 255, 0.7)
117122
for other in others:
118-
if (self.hasCollided[other.index]
123+
if (self.didCollide[other.index]
119124
and self.index != other.index):
120125
with beginShape(LINE_LOOP):
121126
xdist = self.x - other.x
122127
ydist = self.y - other.y
123-
stroke(0, 150 - self.distances[other.index] * 2.0)
128+
stroke(Circle.FgColor, 150 - self.distances[other.index] * 2.0)
124129
vertex(self.x, self.y)
125130
vertex(self.x - xdist * 0.25 + random(-1.0, 1.0),
126131
self.y - ydist * 0.25 + random(-1.0, 1.0))
@@ -129,14 +134,19 @@ def render(self, others):
129134
vertex(self.x - xdist * 0.75 + random(-1.0, 1.0),
130135
self.y - ydist * 0.75 + random(-1.0, 1.0))
131136
vertex(other.x, other.y)
132-
line (self.x, self.y, other.x, other.y);
137+
line(self.x, self.y, other.x, other.y)
133138
noStroke()
134139

135-
@classmethod
136-
def reset(cls):
137-
Circle.NumCollisions = 0
138-
Circle.NumConnections = 0
140+
def resetCollisions(self):
141+
self.numCollisions = 0
142+
self.numConnections = 0
143+
144+
def drawMe(self, shade, alpha, factor):
145+
r = self.radius * factor
146+
fill(Circle.FgColor + self.radius * shade, alpha)
147+
ellipse(self.x, self.y, r, r)
139148

140-
@classmethod
141-
def findAngle(cls, x1, y1, x2, y2):
142-
return 180 + (-(180 * (atan2(y1 - y2, x1 - x2))) / PI)
149+
def findAngle(self, otherX, otherY):
150+
t = atan2(self.y - otherY, self.x - otherX)
151+
a = -(PI + (-t)) # PI radians
152+
return a

0 commit comments

Comments
 (0)