-
Notifications
You must be signed in to change notification settings - Fork 34
Description
This is an enhancement request.
The current implementation of getByPixelCoordinate() provides the nearest hexagon when called with a pixel which lies outside the grid but within the distance of one hexagon. It is therefore an approximation. This makes good sense for many applications whereby a user is clicking a screen pixel, and some tolerance is acceptable. There are certain use cases however, such as the binning of points into hexagons which require a guarantee that the coordinate lies within the hexagon.
While this behaviour remains, a workaround for this scenario is proposed as:
- Instantiate a grid that is 1 hexagon larger all around than necessary. Should the hexagons be used on multiple adjacent canvases (e.g. on map tiles) whereby the grid is further offset to ensure adjacent hexagons align, then a larger buffer is recommended. Note for
flatTopyou need to increase by 2 hexagons left and right to achieve 1 complete hexagon. - Offset the coordinates by the buffer size. (e.g. for
flat toppedhexagons with a buffer of 4 hexagons horizontally (2 left, 2 right) and 2 vertically, theoffsetXis1.5*hexWidth, and theoffsetYis1*hexHeightbefore callinggetByPixelCoordinate(x,y) - Compensate at rendering time by adjusting the vertices of the hexagon by the same offset.
As a quick fix I suggest adding a sentence in the Javadoc for the method as follows:
Note that at the boundaries this is a lenient match. When the pixel provided does not fall within a hexagon on the grid, but is within a close distance of the grid, the nearest hexagon will be returned. Where strict hexagon matching is required, please refer to the workaround offered on https://github.com/Hexworks/hexameter/issues/32