Translate the code from the BASIC idiom to Java and then compile and run. You sh
ID: 3864108 • Letter: T
Question
Translate the code from the BASIC idiom to Java and then compile and run. You should see a Rainbow gradually appearing, point by point, in the Display window. /* Notes: PRINT becomes system.out.println ( ) ; PRINT USING is formatted output, you can likely ignore the formatting REM is a comment INPUT is a combination of System.out.println ( prompt) then use a Scanner to getNext ( ) the needed value. ^ means exponentiation, or "raising to a power" : allows multiple BASIC statements on the same line. GOSUB was a way to run a function. */ // Global floating points -- like BASIC float R, RC, R0, X, Y, B, C, N, I, T1, T2, RS, RP, RB, Rc, I1, I2, TH ; int NP = 0 ; // REM PLOT ON SCREEN void plot ( ) { /* 180 REM PLOT ON SCREEN 185 TH=ABS(TH) 190 IF TH>60 THEN RETURN 195 XP=320+320*(TH/60)*(X/B) 200 YP=325-300*(TH/60)*ABS(Y/B) 205 PSET(XP,YP),C: NP=NP+1 210 LOCATE 1,1: PRINT NP: RETURN */ if ( TH > 60.0 ) { return ; } float XP=139+139*(TH/60)*(X/B) ; float YP=159-159*(TH/60)*abs(Y/B) ; stroke ( C, 100, 100 ) ; point( XP + 300, 600 - YP ) ; NP ++ ; println ( NP + "," + XP + "," + YP + "," + C ) ; return ; } void setup ( ) { size ( 900, 600 ) ; background ( 0 ) ; // https://processing.org/reference/colorMode_.html colorMode(HSB, 100); } void draw ( ) { // Convert this BASIC to Java here: // Notes: for gosub 180, use // plot ( ) ; // // for the goto 30, use a do/while loop // // find Java/Processing equivalents for ATN, SIN, SQR, RND and ^ /* 25 R0=180/3.14159 30 REM RANDOM IMPACT PARAMETER 35 X=-1+2*RND(1) 40 Y=-1+2*RND(1) 45 B=SQR(X*X+Y*Y) 50 IF B>=1 THEN 30 55 REM COLOR & INDEX OF REFR. 60 C=1+INT(3*RND(1)) 65 N=1.33+.01*(C-1) 70 REM COMPUTE ANGLES 75 I=ATN(B/SQR(1-B*B)) 80 R=ATN(B/SQR(N*N-B*B)) 85 T1=(4*R-2*I)*R0 90 T2=(6*R-2*I)*R0-180 95 REM INTENSITY FACTORS 100 RS=(SIN(I-R)/SIN(I+R))^2 105 RP=(TAN(I-R)/TAN(I+R))^2 110 RB=(1-RP)*(1-RP) 115 RC=(1-RS)*(1-RS) 120 I1=(RS*RC+RP*RB)/2 125 I2=(RS*RS*RC+RP*RP*RB)/2 130 IF I1<.04*RND(1) THEN 140 135 TH=T1: GOSUB 180 140 IF I2<.02*RND(1) THEN 150 145 TH=T2: GOSUB 180 150 GOTO 30 */ }
Explanation / Answer
import java.io.*;
import java.awt.Graphics;
import java.awt.Container;
import java.awt.Color;
public class Rainbow {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int SIZE = 1000;
StdDraw.setCanvasSize(SIZE, SIZE/2);
StdDraw.setXscale(0, SIZE);
StdDraw.setYscale(0, SIZE/2.0);
StdDraw.enableDoubleBuffering();
for (int i = 0; i < n; i++) {
// generate random (x, y) uniformly in unit circle centered at (0, 0)
double r = Math.random();
double theta = Math.random() * 2 * Math.PI;
double x = Math.sqrt(r) * Math.sin(theta);
double y = Math.sqrt(r) * Math.cos(theta);
float c = (float) Math.random();
double n = 1.33 + 0.06 * c; // refraction index
double thetaI = Math.asin(r);
double thetaR = Math.asin(r / n);
double thetaP = 4*thetaR - 2*thetaI; // primary rainbow angle
double thetaS = 6*thetaR - 2*thetaI - Math.PI; // secondary rainbow angle
double p = Math.pow(Math.sin(thetaI - thetaR) / Math.sin(thetaI + thetaR),2);
double s = Math.pow(Math.tan(thetaI - thetaR) / Math.tan(thetaI + thetaR), 2);
double intensityP = 0.5 * ( s*(1-s)*(1-s) + p*(1-p)*(1-p));
double intensityS = 0.5 * (s*s*(1-s)*(1-s) + p*p*(1-p)*(1-p));
// plot primary rainbow
float saturation = (float) Math.min(intensityP / 0.04, 1.0);
StdDraw.setPenColor(Color.getHSBColor(c, saturation, 1.0f));
double xp = (SIZE/2.0 * thetaP * 3 / Math.PI * x / r) + SIZE / 2.0;
double yp = (SIZE/2.0 * thetaP * 3 / Math.PI * Math.abs(y) / r);
StdDraw.point(xp, yp);
// plot secondary rainbow
saturation = (float) Math.min(intensityS / 0.02, 1.0);
StdDraw.setPenColor(Color.getHSBColor(c, saturation, 1.0f));
double xs = ( SIZE/2.0 * thetaS * 3 / Math.PI * x / r) + SIZE / 2.0;
double ys = (-SIZE/2.0 * thetaS * 3 / Math.PI * Math.abs(y) / r);
StdDraw.point(xs, ys);
// display every 1000 iterates
if (i % 1000 == 0) {
StdDraw.show();
StdDraw.pause(10);
}
}
StdDraw.show();
}
}
I have written the code as above.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.