Skip to content

Commit 6dcb246

Browse files
authored
Created Rainbow Program
1 parent 49ae4f7 commit 6dcb246

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

Diff for: Rainbow Program

+10-18
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,40 @@ public class Rainbow {
1111
StdDraw.setYscale(0, SIZE/2.0);
1212
StdDraw.enableDoubleBuffering();
1313

14-
// sky blue background
1514
Color skyblue = new Color(48, 144, 199);
1615
StdDraw.clear(skyblue);
1716

18-
// simulate N incident light rays
1917
for (int i = 0; i < N; i++) {
2018

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;
2421
double x = Math.sqrt(r) * Math.sin(theta);
2522
double y = Math.sqrt(r) * Math.cos(theta);
2623

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;
2930

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)
3833
double intensityP = 0.5 * ( s*(1-s)*(1-s) + p*(1-p)*(1-p));
3934
double intensityS = 0.5 * (s*s*(1-s)*(1-s) + p*p*(1-p)*(1-p));
4035

41-
// plot primary rainbow
4236
float saturation = (float) Math.min(intensityP / 0.04, 1.0);
4337
StdDraw.setPenColor(Color.getHSBColor(c, saturation, 1.0f));
4438
double xp = (SIZE/2.0 * thetaP * 3 / Math.PI * x / r) + SIZE / 2.0;
4539
double yp = (SIZE/2.0 * thetaP * 3 / Math.PI * Math.abs(y) / r);
4640
StdDraw.point(xp, yp);
4741

48-
// plot secondary rainbow
4942
saturation = (float) Math.min(intensityS / 0.02, 1.0);
5043
StdDraw.setPenColor(Color.getHSBColor(c, saturation, 1.0f));
5144
double xs = ( SIZE/2.0 * thetaS * 3 / Math.PI * x / r) + SIZE / 2.0;
5245
double ys = (-SIZE/2.0 * thetaS * 3 / Math.PI * Math.abs(y) / r);
5346
StdDraw.point(xs, ys);
5447

55-
// display every 1000 iterates
5648
if (i % 1000 == 0) {
5749
StdDraw.show();
5850
StdDraw.pause(10);

0 commit comments

Comments
 (0)