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

Euclidean Distances on Hexagonal Grid #130

Open
MattWenham opened this issue Sep 8, 2018 · 3 comments
Open

Euclidean Distances on Hexagonal Grid #130

MattWenham opened this issue Sep 8, 2018 · 3 comments

Comments

@MattWenham
Copy link
Contributor

I believe there is an issue with the way the routines euclideanDistanceOnHexagonalPlanarMap and euclideanDistanceOnHexagonalToroidMap are calculating the Euclidean distance between cells on a hexagonal grid.

If we consider the standard indexing system for a hexagonal grid to be as Kohonen intended:

image

then the Euclidean distance between a node and any of its six neighbours is 1 by definition.

If calculating the distance between e.g. nodes (1,1) and (2,2) using euclideanDistanceOnHexagonalPlanarMap, as far as I can see, the result would be approximately 1.11803:

x1 = 1
y1 = 1
x2 = 2
y2 = 2
xdist = 1
ydist = 1
xdist += -0.5 /* so now xdist = 0.5 */
return sqrt(float(0.5 * 0.5 + 1 * 1)) /* Return value is 1.11803... */

This is of little consequence - though still some - when using a 'gaussian' neighbourhood. However, when using 'bubble', the conditional if (distance <= radius) { in getWeight will e.g. fail to connect a node with four of its six neighbours when 'radius' is 1.

The simplest fix I can see for this is to change the return expression to

return sqrt(float(xdist * xdist + ydist * ydist * 0.75));

The factor of 0.75 is the square of cos(30), which is the height of an equilateral triangle of base 1. Cos(30) is therefore the factor by which ydist needs to be scaled to correct the Euclidean distance calculation. This fix should also be applied to euclideanDistanceOnHexagonalToroidMap.

MattWenham added a commit to MattWenham/somoclu that referenced this issue Sep 8, 2018
@MattWenham
Copy link
Contributor Author

Pull Request created.

peterwittek added a commit that referenced this issue Sep 8, 2018
@MattWenham
Copy link
Contributor Author

Thanks Peter. Is there any way I can incorporate this fix into the Python version before you do the next 'official' Python release?

@peterwittek
Copy link
Owner

Just rebuild it from git. I don't think there will be a new release this year. The code is very much in maintenance mode.

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

No branches or pull requests

2 participants