aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/example/BoxRenderer.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/example/BoxRenderer.java')
-rw-r--r--src/main/java/com/example/BoxRenderer.java64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/main/java/com/example/BoxRenderer.java b/src/main/java/com/example/BoxRenderer.java
new file mode 100644
index 0000000..9ec9c43
--- /dev/null
+++ b/src/main/java/com/example/BoxRenderer.java
@@ -0,0 +1,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]);
+ }
+ }
+}