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
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;
public class _inmen_shori 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 _transform tf;
@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();
tf = new _transform();
tf.angle(-30, 30);
tf.add_angle_listener(stage.getScene(), () -> draw());
draw();
stage.show();
}
private double[][] vertices = {
{1, 1, 1}, {1, 1, -1}, {-1, 1, -1}, {-1, 1, 1},
{1, -1, 1}, {1, -1, -1}, {-1, -1, -1}, {-1, -1, 1},
};
private int[][] faces = {
{0, 4, 5, 1}, {1, 5, 6, 2}, {2, 6, 7, 3}, {3, 7, 4, 0},
{0, 1, 2, 3}, {7, 6, 5, 4},
};
private void draw() {
gc.setTransform(1, 0, 0, 1, 0, 0);
gc.clearRect(0, 0, VIEW_W, VIEW_H);
gc.setTransform(1, 0, 0, -1, VIEW_W / 2, VIEW_H / 2);
double[][] points = new double[8][];
double[] v;
for (int i = 0; i < 8; i++) {
v = vertices[i];
points[i] = project(tf.mul(v[0], v[1], v[2]));
}
for (int[] face : faces)
draw_face(face, points);
}
private void draw_face(int[] face, double[][] pa) {
double[] p = pa[face[0]], p1 = pa[face[1]], p2 = pa[face[2]];
double x1 = p1[0] - p[0], y1 = p1[1] - p[1];
double x2 = p2[0] - p[0], y2 = p2[1] - p[1];
if (x1 * y2 - x2 * y1 >= 0) return;
gc.beginPath();
for (int i = 0; i < 4; i++) {
p = pa[face[i]];
if (i == 0) gc.moveTo(p[0], p[1]);
else gc.lineTo(p[0], p[1]);
}
gc.closePath();
gc.stroke();
}
private double[] project(double[] v) {
double z = 1 + v[2] / 5;
v[0] *= 70 / z; v[1] *= 70 / z;
return v;
}
}