Sample1
import java.applet.*;
import java.awt.*;
public class Sample1 extends Applet {
static final int WIDTH = 640, HEIGHT = 400;
static final double C = 4.0 / WIDTH;
static final double YA = Math.sqrt(3) / 2;
static final double THRESHOLD = 0.05 * 0.05;
public void paint(Graphics g) {
int i, j, k;
double x, y, x2, y2, d, t;
for (i = -WIDTH/2; i <= WIDTH/2; i++) {
for (j = 0; j <= HEIGHT/2; j++) {
x = C * i;
y = C * j;
for (k = 0; k <= 15; k++) {
x2 = x * x;
y2 = y * y;
d = x2 + y2;
if (d < 1E-10) break;
d *= d;
t = (1.0/3.0) * (2*x + (x2 - y2) / d);
y = (2.0/3.0) * y * (1 - x/d);
x = t;
if (dist2(x-1, y) < THRESHOLD) {
dot(g, i, +j, Color.blue);
dot(g, i, -j, Color.cyan);
break;
}
if (dist2(x+0.5, y+YA) < THRESHOLD) {
dot(g, i, +j, Color.magenta);
dot(g, i, -j, Color.yellow);
break;
}
if (dist2(x+0.5, y-YA) < THRESHOLD) {
dot(g, i, +j, Color.green);
dot(g, i, -j, Color.red);
break;
}
}
}
}
}
private double dist2(double x, double y) {
return x * x + y * y;
}
private void dot(Graphics g, int x, int y, Color c) {
g.setColor(c);
g.fillRect(WIDTH / 2 + x, HEIGHT / 2 - y, 1, 1); // 画面中心を原点として,(x,y)に点を打つ
}
}