Jump to content
  • Advertisement
Sign in to follow this  
squizm

Measuring distance on Hex grid

This topic is 2445 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:

WryCC.jpg


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


Link to 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


Link to 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


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!