-
Notifications
You must be signed in to change notification settings - Fork 0
/
Galaxy.pde
92 lines (71 loc) · 2.32 KB
/
Galaxy.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// A procedurally generated Spiral galaxy
class Galaxy implements Drawable {
ArrayList<Star> stars;
int numStars;
int maxX = 400, maxY = 400;
int MIN_X = 30, MIN_Y = 30;
float blackHoleSize;
color cStar;
// Generate a disk galaxy
private void generateDisk() {
for(int a = 0; a < this.numStars; a++) {
Star o = new Star(random(maxX), random(maxY), randColor());
stars.add(o);
}
}
private color spiralColor() {
// Generate reddish colors for spiral; not in use
return color(random(214, 220), 242, 255);
}
private void generateSpiral() {
float scale = 2;
float upperBound = 6 * PI;
float spiralCompression = 0.25;
float stepSize = (upperBound / this.numStars) * 2;
for(float t = 0.0; t < upperBound; t += stepSize) {
float x = scale * exp(spiralCompression*t) * cos(t);
float y = scale * exp(spiralCompression*t) * sin(t);
// Angle between this point and origin
float theta = atan2(y, x);
// Fuzz distance from generate point
float fuzzFactor = min(0.2 * (exp(0.25 * t)) + 3, 30);
float fuzz = randomGaussian() * fuzzFactor;
// Generate offset from point
float xOffset = fuzz * cos(theta);
float yOffset = fuzz * sin(theta);
// Final coordinates
float finalX = x + xOffset;
float finalY = y + yOffset;
// Create two mirrored stars
stars.add(new Star(finalX, finalY, spiralColor()));
stars.add(new Star(-finalX, -finalY, spiralColor()));
}
// Create non-spiral disk stars
int diskStars = 1000;
float scatter = 65;
color diskColor = color(255, 235, 63);
for(int a = 0; a < diskStars; a++) {
float x = randomGaussian() * scatter;
float y = randomGaussian() * scatter;
stars.add(new Star(x, y, diskColor));
}
}
public Galaxy(int nStars) {
this.numStars = nStars;
stars = new ArrayList();
generateSpiral();
blackHoleSize = random(5, 15);
cStar = color(0, 0, 0);
}
public void drawme(float t) {
// White point light at center
pointLight(255, 255, 255, 0, 0, 0);
// Draw black hole at center of galaxy
fill(0);
sphere(blackHoleSize);
// Render stars
for(Star s: stars) {
s.drawme(t);
}
}
}