Skip to content

Provide strict getByPixelCoordinate(x,y) #32

@timrobertson100

Description

@timrobertson100

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:

  1. 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 flatTop you need to increase by 2 hexagons left and right to achieve 1 complete hexagon.
  2. Offset the coordinates by the buffer size. (e.g. for flat topped hexagons with a buffer of 4 hexagons horizontally (2 left, 2 right) and 2 vertically, the offsetX is 1.5*hexWidth, and the offsetY is 1*hexHeight before calling getByPixelCoordinate(x,y)
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions