We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Program to find the shortest path
Page Index Toggle Pages: 1
Program to find the shortest path (Read 2620 times)
Program to find the shortest path
Jun 17th, 2010, 9:30pm
 
Hello, I'm new to processing, but have been using arduino for awhile now, but I can't understand what is wrong with this program.

the program is supposed to take a goal position, store it as 0, and take each position around it one number higher. like...
2123456
1012345
2123456
3234567
it stores obsticles as -2 and the unmapped areas as -1
heres the part of the code that has the problem.

void plot() {
 for (int i = 0; i < col; i++) {
   for (int j = 0; j < row; j++) {
     ways[i][j] = -1;
   }
 }
 refresh();
 ways[gx] [gy] = 0;
 for (int z = 0; ways[ox] [oy] == -1; z++) {
   for (int i = 0; i < col; i++) {
     for (int j = 0; j < row; j++) {
       if (ways[i] [j] == z) {
         if (ways[i-1] [j] == -1) {
           ways[i-1] [j] = z+1;
         }
         if (ways[i+1] [j] == -1) {
           ways[i+1] [j] = z+1;
         }
         if (ways[i] [j-1] == -1) {
           ways[i] [j-1] = z+1;
         }
         if (ways[i] [j+1] == -1) {
           ways[i] [j+1] = z+1;
         }
       }
     }
   }
 }
}


and the error...

Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 20
     at plotter.plot(plotter.java:61)
     at plotter.mouseReleased(plotter.java:44)
     at processing.core.PApplet.handleMouseEvent(PApplet.java:1611)
     at processing.core.PApplet.dequeueMouseEvents(PApplet.java:1544)
     at processing.core.PApplet.handleDraw(PApplet.java:1436)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:619)
Re: Program to find the shortest path
Reply #1 - Jun 17th, 2010, 10:50pm
 
Hard to tell from your snippet (we don't have array sizes, where ox/oy come, etc.) but I guess the (classical) error comes from ways[i+1] [j] for example (and might fail on ways[i-1] [j] too).
Beware of bounds!
Re: Program to find the shortest path
Reply #2 - Jun 18th, 2010, 12:50pm
 
ox and oy is the x and y cordinates of the object that needs to move to the goal.  gx and gy is the x and y position of the goal.  thanks for the out of bounds comment.  I'll try that.
Page Index Toggle Pages: 1