Public Group

# Measuring distance on Hex grid

This topic is 2650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I'm trying to figure out how to measure the distance between two hexes in a map. I can't seem to wrap my head around what to do for my setup. I've tried to follow http://www-cs-studen...les/HexLOS.html but it doesn't give me the desired results. Here is a screenshot of my setup:

As you can see, with a "range" of 2 the following tiles should NOT have been highlighted in yellow:

3, 3
4, 3
6, 3
7, 3
3, 7
7, 7

Here is my code:

 public int myDistance(int x1, int y1, int x2, int y2) { int dist = 0; int dx, dy, dt = 0; dx = Math.abs(x1 - x2); dy = Math.abs(y1 - y2); dt = Math.abs(dx-dy); dist = Math.max(dx, dy); dist = Math.max(dist, dt); return dist; } 

Any assistance would be greatly appreciate!

##### Share on other sites
I'll give it a try:
public int myDistance(int x1, int y1, int x2, int y2) { int sane_y1 = y1 - x1/2; int sane_y2 = y2 - x2/2; int dx = x2 - x1; int dy = sane_y2 - sane_y1; return dx*dy > 0 ? Math.abs(dx+dy) : Math.max(Math.abs(dx), Math.abs(dy)); } 

##### Share on other sites

I'll give it a try:
public int myDistance(int x1, int y1, int x2, int y2) { int sane_y1 = y1 - x1/2; int sane_y2 = y2 - x2/2; int dx = x2 - x1; int dy = sane_y2 - sane_y1; return dx*dy > 0 ? Math.abs(dx+dy) : Math.max(Math.abs(dx), Math.abs(dy)); } 

Fantastic! Thank you very much.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633767
• Total Posts
3013735
×