Jump to content
• Advertisement

Public Group

# Measuring distance on Hex grid

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

If you intended to correct an error in the post then please contact us.

## 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 this post

##### Share on other sites
Advertisement
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 this post

##### 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.

#### Share this post

##### Share on other sites

• Advertisement
• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• Advertisement

• ### Popular Now

• 13
• 30
• 9
• 16
• 12
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!