Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to draw a route between two rectangular Blocks on a custom map using A-Star Algorithm?
    text
    copied!<p>I am very new to objective-C, so the task seems typical for me. I am plotting a custom map of rectangular blocks, and also need to draw an interactive route between two given blocks. All the thing is fine, but the problem is that the route drawn using A-Star algorithm goes diagonally looks very scattered. I want a smooth route made by simple lines without any diagonal. </p> <pre><code>-(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) &amp;&amp; (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-&gt;nodeX = startX; startNode-&gt;nodeY = startY; startNode-&gt;parentNode = nil; startNode-&gt;cost = 0; [openList addObject: startNode]; while([openList count]) { currentNode = [self lowestCostNodeInArray: openList]; if((currentNode-&gt;nodeX == endX) &amp;&amp; (currentNode-&gt;nodeY == endY)) { //********** PATH FOUND ******************** aNode = currentNode-&gt;parentNode; while(aNode-&gt;parentNode != nil) { tileMap[aNode-&gt;nodeX][aNode-&gt;nodeY] = TILE_MARKED; aNode = aNode-&gt;parentNode; } return; //*****************************************// } else { [closedList addObject: currentNode]; [openList removeObject: currentNode]; currentX = currentNode-&gt;nodeX; currentY = currentNode-&gt;nodeY; for(y=-1;y&lt;=1;y++) { newY = currentY+y; for(x=-1;x&lt;=1;x++) { newX = currentX+x; if(y || x) //avoid 0,0 { if((newX&gt;=0)&amp;&amp;(newY&gt;=0)&amp;&amp;(newX&lt;H)&amp;&amp;(newY&lt;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-&gt;nodeX = newX; aNode-&gt;nodeY = newY; aNode-&gt;parentNode = currentNode; aNode-&gt;cost = currentNode-&gt;cost + 1; //distance, added to the existing cost aNode-&gt;cost += (abs((newX) - endX) + abs((newY) - endY)); //aNode-&gt;cost += MAX(abs(newX - endX), abs(newY - endY)); //aNode-&gt;cost += sqrt(pow(newX - endX, 2) + pow(newY - endY, 2)); NSLog(@"Path: %d",aNode-&gt;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]; } </code></pre> <p>Finally i am running a loop to check tileMap[][], and the node array tileMap[x][y] marked as TILE_MARKED is the nodes to be drawn in blue colored route. I am attaching the image below, can anyone kindly help me?</p> <p>Thanks in advance and have a great day. -asim</p> <p><img src="https://i.stack.imgur.com/Bawkq.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload