Ios 如何使用a-Star算法在自定义地图上的两个矩形块之间绘制路线?

Ios 如何使用a-Star算法在自定义地图上的两个矩形块之间绘制路线?,ios,iphone,objective-c,a-star,Ios,Iphone,Objective C,A Star,我对objective-C很陌生,所以这个任务对我来说很典型。我正在绘制矩形块的自定义地图,还需要在两个给定块之间绘制交互路线。一切都很好,但问题是使用A-Star算法绘制的路线呈对角线方向,看起来非常分散。我想要一条没有任何对角线的简单线条构成的平滑路线 -(void)findPath:(int)startX :(int)startY :(int)endX :(int)endY { int x,y; int newX,newY; int currentX

我对objective-C很陌生,所以这个任务对我来说很典型。我正在绘制矩形块的自定义地图,还需要在两个给定块之间绘制交互路线。一切都很好,但问题是使用A-Star算法绘制的路线呈对角线方向,看起来非常分散。我想要一条没有任何对角线的简单线条构成的平滑路线

-(void)findPath:(int)startX :(int)startY :(int)endX :(int)endY 
{       
    int x,y;
    int newX,newY;
    int currentX,currentY;
    NSMutableArray *openList, *closedList;

    if((startX == endX) && (startY == endY))
        return;

    openList = [NSMutableArray array];

    //BOOL animate = [shouldAnimateButton state];

    if(animate)
        pointerToOpenList = openList;

    closedList = [NSMutableArray array];
    PathFindNode *currentNode = nil;
    PathFindNode *aNode = nil;

    PathFindNode *startNode = [PathFindNode node];
    startNode->nodeX = startX;
    startNode->nodeY = startY;
    startNode->parentNode = nil;
    startNode->cost = 0;

    [openList addObject: startNode];

    while([openList count])
    {

        currentNode = [self lowestCostNodeInArray: openList];

        if((currentNode->nodeX == endX) && (currentNode->nodeY == endY))
        {

            //********** PATH FOUND ********************

            aNode = currentNode->parentNode;
            while(aNode->parentNode != nil)
            {
                tileMap[aNode->nodeX][aNode->nodeY] = TILE_MARKED;
                aNode = aNode->parentNode;
            }
            return;
            //*****************************************//
        }
        else
        {
            [closedList addObject: currentNode];
            [openList removeObject: currentNode];

            currentX = currentNode->nodeX;
            currentY = currentNode->nodeY;

            for(y=-1;y<=1;y++)
            {
                newY = currentY+y;
                for(x=-1;x<=1;x++)
                {
                    newX = currentX+x;
                    if(y || x) //avoid 0,0
                    {
                        if((newX>=0)&&(newY>=0)&&(newX<H)&&(newY<W))
                        {
                            if(![self nodeInArray: openList withX: newX Y:newY])
                            {
                                if(![self nodeInArray: closedList withX: newX Y:newY])
                                {
                                    if(![self spaceIsBlocked: newX :newY])
                                    {
                                        //the 'cost':
                                        aNode = [PathFindNode node];
                                        aNode->nodeX = newX;
                                        aNode->nodeY = newY;
                                        aNode->parentNode = currentNode;
                                        aNode->cost = currentNode->cost + 1;

                                        //distance, added to the existing cost
                                        aNode->cost += (abs((newX) - endX) + abs((newY) - endY));

                                        //aNode->cost += MAX(abs(newX - endX), abs(newY - endY));

                                        //aNode->cost += sqrt(pow(newX - endX, 2) + pow(newY - endY, 2));

                                        NSLog(@"Path: %d",aNode->cost);

                                        [openList addObject: aNode];
                                        // if(animate) // animation
                                        // [self setNeedsDisplay];

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    //**** NO PATH FOUND *****
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"OOPs!!" message:@"Couldn't find a path, Please try for another nearest Exhibitor." delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil, nil];
    [alert show];

}
-(void)findPath:(int)startX:(int)startY:(int)endX:(int)endY
{       
int x,y;
int newX,newY;
int currentX,currentY;
NSMutableArray*openList、*closedList;
如果((startX==endX)和&(startY==endY))
返回;
openList=[NSMutableArray];
//BOOL animate=[应设置动画按钮状态];
如果(动画)
pointerToOpenList=openList;
closedList=[NSMutableArray];
PathFindNode*currentNode=nil;
PathFindNode*阳极=零;
PathFindNode*startNode=[PathFindNode节点];
startNode->nodeX=startX;
startNode->nodeY=startY;
startNode->parentNode=nil;
开始节点->成本=0;
[openList addObject:startNode];
而([打开列表计数])
{
currentNode=[自低成本NodeInArray:openList];
if((currentNode->nodeX==endX)和&(currentNode->nodeY==endY))
{
//**********找到的路径********************
阳极=当前节点->父节点;
while(阳极->父节点!=nil)
{
tileMap[阳极->节点][阳极->节点]=瓷砖标记;
阳极=阳极->父节点;
}
返回;
//*****************************************//
}
其他的
{
[closedList addObject:currentNode];
[openList removeObject:currentNode];
currentX=currentNode->nodeX;
currentY=currentNode->nodeY;
对于(y=-1;y=0)和&(newXnodeY=newY;
阳极->父节点=当前节点;
阳极->成本=当前节点->成本+1;
//距离,添加到现有成本中
阳极->成本+=(abs((newX)-endX)+abs((newY)-endY));
//阳极->成本+=最大值(abs(newX-endX),abs(newY-endY));
//阳极->成本+=sqrt(功率(newX-endX,2)+功率(newY-endY,2));
NSLog(@“路径:%d”,阳极->成本);
[openList addObject:阳极];
//if(animate)//动画
//[自我设置需要显示];
}
}
}
}
}
}
}
}
}
//****找不到路径*****
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“OOPs!!”消息:@“找不到路径,请尝试另一个最近的参展商。”代表:自取消按钮:@“完成”其他按钮:无,无];
[警报显示];
}
最后,我运行一个循环来检查tileMap[][],标记为TILE_marked的节点数组tileMap[x][y]是要以蓝色路径绘制的节点。 我附上下面的图片,有人能帮我吗

提前感谢,祝你度过愉快的一天。 -阿西姆


Yeah@josh,我已经用代码更新了上面的内容,实际上我使用的是简单的a-star算法,没有太多修改。我已经用清晰的颜色将地板转换成小网格,只有绘制的蓝色网格点(路径)可见。