Skip to content

Commit

Permalink
Fix hex light construction
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kwvanderlinde committed Oct 5, 2024
1 parent 866eb94 commit e662f70
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions src/main/java/net/rptools/maptool/model/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit e662f70

Please sign in to comment.