Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide strict getByPixelCoordinate(x,y) #32

Open
timrobertson100 opened this issue Sep 7, 2016 · 3 comments
Open

Provide strict getByPixelCoordinate(x,y) #32

timrobertson100 opened this issue Sep 7, 2016 · 3 comments

Comments

@timrobertson100
Copy link

timrobertson100 commented Sep 7, 2016

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

@adam-arold
Copy link
Member

Good catch! I'll look into it. Conceptually this method should give you a non-present result for any pixel which is not part of a hexagon so I'll try to implement it like this.

@HoldYourWaffle
Copy link
Contributor

Is this still an issue? The current documentation reads:

@return Maybe with a Hexagon if it is present

Implying that there won't be a result if there is no Hexagon at the specified coordinates.

@adam-arold
Copy link
Member

I'm gonna check, but I don't recall making changes to the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants