2013-05-27 47 views
0

我想在我的十六进制地图中显示最大移动重叠。例如:在六角形地图上显示最大范围

中心点位于50,50 最大允许移动量为5格。

这是我使用用于重叠的代码:

for (int height = lowHeight; height <= highHeight; height++) 
{ 
    for (int width = lowWidth; width <= highWidth; width++) 
    { 
     [self hexOnMap:height :width :@"green"]; 
    } 
} 

宽度为x坐标50 高度为y坐标50

lowHeight =高度 - 5

highHeight =高度+ 5

lowWidth = width - 5

hightWidth = width + 5

很明显,我的循环不工作,因为角落的运动超过5格。因为我觉得我的智商在一分钟内下降,有人请告诉我明显的:)并且5的移动值不是静态的。

enter image description here

编辑:@DPenner

感谢您的答复。我尝试了类似的东西,但这个被诅咒的东西仍然拒绝工作。你的代码显示了这样的结果:

enter image description here

编辑2:@DPenner - 你几乎拥有它。我正在上传覆盖您的代码,以便您可以看到。昨天晚上我发现了一篇很棒的文章,给了我解决这个问题所需的线索。但我真的很感谢你的帮助,并试图解决这个问题!

enter image description here

+0

产地是:X = 50 Y = 50 /顶:50, 49 /右上:51,49 /右下:51,50 /下:50,51 /左下:49,50 /左上:49,49。我在左上角有x和y零。 – sangony

回答

1

经过将近24小时的不眠之后,我发现了一篇很好的文章,讨论了这个问题。这篇文章是在这里:

http://keekerdc.com/2011/03/hexagon-grids-coordinate-systems-and-distance-calculations/

这里是代码,使这一切工作:

for (int y = minY; y <= maxY; y++) 
{ 
    for (int x = minX; x <= maxX; x++) 
    { 
     int xDistance = (x - startXcoordinate); 

     int yStart = 0; 
     if(x > startXcoordinate) 
      yStart = -1; 

     int yDistance = ((xDistance * -1) + yStart)/2; 

     yDistance = yDistance + (y - startYcoordinate); 

     int z = (xDistance + yDistance)* -1 ; 


     int maxDistance = 0; 

     if(abs(xDistance) > maxDistance) 
      maxDistance = abs(xDistance); 

     if(abs(yDistance) > maxDistance) 
      maxDistance = abs(yDistance); 

     if(abs(z) > maxDistance) 
      maxDistance = abs(z); 

     if(abs(maxDistance) <= patrolRange) 
      [self hexOnMap:y :x :@"green"]; 
    } 
} 

enter image description here

1

我已经删除了我的旧的答案,因为这是完全错误的:我忘了考虑相邻不吉利的东西可以在两个x和y坐标有时不同。捕获这是棘手,但下面的代码应工作:

如果中心X坐标为偶数:

for (int width = lowWidth; width <= highWidth; width++) 
{   
    double heightNeeded = 5 - abs((centerX - width)/2.0); 
    for (int height = centerY - (int)ceil(heightNeeded); height <= centerY + (int)floor(heightNeeded); height++) 
    { 
     [self hexOnMap:height :width :@"green"]; 
    } 
} 

如果中心X坐标为奇数,交换高斯符号。将5改为不同大小的叠加层。

我手工检查过,它似乎工作正常。外部循环是宽度/ X循环,因为它的X坐标是水平曲折的,这样细胞和底部函数“固定”在内部高度/ Y循环中。