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
|
package com.example;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class BoxRenderer {
private static final int[][] EDGES = {
{0, 1}, {1, 2}, {2, 3}, {3, 0},
{4, 5}, {5, 6}, {6, 7}, {7, 4},
{0, 4}, {1, 5}, {2, 6}, {3, 7}
};
public void draw(
GraphicsContext gc,
double canvasWidth,
double canvasHeight,
BoxModel box,
double angleX,
double angleY
) {
double hw = box.getWidth() / 2.0;
double hh = box.getHeight() / 2.0;
double hd = box.getDepth() / 2.0;
double[][] vertices = {
{-hw, -hh, -hd}, {hw, -hh, -hd}, {hw, hh, -hd}, {-hw, hh, -hd},
{-hw, -hh, hd}, {hw, -hh, hd}, {hw, hh, hd}, {-hw, hh, hd}
};
double[][] projected = new double[8][2];
double cx = canvasWidth / 2.0;
double cy = canvasHeight / 2.0;
double cosY = Math.cos(angleY);
double sinY = Math.sin(angleY);
double cosX = Math.cos(angleX);
double sinX = Math.sin(angleX);
double scale = 1.6;
for (int i = 0; i < vertices.length; i++) {
double x = vertices[i][0];
double y = vertices[i][1];
double z = vertices[i][2];
double x1 = x * cosY + z * sinY;
double z1 = -x * sinY + z * cosY;
double y2 = y * cosX - z1 * sinX;
double z2 = y * sinX + z1 * cosX;
projected[i][0] = cx + x1 * scale;
projected[i][1] = cy + y2 * scale + z2 * 0.08;
}
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, canvasWidth, canvasHeight);
gc.setStroke(Color.DARKBLUE);
gc.setLineWidth(2);
for (int[] edge : EDGES) {
int a = edge[0];
int b = edge[1];
gc.strokeLine(projected[a][0], projected[a][1], projected[b][0], projected[b][1]);
}
}
}
|