【问题标题】:Figuring out if a point on a grid is surrounded by points of a certain kind确定网格上的点是否被某种点包围
【发布时间】:2013-08-10 20:58:41
【问题描述】:

在我的游戏中,玩家可以激活网格中的对象(此处为图块)。在示例图片中,假设所有红色瓷砖都已“激活”。当玩家触摸任何红色瓷砖(例如带有紫色箭头的瓷砖)时,我想向所有汇合的相邻红色瓷砖以及任何被红色瓷砖(绿色瓷砖)绑定的瓷砖发送消息。

我不介意能够将消息发送到也由红色瓷砖或网格墙绑定的瓷砖(如果只包括 1 面墙,而不是角落或整个空白空间)。我在这里将其显示为蓝色瓷砖。当红色瓷砖是对角线而不是相邻的(黄色形状)时,我也不介意尝试让它工作,但我不确定其中任何一个都会进入游戏。

这是一个简化,但我只需要一些关于去哪里的建议。我只需要知道我需要做的数学类型。

不确定是否有帮助,但游戏在 Objective-C 和 Cocos2D 中,每个图块都是子类对象,并且有一个属性来判断它是哪个状态以及在数组数组中。

这个帖子好像没有关联:surrounding objects algorithm

【问题讨论】:

  • 其实,这可能是我需要的,考虑一下:techuser.net/minecascade.html
  • 您实际上是在寻找(一种变体)轮廓跟踪算法,例如 Moore 的:imageprocessingplace.com/downloads_V3/root_downloads/tutorials/…跟踪轮廓,然后您就知道里面的瓷砖并可以检查它们的状态。跨度>
  • Steffen - 再次救援!我刚刚开始了一个类似的过程,我猜它是扫雷级联算法和摩尔轮廓算法之间的混合。我基本上采用起始瓷砖并构建一组瓷砖,这些瓷砖是与其相连的瓷砖。然后我遍历整个地图中的每个图块并检查它是否被标记的图块包围。
  • 哦,等等,这会成群结队地错过它们。好吧,我想我还是会弄明白的
  • 这听起来像flood fill,如果我没看错的话。给定一个红色图块,您想要一个仅通过其他红色图块即可到达的所有红色图块的列表,对吗?

标签: algorithm cocos2d-iphone nodes


【解决方案1】:

所以,这并不完美,但它确实有效。我不会详细介绍,但我使用了 learncocos2d / steffen 的方法,然后使用了我自己的边界检测器版本来尝试检测漏洞。它只在水平扫描中这样做。显然,代码包括对盒子数组的调用,我没有为此更改,但正如问题中所述,arrayofboxes 是一个数组数组,其中每个条目都是一排名为 Box 的图块:

我发布这个以防它对任何人都有帮助,而且我还不能 100% 确定没有错误

-(NSMutableDictionary*) returnMooreNeighborDictFromBox:(Box*)enteredBox {

NSMutableDictionary * mooreDictToReturn = [NSMutableDictionary dictionaryWithCapacity:8];

//define moore-neighbor as the 8 boxes around any given box. p1 is upper left

/*
 P1 P2 P3
 P8 XX P4
 P7 P6 P5
 */

int depth = enteredBox.trueDepth;
int depthRelativeXCoord = enteredBox.gridAbsoluteXCoord/((pow(2,depth)));
int depthRelativeYCoord = enteredBox.gridAbsoluteYCoord/((pow(2,depth)));
int xPosInArray = depthRelativeXCoord;
int yPosInArray = depthRelativeYCoord;

int maxCoord = (self.numberOfBoxesOfAcross/(pow(2,depth))-1);

NSMutableArray *boxArray = [arrayOfBoxArrays objectAtIndex:depth];

NSMutableArray *boxRowOfArray = [boxArray objectAtIndex:yPosInArray];
    if ((xPosInArray-1)>=0){
        Box * p8Box = [boxRowOfArray objectAtIndex:xPosInArray-1];
        [mooreDictToReturn setObject:p8Box forKey:@"p8"];
        }
    else {
        [mooreDictToReturn setObject:[NSNull new] forKey:@"p8"];
        }
    if ((xPosInArray+1)<=maxCoord){
        Box * p4Box = [boxRowOfArray objectAtIndex:xPosInArray+1];
        [mooreDictToReturn setObject:p4Box forKey:@"p4"];
        }
    else {
        [mooreDictToReturn setObject:[NSNull new] forKey:@"p4"];
        }

if ((yPosInArray-1)>=0){
    NSMutableArray *boxRowBelowArray = [boxArray objectAtIndex:yPosInArray-1];
    Box * p6Box = [boxRowBelowArray objectAtIndex:xPosInArray];
    [mooreDictToReturn setObject:p6Box forKey:@"p6"];
    if ((xPosInArray-1)>=0){
        Box * p7Box = [boxRowBelowArray objectAtIndex:xPosInArray-1];
        [mooreDictToReturn setObject:p7Box forKey:@"p7"];
        }
    else {
        [mooreDictToReturn setObject:[NSNull new] forKey:@"p7"];
        }
    if ((xPosInArray+1)<=maxCoord){
        Box * p5Box = [boxRowBelowArray objectAtIndex:xPosInArray+1];
        [mooreDictToReturn setObject:p5Box forKey:@"p5"];
        }
    else {
        [mooreDictToReturn setObject:[NSNull new] forKey:@"p5"];
        }
    }
else {
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p5"];
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p6"];
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p7"];        
    }


if ((yPosInArray+1)<=maxCoord){
    NSMutableArray *boxRowAboveArray = [boxArray objectAtIndex:yPosInArray+1];
    Box * p2Box = [boxRowAboveArray objectAtIndex:xPosInArray];
    [mooreDictToReturn setObject:p2Box forKey:@"p2"];
    if ((xPosInArray-1)>=0){
        Box * p1Box = [boxRowAboveArray objectAtIndex:xPosInArray-1];
        [mooreDictToReturn setObject:p1Box forKey:@"p1"];
        }
    else {
        [mooreDictToReturn setObject:[NSNull new] forKey:@"p1"];
        }
    if ((xPosInArray+1)<=maxCoord){
        Box * p3Box = [boxRowAboveArray objectAtIndex:xPosInArray+1];
        [mooreDictToReturn setObject:p3Box forKey:@"p3"];
        }
    else {
        [mooreDictToReturn setObject:[NSNull new] forKey:@"p3"];
        }
    }
else {
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p1"];
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p2"];
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p3"];
    }

return mooreDictToReturn;

}

-(NSString*) stepClockwiseMooreItem:(NSString*) currentMooreItem {

CCLOG(@"STEPCLOC");

//go through the 8-moore clockwise starting at P6 until hit. add hit to array. mark "entry box" as last box that was inactive before hit
//go to hit box and go through 8 starting with last hit box (if p3 was prior hit then p2 was empty so go to p8 of new box. these are the pairs: if X hit then prior empty was Y: p1>p8 P2>p1, P3>p2, P4>p3 etc.

NSString *returnString;

if ([currentMooreItem isEqualToString:@"p1"]){returnString= [NSString stringWithFormat:@"p2"];}
else if ([currentMooreItem isEqualToString:@"p2"]){returnString= [NSString stringWithFormat:@"p3"];}
else if ([currentMooreItem isEqualToString:@"p3"]){returnString= [NSString stringWithFormat:@"p4"];}
else if ([currentMooreItem isEqualToString:@"p4"]){returnString= [NSString stringWithFormat:@"p5"];}
else if ([currentMooreItem isEqualToString:@"p5"]){returnString= [NSString stringWithFormat:@"p6"];}
else if ([currentMooreItem isEqualToString:@"p6"]){returnString= [NSString stringWithFormat:@"p7"];}
else if ([currentMooreItem isEqualToString:@"p7"]){returnString= [NSString stringWithFormat:@"p8"];}
else if ([currentMooreItem isEqualToString:@"p8"]){returnString= [NSString stringWithFormat:@"p1"];}

return returnString;

}

-(NSString*) stepCounterClockMooreItem:(NSString*) currentMooreItem {

//go through the 8-moore clockwise starting at P6 until hit. add hit to array. mark "entry box" as last box that was inactive before hit
//go to hit box and go through 8 starting with last hit box (if p3 was prior hit then p2 was empty so go to p8 of new box. these are the pairs: if X hit then prior empty was Y: p1>p8 P2>p1, P3>p2, P4>p3 etc.

NSString *returnString;

if ([currentMooreItem isEqualToString:@"p1"]){returnString= [NSString stringWithFormat:@"p8"];}
else if ([currentMooreItem isEqualToString:@"p2"]){returnString= [NSString stringWithFormat:@"p1"];}
else if ([currentMooreItem isEqualToString:@"p3"]){returnString= [NSString stringWithFormat:@"p2"];}
else if ([currentMooreItem isEqualToString:@"p4"]){returnString= [NSString stringWithFormat:@"p3"];}
else if ([currentMooreItem isEqualToString:@"p5"]){returnString= [NSString stringWithFormat:@"p4"];}
else if ([currentMooreItem isEqualToString:@"p6"]){returnString= [NSString stringWithFormat:@"p5"];}
else if ([currentMooreItem isEqualToString:@"p7"]){returnString= [NSString stringWithFormat:@"p6"];}
else if ([currentMooreItem isEqualToString:@"p8"]){returnString= [NSString stringWithFormat:@"p7"];}

return returnString;
}

-(NSString*) stepMooreBacktrackAfterHitAt:(NSString*) currentMooreItem {

// with the moore, you can only back track adjacenetly, not diagnally, so its not straightforward counterclockwise walk

NSString *returnString;

if ([currentMooreItem isEqualToString:@"p1"]){returnString= [NSString stringWithFormat:@"p6"];}
else if ([currentMooreItem isEqualToString:@"p2"]){returnString= [NSString stringWithFormat:@"p8"];}
else if ([currentMooreItem isEqualToString:@"p3"]){returnString= [NSString stringWithFormat:@"p8"];}
else if ([currentMooreItem isEqualToString:@"p4"]){returnString= [NSString stringWithFormat:@"p2"];}
else if ([currentMooreItem isEqualToString:@"p5"]){returnString= [NSString stringWithFormat:@"p2"];}
else if ([currentMooreItem isEqualToString:@"p6"]){returnString= [NSString stringWithFormat:@"p4"];}
else if ([currentMooreItem isEqualToString:@"p7"]){returnString= [NSString stringWithFormat:@"p4"];}
else if ([currentMooreItem isEqualToString:@"p8"]){returnString= [NSString stringWithFormat:@"p6"];}

return returnString;

}

-(void) boxAreaCollapse:(float)boxCenterGridCoordX andY:(float)boxCenterGridCoordY atDepth:(int)depth{

//ok, to do this, we'll use moore neighbor tracing
//might be able to capture edges by add a row above and below and to either side of grid. each of these has a unique identity so if end wall included or something but this is later

//set array of boxes to empty
NSMutableArray * boxesOnEdgeOfShapeContainingTouchedBox = [NSMutableArray arrayWithCapacity:( (self.numberOfBoxesOfAcross/(pow(2,depth)))*(self.numberOfBoxesOfAcross/(pow(2,depth))) )];

//get array of array for this "depth"
NSMutableArray *boxArray = [arrayOfBoxArrays objectAtIndex:depth];

//get the box that was touched
NSMutableArray * boxRowSameAsTouchedBox = [boxArray objectAtIndex:boxCenterGridCoordY];
Box * boxTouched = [boxRowSameAsTouchedBox objectAtIndex:boxCenterGridCoordX];

CCLOG(@"boxTouched %i,%i",boxTouched.gridAbsoluteXCoord,boxTouched.gridAbsoluteYCoord);

//define boundaries > dont need to do this
//bottom left coord is (0,0), top right is (maxCoord,maxCoord)
//float maxCoord = (self.numberOfBoxesOfAcross/(pow(2,depth))-1); // the -1 because the first is "coord" is 0

//must start on bottom left so move all the way left until hit inactive then down
//move to the left through adjacenet boxes from touched best to find the furthest left connected directly to touch box
int numberOfBoxesLeft = boxCenterGridCoordX;
Box * furthestLeftBox = boxTouched;
for (int i = 1; i <= numberOfBoxesLeft; i++){
    Box * box = [boxRowSameAsTouchedBox objectAtIndex:boxCenterGridCoordX-i];
    if (box.boxState == kBoxActivated || box.boxState == kBoxClicked || box.boxState == kBoxLastDepthCompleted){
        furthestLeftBox = box;
        }
    else {
        break;
        }
    }

//move down from there to connected boxes below
int furthestLeftXCoord = (furthestLeftBox.gridAbsoluteXCoord / ((pow(2,depth))));
int furthestLeftYCoord = (furthestLeftBox.gridAbsoluteYCoord / ((pow(2,depth))));
int numberOfBoxesBelow = furthestLeftYCoord;

Box * furthestDownBox = furthestLeftBox;
for (int i = 1; i <= numberOfBoxesBelow; i++){
    NSMutableArray * boxRowBelow = [boxArray objectAtIndex:furthestLeftYCoord-i];
    Box * box = [boxRowBelow objectAtIndex:furthestLeftXCoord];
    if (box.boxState == kBoxActivated || box.boxState == kBoxClicked || box.boxState == kBoxLastDepthCompleted){
        furthestDownBox = box;
        }
    else {
        break;
        }
    }


//define starting Box and "entry box" for defining when to STOP this algorithim. we know p6 from the starting box should be empty and when we reenter the starting box from p6 we will define the "end"
Box * startingBoxFilled = furthestDownBox;
NSString * startingEnterPoint = @"p6";

//add start box to shape array > dont need to do this, it'll happen in first load of loop
//[boxesOnEdgeOfShapeContainingTouchedBox addObject:furthestDownBox];

//go through the 8-moore clockwise starting at P6 until hit. add hit to array. mark "entry box" as last box that was inactive before hit
//go to hit box and go through 8 starting with last hit box (if p3 was prior hit then p2 was empty so go to p8 of new box. these are the pairs: if X hit then prior empty was Y: p1>p8 P2>p1, P3>p2, P4>p3 etc.


//define first hitBox and entry point for backtrack
Box * hitBox =furthestDownBox;
NSString * hitBoxEnteredFrom = @"p6";

//CCLOG(@"fursthest down box %i,%i: %@, ",hitBox.gridAbsoluteXCoord, hitBox.gridAbsoluteYCoord,hitBox);

BOOL algorithimRunning = YES;
BOOL singleCircleRunning;

while (algorithimRunning == YES) {
    //CCLOG(@"starting loop for box relX %i, relY%i", hitBox.gridAbsoluteXCoord, hitBox.gridAbsoluteYCoord);

    NSMutableDictionary * mooreDictForBox = [self returnMooreNeighborDictFromBox:hitBox];
    singleCircleRunning = YES;

    NSString * circleStartingChecker = hitBoxEnteredFrom;

    while (singleCircleRunning == YES){

        //CCLOG(@"inner circle loop from box relX %i, relY%i checking  %@ ", hitBox.gridAbsoluteXCoord, hitBox.gridAbsoluteYCoord,hitBoxEnteredFrom);

        if ([hitBoxEnteredFrom isEqualToString:circleStartingChecker]){
            singleCircleRunning = NO;
            //CCLOG(@"loop circled around, ending inner loop");
            }

        if ([[mooreDictForBox objectForKey:hitBoxEnteredFrom] isKindOfClass:[Box class]]){
            Box * box = [mooreDictForBox objectForKey:hitBoxEnteredFrom];
            //CCLOG(@"moore object is box: state %i, depth %i, gridX %i, y %i", box.boxState, box.trueDepth, box.gridAbsoluteXCoord, box.gridAbsoluteYCoord);
            if (box.boxState == kBoxActivated || box.boxState == kBoxClicked || box.boxState == kBoxLastDepthCompleted){
                hitBox = box;
                [boxesOnEdgeOfShapeContainingTouchedBox addObject:box];
                NSString * newmooreChecker = [self stepMooreBacktrackAfterHitAt:hitBoxEnteredFrom];
                hitBoxEnteredFrom = newmooreChecker;
                singleCircleRunning = NO;
                //CCLOG(@"box is activated, etc, adjusting to %@ for backtrack ", hitBoxEnteredFrom);
                }
            else {
                NSString * newmooreChecker = [self stepClockwiseMooreItem:hitBoxEnteredFrom];
                hitBoxEnteredFrom = newmooreChecker;
                //CCLOG(@"box is not activated, etc, check next moore");
                }
            }
        else {
            //CCLOG(@"moore object is nil, check next moore object");
            NSString * newmooreChecker = [self stepClockwiseMooreItem:hitBoxEnteredFrom];
            hitBoxEnteredFrom = newmooreChecker;
            }
        }

    //repeat until you reach "starting down". if entered starting down from "entry" stop.
    if ([hitBox isEqual:startingBoxFilled] && [hitBoxEnteredFrom isEqualToString:startingEnterPoint]) {
        algorithimRunning = NO;
        //CCLOG(@"hitbox is starting box and entered from is same as start");
        }
    }


//create an array of dictionaries to represent "on" and "off" for the rows.
//first initialize the array of row arrays with NO in each pixel slot
int numberOfRows = (self.numberOfBoxesOfAcross/(pow(2,depth)));
NSMutableArray * arrayOfRowsForShape = [NSMutableArray arrayWithCapacity:numberOfRows];
for (int i= 0; i< numberOfRows; i++){
    NSMutableDictionary * rowDict = [NSMutableDictionary dictionaryWithCapacity:numberOfRows];
    for (int p = 0; p< numberOfRows; p++){
        [rowDict setObject:[NSString stringWithFormat:@"OUTSIDE"] forKey:[NSNumber numberWithInt:p]];
        }
    [arrayOfRowsForShape addObject:rowDict];
    }

//go through boxes in shape and change to YES if box located at that spot
for (Box * box in boxesOnEdgeOfShapeContainingTouchedBox) {
    NSMutableDictionary * rowDict = [arrayOfRowsForShape objectAtIndex:box.gridAbsoluteYCoord/(pow(2,depth))];
    [rowDict setObject:[NSString stringWithFormat:@"EDGE"] forKey:[NSNumber numberWithInt:box.gridAbsoluteXCoord/(pow(2,depth))]];
    }

//go through array of dict and for each one, go left to right and mark anything bound on both sides as "inside"
for (int locationY = 0; locationY<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationY++ ){
    NSMutableDictionary * rowDict = [arrayOfRowsForShape objectAtIndex:locationY];
    BOOL possiblyInsideContour = NO;
    BOOL edgeFound = NO;
    int holesFound = 0;
    for (int locationXin = 0; locationXin<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationXin++ ){

        NSNumber * locationX = [NSNumber numberWithInt:locationXin];
        NSMutableArray * markForChanging = [NSMutableArray arrayWithCapacity:(self.numberOfBoxesOfAcross/(pow(2,depth)))];
        NSString * thisLocation = [rowDict objectForKey:locationX];
        if ([thisLocation isEqualToString:@"EDGE"] && edgeFound==NO  && possiblyInsideContour==NO ){
            edgeFound=YES;
            }
        else if ([thisLocation isEqualToString:@"OUTSIDE"] && edgeFound==YES && possiblyInsideContour==NO){
            possiblyInsideContour=YES;
            [rowDict setObject:@"MAYBEINSIDE" forKey:locationX];
            [markForChanging addObject:[rowDict objectForKey:locationX]];
            }
        else if ([thisLocation isEqualToString:@"OUTSIDE"] && possiblyInsideContour==YES ){
            [rowDict setObject:@"MAYBEINSIDE" forKey:locationX];
            edgeFound=NO;
            }
        else if ([thisLocation isEqualToString:@"EDGE"] && possiblyInsideContour==YES ){
            possiblyInsideContour=NO;
            holesFound++;
            edgeFound=YES;
            }

        }

    int holesPatched = 0;
    BOOL patchingHole=NO;
    while (holesPatched < holesFound){
        for (int locationXin = 0; locationXin<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationXin++ ){
            NSNumber * locationX = [NSNumber numberWithInt:locationXin];
            NSString * thisLocation = [rowDict objectForKey:locationX];
            if ([thisLocation isEqualToString:@"MAYBEINSIDE"]){
                patchingHole = YES;
                 [rowDict setObject:@"INSIDE" forKey:locationX];
                }
            else if ([thisLocation isEqualToString:@"EDGE"] &&  patchingHole == YES){
                holesPatched++;
                patchingHole=NO;
                }
            }
        }

    }


int locationY=0;
for (NSMutableDictionary *rowDict in arrayOfRowsForShape){

    for (int locationXin = 0; locationXin<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationXin++ ){

        NSNumber * locationX = [NSNumber numberWithInt:locationXin];

        NSString * thisLocation = [rowDict objectForKey:locationX];
        if ([thisLocation isEqualToString:@"INSIDE"]){
            CCLOG(@"inside at %i,%i", [locationX intValue], locationY);
            }
        }
    locationY++;
    }










}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多