获得某个点周围的位置半径

时间:2013-03-23 01:08:35

标签: objective-c cocos2d-iphone

我需要获得某个点周围点的位置。 E.I. :

[x] [x] [ 2 ] [x] [x]

[x] [ 2 ] [ 1 ] [ 2 ] [x]

[ 2 ] [ 1 ] [ c ] [ 1 ] [ 2

[x] [ 2 ] [ 1 ] [ 2 ] [x]

[x] [x] [ 2 ] [x] [x] (1 =半径为1,2 =半径为2,c =中心点)

现在我正在使用 (CCSprite = [array objectAtIndex:i] CGPoint pos = sprite.position (pos.x+1,pos.y)(pos.x-1,pos.y)(pos.x,pos.y+1)(pos.x,pos.y-1)获取坐标,我添加那些到array然后通过for循环的次数等于半径。但是我无法添加已经存在于阵列中的位置,并使其在半径大于1的情况下正常工作。

3 个答案:

答案 0 :(得分:0)

请看这个图表,它显示了半径3的偏移量:

Radius 3 diagram

我已将细胞分为四组,标记为“象限1”至“象限4”。请注意,在每个组中,所有x个偏移都具有相同的符号,并且所有y偏移具有相同的符号。在所有12个单元格中,abs(x) + abs(y) = radius。所以,考虑到所有这些,我们可以编写一个循环来访问半径为3的所有单元格:

int radius = 3;
for (int i = 1; i < radius; ++i) {
    int j = radius - i;
    [self visitCellAtXOffset:+j yOffset:+i]; // quadrant 1
    [self visitCellAtXOffset:-i yOffset:+j]; // quadrant 2
    [self visitCellAtXOffset:-j yOffset:-i]; // quadrant 3
    [self visitCellAtXOffset:+i yOffset:-j]; // quadrant 4
}

当然你可以将半径从1循环到你需要的任何东西。如果您还想访问中心单元格(偏移0,0),请在循环外单独调用。

答案 1 :(得分:0)

我认为你在这里寻找的是网格上每个点的曼哈顿距离

http://en.wikipedia.org/wiki/Taxicab_geometry

您可以将其计算为任意两个点,只需abs( x1 - x2 ) + abs( y1 - y2 ),因此无需为每个半径重复计算。只需保持x1,y1固定并遍历所有x2,y2点。

如果您正在寻找实际上是圆形的东西,欧几里德距离sqrt( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) )

给出

答案 2 :(得分:0)

我自己想出来了,但感谢那些帮助过的人。这假设在你的最终数组中你在索引0处添加了中心坐标。最终结果将是一个数组(在本例中为attackableTiles),其中包含CCSprites(从中可以获取坐标),其中包含一个位于中心点周围的位置半径。正如我原来的问题所描述的那样。

int timesThrough = 0;
int pRange = (the desired radius)
//(attackableTiles and goingToAddTiles are both NSMutableArrays)
if (timesThrough < pRange) {
        for (int i = 0; i < attackableTiles.count; i++) {
            if (attackableTiles.count < ((pRange*pRange) +((pRange+1)*(pRange+1)))) {
                CCSprite * tile = [attackableTiles objectAtIndex:i];
                CGPoint pos = [self tileCoordForPosition:tile.position];
                CGPoint posXUp1 = ccp(pos.x+1, pos.y);
                CGPoint posXDown1 = ccp(pos.x-1, pos.y);
                CGPoint posYUp1 = ccp(pos.x, pos.y+1);
                CGPoint posYDown1 = ccp(pos.x, pos.y-1);
                CCSprite * addingTileXUp1 = [CCSprite spriteWithFile:@"Icon-72.png"];
                addingTileXUp1.position = [self positionForTileCoord:posXUp1];
                CCSprite * addingTileXDown1 = [CCSprite spriteWithFile:@"Icon-72.png"];
                addingTileXDown1.position = [self positionForTileCoord:posXDown1];
                CCSprite * addingTileYUp1 = [CCSprite spriteWithFile:@"Icon-72.png"];
                addingTileYUp1.position = [self positionForTileCoord:posYUp1];
                CCSprite * addingTileYDown1 = [CCSprite spriteWithFile:@"Icon-72.png"];
                addingTileYDown1.position = [self positionForTileCoord:posYDown1];

                addedXUp1 = FALSE;
                addedYUp1 = FALSE;
                addedXDown1 = FALSE;
                addedYDown1 = FALSE;

                for (int i = 0; i < attackableTiles.count; i++) {
                        CCSprite * tile = [attackableTiles objectAtIndex:i];
                        if (CGPointEqualToPoint(tile.position, addingTileXUp1.position)) {
                            addedXUp1 = TRUE;
                        }    
                }
                if (!addedXUp1) {
                    [goingToAddTiles addObject:addingTileXUp1];
                }

                for (int i = 0; i < attackableTiles.count; i++) {
                    CCSprite * tile = [attackableTiles objectAtIndex:i];
                    if (CGPointEqualToPoint(tile.position, addingTileYUp1.position)) {
                        addedYUp1 = TRUE;
                    }
                }
                if (!addedYUp1) {
                    [goingToAddTiles addObject:addingTileYUp1];
                }

                for (int i = 0; i < attackableTiles.count; i++) {
                    CCSprite * tile = [attackableTiles objectAtIndex:i];
                    if (CGPointEqualToPoint(tile.position, addingTileXDown1.position)) {
                        addedXDown1 = TRUE;
                    }
                }
                if (!addedXDown1) {
                    [goingToAddTiles addObject:addingTileXDown1];
                }

                for (int i = 0; i < attackableTiles.count; i++) {
                    CCSprite * tile = [attackableTiles objectAtIndex:i];
                    if (CGPointEqualToPoint(tile.position, addingTileYDown1.position)) {
                        addedYDown1 = TRUE;
                    }
                }
                if (!addedYDown1) {
                    [goingToAddTiles addObject:addingTileYDown1];
                }

                [attackableTiles addObjectsFromArray:goingToAddTiles];
                [goingToAddTiles removeAllObjects];
                timesThrough++;

            }

        }