From e662f709b6e3d4ffa55f5f30f07a84224305c4aa Mon Sep 17 00:00:00 2001 From: Kenneth VanderLinde Date: Sat, 5 Oct 2024 14:14:42 -0700 Subject: [PATCH] Fix hex light construction The proportions and center were just all wrong. `Grid.getShapedArea()` should always treat (0, 0) as its origin for the light, so no adjustments are needed to account for the grid or token shape. Also fixed a bug where the hex radius would measure to the vertices instead of the edges (compare with square lights that measure to the edges). Since we're modifying the hex code anyways, also refactored to be more direct. We don't need trig - there's only six points and they're always the same up to scale. Well, until the day we account for stretched hex grids. --- .../java/net/rptools/maptool/model/Grid.java | 39 ++++++------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/rptools/maptool/model/Grid.java b/src/main/java/net/rptools/maptool/model/Grid.java index 91d3754509..c90d13bf7a 100644 --- a/src/main/java/net/rptools/maptool/model/Grid.java +++ b/src/main/java/net/rptools/maptool/model/Grid.java @@ -479,16 +479,7 @@ public void setSize(int size) { visibleArea.add(new Area(footprintPart)); } case HEX -> { - double x = footprint.getCenterX(); - double y = footprint.getCenterY(); - - double footprintWidth = footprint.getWidth(); - double footprintHeight = footprint.getHeight(); - double adjustment = Math.min(footprintWidth, footprintHeight); - x -= adjustment / 2; - y -= adjustment / 2; - - visibleArea = createHex(x, y, visionRange, 0); + visibleArea = createHex(visionRange); } default -> { log.error("Unhandled shape {}; treating as a circle", shape); @@ -527,25 +518,19 @@ public double cellDistance(CellPoint cellA, CellPoint cellB, WalkerMetric wmetri return distance; } - protected Area createHex(double x, double y, double radius, double rotation) { - GeneralPath hexPath = new GeneralPath(); + protected Area createHex(double inRadius) { + double radius = inRadius * 2 / Math.sqrt(3); - for (int i = 0; i < 6; i++) { - if (i == 0) { - hexPath.moveTo( - x + radius * Math.cos(i * 2 * Math.PI / 6), y + radius * Math.sin(i * 2 * Math.PI / 6)); - } else { - hexPath.lineTo( - x + radius * Math.cos(i * 2 * Math.PI / 6), y + radius * Math.sin(i * 2 * Math.PI / 6)); - } - } + var hexPath = new Path2D.Double(); + hexPath.moveTo(radius, 0); + hexPath.lineTo(radius * 0.5, inRadius); + hexPath.lineTo(-radius * 0.5, inRadius); + hexPath.lineTo(-radius, 0); + hexPath.lineTo(-radius * 0.5, -inRadius); + hexPath.lineTo(radius * 0.5, -inRadius); + hexPath.closePath(); - if (rotation != 0) { - AffineTransform atArea = AffineTransform.getRotateInstance(rotation); - return new Area(atArea.createTransformedShape(hexPath)); - } else { - return new Area(hexPath); - } + return new Area(hexPath); } private void fireGridChanged() {