@@ -11,48 +11,40 @@ public class Rainbow {
11
11
StdDraw.setYscale(0, SIZE/2.0);
12
12
StdDraw.enableDoubleBuffering();
13
13
14
- // sky blue background
15
14
Color skyblue = new Color(48, 144, 199);
16
15
StdDraw.clear(skyblue);
17
16
18
- // simulate N incident light rays
19
17
for (int i = 0; i < N; i++) {
20
18
21
- // generate random (x, y) uniformly in unit circle centered at (0, 0)
22
- double r = Math.random(); // impact parameter
23
- double theta = Math.random() * 2 * Math.PI; // between 0 and 2*pi
19
+ double r = Math.random();
20
+ double theta = Math.random() * 2 * Math.PI;
24
21
double x = Math.sqrt(r) * Math.sin(theta);
25
22
double y = Math.sqrt(r) * Math.cos(theta);
26
23
27
- float c = (float) Math.random(); // random hue of rainbow
28
- double n = 1.33 + 0.06 * c; // refraction index
24
+ float c = (float) Math.random();
25
+ double n = 1.33 + 0.06 * c;
26
+ double thetaI = Math.asin(r);
27
+ double thetaR = Math.asin(r / n);
28
+ double thetaP = 4*thetaR - 2*thetaI;
29
+ double thetaS = 6*thetaR - 2*thetaI - Math.PI;
29
30
30
- double thetaI = Math.asin(r); // angle of incidence
31
- double thetaR = Math.asin(r / n); // angle of refraction
32
- double thetaP = 4*thetaR - 2*thetaI; // primary rainbow angle
33
- double thetaS = 6*thetaR - 2*thetaI - Math.PI; // secondary rainbow angle
34
-
35
- // intensities of principle and secondary
36
- double p = Math.pow(Math.sin(thetaI - thetaR) / Math.sin(thetaI + thetaR), 2);
37
- double s = Math.pow(Math.tan(thetaI - thetaR) / Math.tan(thetaI + thetaR), 2);
31
+ double p = Math.pow(Math.sin(thetaI - thetaR)
32
+ double s = Math.pow(Math.tan(thetaI - thetaR)
38
33
double intensityP = 0.5 * ( s*(1-s)*(1-s) + p*(1-p)*(1-p));
39
34
double intensityS = 0.5 * (s*s*(1-s)*(1-s) + p*p*(1-p)*(1-p));
40
35
41
- // plot primary rainbow
42
36
float saturation = (float) Math.min(intensityP / 0.04, 1.0);
43
37
StdDraw.setPenColor(Color.getHSBColor(c, saturation, 1.0f));
44
38
double xp = (SIZE/2.0 * thetaP * 3 / Math.PI * x / r) + SIZE / 2.0;
45
39
double yp = (SIZE/2.0 * thetaP * 3 / Math.PI * Math.abs(y) / r);
46
40
StdDraw.point(xp, yp);
47
41
48
- // plot secondary rainbow
49
42
saturation = (float) Math.min(intensityS / 0.02, 1.0);
50
43
StdDraw.setPenColor(Color.getHSBColor(c, saturation, 1.0f));
51
44
double xs = ( SIZE/2.0 * thetaS * 3 / Math.PI * x / r) + SIZE / 2.0;
52
45
double ys = (-SIZE/2.0 * thetaS * 3 / Math.PI * Math.abs(y) / r);
53
46
StdDraw.point(xs, ys);
54
47
55
- // display every 1000 iterates
56
48
if (i % 1000 == 0) {
57
49
StdDraw.show();
58
50
StdDraw.pause(10);
0 commit comments