APL/DEV 3.2/TP02/Flocon/Flocon.java

105 lines
2.7 KiB
Java
Raw Normal View History

2022-10-12 13:29:36 +02:00
import java.awt.*;
import javax.swing.JFrame;
public class Flocon extends Polygon {
Vector2[] ptable;
public Flocon() {
super();
}
2022-10-14 17:25:23 +02:00
private int floc(int n, int offset, int aoffset) {
2022-10-12 13:29:36 +02:00
if (n < 0) return offset+1;
Vector2 local = ptable[offset].toLocal(ptable[offset+1]);
local.div(3);
Vector2 s1 = Vector2.add(ptable[offset], local);
ptable[offset+1] = s1;
if (n > 0) {
2022-10-14 17:25:23 +02:00
offset = floc(n-1, offset, aoffset);
2022-10-12 13:29:36 +02:00
} else offset++;
2022-10-14 17:25:23 +02:00
Vector2 s2 = Vector2.add(s1, Vector2.rotate(local, -60 - aoffset));
2022-10-12 13:29:36 +02:00
ptable[offset+1] = s2;
if (n > 0) {
2022-10-14 17:25:23 +02:00
offset = floc(n-1, offset, aoffset);
2022-10-12 13:29:36 +02:00
} else offset++;
2022-10-14 17:25:23 +02:00
Vector2 s3 = Vector2.add(s2, Vector2.rotate(local, 60 + aoffset));
2022-10-12 13:29:36 +02:00
ptable[offset+1] = s3;
if (n > 0) {
2022-10-14 17:25:23 +02:00
offset = floc(n-1, offset, aoffset);
2022-10-12 13:29:36 +02:00
} else offset++;
Vector2 s4 = Vector2.add(s3, local);
ptable[offset+1] = s4;
if (n > 0) {
2022-10-14 17:25:23 +02:00
offset = floc(n-1, offset, aoffset);
2022-10-12 13:29:36 +02:00
} else offset++;
return offset;
}
2022-10-14 17:25:23 +02:00
public void createFloc(int x, int y, int r, int n, int aoffset) {
2022-10-12 13:29:36 +02:00
int size = 4;
for (int i = 0; i < n; i++) size = size * 4 - 3;
this.ptable = new Vector2[size];
int offset = 0;
ptable[0] = Vector2.rotate(new Vector2(0, -r),-60).add(new Vector2(x, y));
ptable[1] = Vector2.rotate(new Vector2(0, -r), 60).add(new Vector2(x, y));
2022-10-14 17:25:23 +02:00
offset = floc(n-1, offset, aoffset);
2022-10-12 13:29:36 +02:00
ptable[offset+1] = Vector2.rotate(new Vector2(0, -r), 180).add(new Vector2(x, y));
2022-10-14 17:25:23 +02:00
offset = floc(n-1, offset, aoffset);
2022-10-12 13:29:36 +02:00
ptable[offset+1] = Vector2.rotate(new Vector2(0, -r), -60).add(new Vector2(x, y));
2022-10-14 17:25:23 +02:00
floc(n-1, offset, aoffset);
2022-10-12 13:29:36 +02:00
for (Vector2 v : ptable) {
addPoint(v.x, v.y);
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocation(100, 100);
f.setSize(1200, 1200);
Flocon p = new Flocon();
2022-10-14 17:25:23 +02:00
p.createFloc(f.getWidth() / 2, f.getHeight() / 2 - 100, 500, 4, 0);
2022-10-12 13:29:36 +02:00
2022-10-14 17:25:23 +02:00
FloconPanel fp = new FloconPanel(p);
2022-10-12 13:29:36 +02:00
2022-10-14 17:25:23 +02:00
f.add(fp);
2022-10-12 13:29:36 +02:00
f.setVisible(true);
2022-10-14 17:25:23 +02:00
int a = 0;
while(true) {
try {
Thread.sleep(16);
} catch (Exception e) {
}
a += 1;
p.xpoints = new int[0];
p.ypoints = new int[0];
p.npoints = 0;
p.createFloc(f.getWidth() / 2, f.getHeight() / 2 - 100, 500, 4, a);
fp.repaint();
}
2022-10-12 13:29:36 +02:00
}
}