gpt4 book ai didi

algorithm - 确定网格上的一个点是否被某种类型的点包围

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:08:23 28 4
gpt4 key购买 nike

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

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

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

不确定它是否需要帮助,但游戏是在 Objective-C 和 Cocos2D 中,每个图 block 都是子类对象,并且有一个属性来告诉它是哪个状态,并且在数组的数组中。

这篇文章似乎不相关:surrounding objects algorithm

enter image description here

最佳答案

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

我发布这个以防它对任何人有帮助,但我还不能 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++;
}










}

关于algorithm - 确定网格上的一个点是否被某种类型的点包围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18166288/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com