Okay, so I\'m supposed to write a program that draws out the Julia set in cool c
ID: 3633901 • Letter: O
Question
Okay, so I'm supposed to write a program that draws out the Julia set in cool colors from a mouse click using stddraw. The first problem that I am running into, is I can't get the mouse click to pick up the x and y coordinates and the second problem is I can't get the colors to work but I am positive that my math is correct. Here is what I have so far, I hope you can help. This program requires Complex.java located here http://introcs.cs.princeton.edu/java/32class/Complex.java.html and the standard input/output libraries here http://introcs.cs.princeton.edu/java/stdlib/. Hope this helps!
import java.awt.Color;
public class ColorJulia {
static int julia(Complex c, Complex z, int max) {
for (int t = 0; t < max; t++) {
if (z.abs() > 2.0) return t;
z = z.times(z).plus(c);
}
return max - 1;
}
public static void main(String[] args) {
StdDraw.setCanvasSize(512,512);
StdDraw.setXscale(-2,2);
StdDraw.setYscale(-2,2);
double real = StdDraw.mouseX();
double imag = StdDraw.mouseY();
System.out.println(real+","+imag);
Complex c = new Complex((double) real, (double) imag);
double xmin = -2.0;
double ymin = -2.0;
double width = 4.0;
double height = 4.0;
int N = 512;
int max = 256;
StdDraw.setPenRadius(0.01);
StdDraw.show(0);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
double x = xmin + i * width / N;
double y = ymin + j * height / N;
Complex a = new Complex(y, x);
Complex z = new Complex(x, y);
int r = julia(a,z,max);
int g = julia(z,a,max);
int b = julia(a,a,max);
Color[] colors = new Color(r,g,b);
StdDraw.setPenColor(colors);
StdDraw.point(x, y);
}
}
StdDraw.show(0);
}
}
Explanation / Answer
If your colors aren't working, you need to make sure that your jawa.awt.color file is not corrupt and will show up properly. Otherwise, your math looks correct.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.