六边形分形程序

玩桌游时,想到写个程序自动生成卡坦岛那样的,以正六边形为单位的地图。
可以生成任意层数的地图。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import javax.swing.*;  
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
public class MyPaint extends JFrame {

public MyPaint(String title){
super(title);
}

Container container = getContentPane();
DrawingPanel panel = new DrawingPanel();
public void go(){
setSize(500, 500);
setLocation(200, 200);
container.add(panel);
setVisible(true);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {

try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e){
System.out.print(e);
}

MyPaint paint = new MyPaint("java painter");
paint.go();

}
}
class DrawingPanel extends JPanel{
static double R = 30;
int oldX, oldY, newX, newY;
Color color = Color.blue;
public DrawingPanel(){
setBackground(Color.white);
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseListener());

}

public void paint(Graphics g){
//g.drawArc(10, 10, 10, 10, 10, 190);
g.setColor(color);
g.drawLine(oldX, oldY, newX, newY);

//specify the max level of map
drawMap(g, 6);

}

/*
* draw a map of Catan
* @param g
*/
private void drawMap(Graphics g, int maxLevel) {

double r = R * Math.pow(3, 0.5) / 2;
double offsetX = R * (maxLevel * 2+ 1) + 5;
double offsetY = r * (maxLevel * 2+ 1) + 5;
drawPoly6(g, 0 + offsetX, 0 + offsetY); //the central one
double xi[] = new double[6];
double yi[] = new double[6];
double tempx, tempy;
for(int level = 1; level <= maxLevel; level++) {

for(int j = 0; j < 6; j++) {
xi[j] = (2 * level * r) * Math.cos(Math.PI / 6 + Math.PI / 3 * j);
yi[j] = (2 * level * r) * Math.sin(Math.PI / 6 + Math.PI / 3 * j);
//drawPoly6(g, xi[j] + offsetX, yi[j] + offsetY);
g.drawString(String.valueOf(level), (int)(xi[j] + offsetX), (int)(yi[j] + offsetY));
}

for(int k = 1; k <= level; k++) {
for(int j = 0; j < 6; j++) {
//Interpolation
tempx = xi[(j + 1) % 6];
tempy = yi[(j + 1) % 6];
drawPoly6(g,
k * (tempx - xi[j]) / level + xi[j] + offsetX,
k * (tempy - yi[j]) / level + yi[j] + offsetY);
}
}
}

}

private void drawPoly6(Graphics g, double x, double y) {
int xx[] = new int[6];
int yy[] = new int[6];
for(int i = 0; i < 6; i++) {
xx[i] = (int) (x + R * Math.cos(Math.PI / 3 * i));
yy[i] = (int) (y + R * Math.sin(Math.PI / 3 * i)); Math.round(1);

System.out.println(xx[i] + ":" + yy[i]);
}

g.drawPolygon(xx, yy, xx.length);

}

class MyMouseListener extends MouseAdapter implements MouseMotionListener{
public void mousePressed(MouseEvent e){
oldX = newX = e.getX();
oldY = newY = e.getY();
}
public void mouseDragged(MouseEvent e){
oldX = newX;
oldY = newY;
newX = e.getX();
newY = e.getY();
repaint();

}
}

}