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
package sample3d;
import my3d._math3d;
import my3d._math3d._matrix;
import my3d._math3d._point;
import my3d._math3d._transform;
import my3d._math3d._vector;
import my3d._slider_dialog._my_slider;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class _sample1 extends Application {
public static void main(String... args) {
Application.launch(args);
}
private static final int VIEW_W = 400, VIEW_H = 300;
private Canvas view;
private double round_x, round_y;
private _vector[] vertices = new _vector[8];
private _transform tf;
@Override
public void start(Stage stage) throws Exception {
// TODO 自動生成されたメソッド・スタブ
// レイアウトの作成
BorderPane border_pane = new BorderPane();
// ビューの作成
view = new Canvas(VIEW_W, VIEW_H);
border_pane.setCenter(view);
// スライダの作成
_my_slider slider;
slider = new _my_slider(-90, 90, round_x, Orientation.VERTICAL,
(val) -> {
round_x = val;
set_angle(round_x, round_y);
repaint();
});
border_pane.setRight(slider);
slider = new _my_slider(180, -180, round_y, Orientation.HORIZONTAL,
(val) -> {
round_y = val;
set_angle(round_x, round_y);
repaint();
});
border_pane.setBottom(slider);
// 頂点座標の作成
for (int i = 0; i < 8; i++)
vertices[i] = new _vector(
(i % 4 == 0 || i % 4 == 1) ? 1 : -1,
(i < 4) ? 1 : -1,
(i % 4 == 0 || i % 4 == 3) ? 1 : -1);
// 表示
stage.setTitle("3Dサンプル");
stage.setScene(new Scene(border_pane));
stage.setResizable(false);
stage.show();
set_angle(round_x, round_y);
repaint();
}
public void set_angle(double tx, double ty) {
tx *= Math.PI / 180;
ty *= Math.PI / 180;
tf = new _transform(
_math3d.mul(_matrix.rot_x(tx), _matrix.rot_y(ty)),
new _vector(0, 0, 5));
}
public void repaint() {
GraphicsContext gc = view.getGraphicsContext2D();
gc.clearRect(0, 0, VIEW_W, VIEW_H);
_vector[] va = new _vector[8];
for (int i = 0; i < 8; i++)
va[i] = _math3d.mul(tf, vertices[i]);
_point[] pa = new _point[8];
for (int i = 0; i < 8; i++)
pa[i] = project(va[i]);
for (int i = 0; i < 4; i++) {
int j = (i + 1) % 4;
draw_line(gc, pa[i], pa[j]);
draw_line(gc, pa[i], pa[i + 4]);
draw_line(gc, pa[i + 4], pa[j + 4]);
}
}
private _point project(_vector v) {
return new _point(
VIEW_W / 2 + (v.x / v.z) * VIEW_W,
VIEW_H / 2 - (v.y / v.z) * VIEW_W);
}
private void draw_line(GraphicsContext gc, _point p1, _point p2) {
gc.beginPath();
gc.moveTo(p1.x, p1.y);
gc.lineTo(p2.x, p2.y);
gc.stroke();
}
}