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
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import my3d._line_art3d;
import my3d._math3d;
import my3d._math3d._transform;
import my3d._scene3d;
import my3d._shape3d;
public class _koch3d extends Application {
public static void main(String... args) {
Application.launch(args);
}
private static final int VIEW_W = 400, VIEW_H = 300;
private GraphicsContext gc;
private _line_art3d line_art;
private _transform tf = new _transform(0, 0, 0, 0, 0, 0);
@Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane);
stage.setScene(scene);
Canvas view = new Canvas(VIEW_W, VIEW_H);
pane.setCenter(view);
gc = view.getGraphicsContext2D();
// 情景の設定
_scene3d scene3d = new _scene3d();
scene3d.angle(-20, 20); scene3d.position(0, -0.5, 5);
scene3d.add_angle_listener(stage.getScene(), () -> draw());
// ラインアート情報の作成
line_art = new _line_art3d(gc, VIEW_W, VIEW_H, scene3d.get_transform());
_shape3d rect = _shape3d.create_rect(3, 3, tf);
line_art.add_shape(rect);
double[] pos = new double[] {0, -1.5, 0};
koch(3, 3, 4, pos);
draw();
stage.show();
}
private void draw() {
line_art.draw();
}
private void koch(int n, double prev_len, int prev_dir, double[] prev_pos) {
double[] pos = new double[3];
_math3d.copy_vector(prev_pos, pos);
double len = prev_len / 3;
double move = len * 2;
int skip_dir = (prev_dir + 3) % 6;
if (prev_dir < 3) move = -move;
pos[prev_dir % 3] += move;
_math3d.copy_vector(pos, tf.pos);
_shape3d box = _shape3d.create_box(len, len, len, tf);
line_art.add_shape(box);
if (n == 0) return;
n--;
for (int dir = 0; dir < 6; dir++) {
if (dir == skip_dir) continue;
koch(n, len, dir, pos);
}
}
}