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
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.scene.paint.Color;
import javafx.stage.Stage;
public class _chinou_test 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, 135);
tf.position(0, -2, 0);
tf.add_angle_listener(stage.getScene(), () -> draw());
draw();
stage.show();
}
private double[][] vertices = {
{1, 1, 1}, {1, 1, 0}, {0, 1, 0}, {0, 1, 1},
{1, 0, 1}, {1, 0, 0}, {0, 0, 0}, {0, 0, 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 int[][] blocks = {
{6, 4, 3}, {5, 2, 0}, {1, 0, 0},
};
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);
gc.setFill(Color.WHITE);
for (int z = 0; z < 3; z++)
for (int x = 0; x < 3; x++)
for (int y = 0; y < blocks[z][x]; y++)
draw_block(x, y, z);
}
private void draw_block(int x, int y, int z) {
double[][] points = new double[8][];
double[] v;
for (int i = 0; i < 8; i++) {
v = vertices[i];
points[i] = project(tf.mul(v[0] + x, v[1] + y, v[2] + z));
}
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.fill();
gc.stroke();
}
private double[] project(double[] v) {
double z = 1 + v[2] / 10;
v[0] *= 30 / z; v[1] *= 30 / z;
return v;
}
}