0

我需要获取某个点周围的点的位置。艾:

[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 的情况下正常工作。

4

3 回答 3

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++;

            }

        }
于 2013-03-23T22:22:30.557 回答
0

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

半径 3 图

我将单元格分为四组,分别标记为“象限 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),请在循环外单独调用。

于 2013-03-23T03:12:32.430 回答
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) )

于 2013-03-23T03:20:46.577 回答