Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problems with the Diamond-Square algorithm

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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 ;

    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 !
    Last edited by jps; September 1st, 2013 at 02:35 PM. Reason: code tags


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default 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:
                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.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problems with the Diamond-Square algorithm

    Welcome to the forum
    Please see the Announcements page for the use of code tags

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 ;

    About the diamond-square algorithm

    There's multiple articles

    Basically






    Cheers !

    Edit :

    My bad , totally forgot about the reason of the problem

    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
    Last edited by Whiteclaws; September 1st, 2013 at 04:46 PM. Reason: Typo

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problems with the Diamond-Square algorithm

    The Arrays tutorial should help discover the cause

Similar Threads

  1. Help With Diamond Square Terrain
    By hobbles in forum Algorithms & Recursion
    Replies: 5
    Last Post: May 6th, 2013, 03:10 PM
  2. Replies: 10
    Last Post: April 21st, 2013, 09:28 AM
  3. Diamond perimeter with loop
    By Lanto in forum Loops & Control Statements
    Replies: 5
    Last Post: August 9th, 2012, 12:02 PM
  4. diamond pattern and loop
    By Naif in forum Loops & Control Statements
    Replies: 4
    Last Post: October 31st, 2011, 10:51 PM
  5. asterisk forming diamond
    By cutee_eyeh in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 13th, 2010, 08:53 PM