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
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 _toushi_zuhou 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();
stage.setScene(new Scene(pane));
Canvas view = new Canvas(VIEW_W, VIEW_H);
gc = view.getGraphicsContext2D();
tf = new _transform();
tf.angle(0, 60);
tf.position(-40, -40, -50);
tf.add_angle_listener(stage.getScene(), () -> { draw(); });
pane.setCenter(view);
draw();
stage.show();
}
private double[][] house = {
{ 0, 0 }, { 80, 0 }, { 80, 50 }, { 40, 80 }, { 0, 50 },
{ 0, 100 },
};
private double[][] entotsu = {
{ 50, 72 }, { 65, 61 }, { 65, 90 }, { 50, 90 },
{ 0, 20 },
};
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);
draw_shape(house);
draw_shape(entotsu);
}
private void draw_shape(double[][] shape) {
int n = shape.length - 1;
double[][] front = new double[n][];
double[][] back = new double[n][];
for (int i = 0; i < n; i++) {
front[i] = tf.mul(shape[i][0], shape[i][1], shape[n][0]);
back[i] = tf.mul(shape[i][0], shape[i][1], shape[n][1]);
project(front[i]); project(back[i]);
draw_line(front[i][0], front[i][1], back[i][0], back[i][1]);
}
draw_polygon(front);
draw_polygon(back);
}
private void draw_polygon(double[][] va) {
gc.beginPath();
gc.moveTo(va[0][0], va[0][1]);
for (int i = 1; i < va.length; i++)
gc.lineTo(va[i][0], va[i][1]);
gc.closePath();
gc.stroke();
}
private void draw_line(double x1, double y1, double x2, double y2) {
gc.beginPath();
gc.moveTo(x1, y1);
gc.lineTo(x2, y2);
gc.stroke();
}
private void project(double[] v) {
double z = 1 + v[2] / 200;
v[0] *= 1.5 / z; v[1] *= 1.5 / z;
}
}