# Problems with the Diamond-Square algorithm

• September 1st, 2013, 02:48 PM
Whiteclaws
Problems with the Diamond-Square algorithm
Hi everyone ,

My name is Whiteclaws , and I'm new here , this my fifth try at the Diamond-Square algorithm , and I feel like it's time to ask for some help , p.s; I started learning Java yesterday , so take it easy on me ( the last 4 tries were on a drag n' drop engine )

I know what is the problem , but I don't know how to fix it !

Basically , this ;

Code java:

```package Array.Generation.DiamondSquare;   public class DiamondSquare {   public static void main(String []args){     // Size 2n+1 int n = 8 ;   //DO NOT CHANGE int Size = 2^n+1 ;   //Creating Basic Array (l*l) {L} double[][] Array = new double[Size][Size];   //Initialise array   /* SEED   tl tr * * * * * bl br   */   double tl = 0.5 ; double bl = 0.2 ; double tr = 0.8 ; double br = 0.4 ;   // Putting Values (X , Y) Array[0][0] = tl ; Array[0][Size-1]= bl ; Array[Size-1][0]= tr ; Array[Size-1][Size-1]= br ;     int Space = Size ;   //Start of the loop while(Space > 1) { Space = Space/2 ;   for(int y = 0 ; y != (Size/Space)-1 ; y++) { for(int x = 0 ; x != (Size/Space)-1 ; x++) {   //Square   Array[x+Space][y+Space] = Array[x][y]+ // Top Left Array[x+Space*2][y]+ // Top Right Array[x][y+Space*2]+ // Bottom Left Array[x+Space*2][y+Space*2]/4 ; // Bottom Right   //Diamond       /* Left + o | - + | Sum all the + and divide by 3 to get - ! + o | */   Array[x][y+Space]= Array[x][y]+ Array[x+Space][y+Space]+ Array[x][y+Space*2]/3;   /* Right o + | + - | Sum all the + and divide by 3 to get - ! o + | */   Array[x+Space*2][y+Space]= Array[x+Space*2][y]+ Array[x+Space][y+Space]+ Array[x+Space*2][y+Space*2]/3;     /* Up + - + | + | Sum all the + and divide by 3 to get - ! o o | */   Array[x+Space][y]= Array[x+Space*2][y]+ Array[x+Space][y+Space]+ Array[x][y]/3;     /* Down o o | + | Sum all the + and divide by 3 to get - ! + - + | */   Array[x+Space][y+Space*2]= Array[x+Space*2][y+Space*2]+ Array[x+Space][y+Space]+ Array[x][y+Space*2]/3;   } }   }   }   }```

Is my last try , and it's working for 2 , but otherwise , it's throwing an error of outbound

Any ideas on how to fix this

Cheers !
• September 1st, 2013, 02:56 PM
GregBrannon
Re: Problems with the Diamond-Square algorithm
You say you know what the problem is, so please describe what you think the problem is and any guesses you have about how to fix it.

Please post your errors and copies of runs that result in errors. Just copy from your terminal and paste between code or quote tags as you did above.

Edit:

These statements are suspect:
Code :

``` for(int y = 0 ; y != (Size/Space)-1 ; y++) { for(int x = 0 ; x != (Size/Space)-1 ; x++) {```
It's more typical (and safer) to provide an upper limit for the loop control variable when it is increasing in value using either '<' or '<='.

Edit 2:

Also, what is the "Diamond-Square" algorithm. It may be a common term in your world, but it's not in mine.
• September 1st, 2013, 03:34 PM
jps
Re: Problems with the Diamond-Square algorithm
Welcome to the forum
Please see the Announcements page for the use of code tags
• September 1st, 2013, 05:33 PM
Whiteclaws
Re: Problems with the Diamond-Square algorithm
Thank you jps !

the array is going out of bounds and causing an error to pop up , because the center is sometimes badly placed

I don't know much on how to fix that ;

There's multiple articles

Basically

http://hiko-seijuro.developpez.com/a...ges/square.png

http://hiko-seijuro.developpez.com/a...es/diamond.png

Cheers !

Edit :

My bad , totally forgot about the reason of the problem :o

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
at Array.Generation.DiamondSquare.DiamondSquare.main( DiamondSquare.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main (AppMain.java:120)

Edit2 :

Oh , I made a small error , I suspected that , it shouldn't be -1 but -2

I'll keep you tuned in with my progress on this one
• September 1st, 2013, 05:42 PM
jps
Re: Problems with the Diamond-Square algorithm
The Arrays tutorial should help discover the cause