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 6 of 6

Thread: Fibonacci Spiral Help?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Fibonacci Spiral Help?

    I'm trying to write a Java program that will display the Fibonacci Spiral as shown here . For right now, I just have it printing the starting coordinates of the rectangle that's being drawn, along with its length. There's nothing wrong with my program, except for a little array problem that I can fix, but I think there is a fundamental flaw in my thinking for this program. Can someone take a look at my code below and see why it does not work?
    *Note: This is a school project, so I don't want too much help telling me what I should do, I just need help telling me what I shouldn't have done and why.
    Thanks!

    public class FibSpiralPanel extends JPanel{
     
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
     
    		int xCenter = getWidth()/2;
    		int yCenter = getHeight()/2;
    		int x=0, number, nextNumber = 0, count;
    		int[] xStart = {0,0,0,0,0,0,0,0}, yStart={0,0,0,0,0,0,0,0};
     
    		xStart[0] = xCenter;
    		yStart[0] = yCenter;
     
    		for(x=1; x<5; x++){
    			number = (int)Fibonacci.getFibonacci(x);  //Get the length of the square.
     
     
     
    			if(x%4 == 0){
    				xStart[x] = xStart[x-1] - number;
    				yStart[x] = yStart[x-1];
    			}
     
    			else if(x%3==0){
    				xStart[x] = xStart[x-3];
    				yStart[x] = yStart[x-3] - number;
    			}
     
    			else if(x%2 ==0){
    				xStart[x] = xStart[x-2] + number;
    				yStart[x] = yStart[x-2];
    			}
     
    			else{
    				xStart[x] = xStart[x-1];
    				yStart[x] = yStart[x-1] + number;
     
    			}
    			System.out.println("Fibonacci "+x+" is "+number);
    			System.out.println("xStart: "+xStart[x]+"\nyStart: "+yStart[x]);
    		}				
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,371
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Fibonacci Spiral Help?

    Inspect the way the code determines the location of a Fibonacci square (eg the uses of modulus). My understanding is that the squares are repetitive in fours, and so checking the remainder from dividing by 4 may be more appropriate. Also, I assume the getFibonacci method returns the value at position x in the series? Because that code isn't listed I'll just comment that this is in the loop of a drawing routine so it should be fast (precalculated)

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Fibonacci Spiral Help?

    Calculating Fibonacci can be fast... O(n) with O(1) memory space

    It might be easier to work with the corners rather than the centers because they are always aligned by at least one axis (either x or y), so there's no need to calculate offsets between different centers. Maybe stick to the top-left corner? Also, in your modulus section, you don't want to modulate with different numbers, but rather with 4 all the time and check to see what the remainder value is.

    for(int x = 1; x < 5; x++)
    {
         if (x % 4 == 0)
         { }
         else if (x % 4 == 1)
         { }
         else if (x % 4 == 2)
         { }
         else if (x % 4 == 3)
         { }
    }
    Last edited by helloworld922; January 12th, 2010 at 11:43 AM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,371
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Fibonacci Spiral Help?

    Quote Originally Posted by helloworld922 View Post
    Calculating Fibonacci can be fast... O(n) with O(1) memory space
    I dislike big O notation, mainly because I stink at calculating it ...but do that in a loop it becomes something like O(n*n) (I know I know, we're talking a loop of five here though )

    I'll second Helloworld's advice on working with corners, especially if you plan to do the drawing using g.drawArc(...)

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Fibonacci Spiral Help?

    public static int fib(int n)
    {
         if (n < 3)
         {
              return 1;
         }
         int num1 = 1, num2 = 1;
         for (int i = 3; i < n; i++)
         {
              int temp = num1 + num2;
              num1 = num2;
              num2 = temp;
         }
         return num2;
    }

    In a loop, still O(n) time. True dat it's only a loop of 5, but it's still nice to be efficient

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,371
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Fibonacci Spiral Help?

    Quote Originally Posted by helloworld922 View Post
    In a loop, still O(n) time. True dat it's only a loop of 5, but it's still nice to be efficient
    Sorry, I meant to calculate the series over and over again in a loop. For example (using your fib example function)

    int n = 5;
    for ( int i = 0; i < n; i++ ){
        int fib = fib(i);
    }
    As the loop goes through, it recalculates the previously calculated fibonacci values, doing so in a drawing routine which may be called often. Of course, for 5 its not a big deal, but larger values the computation time starts to pile up

    This is as opposed to previously computing the series up to n, then accessing these as needed

    /**
    *Returns an array containing the Fibonacci sequence up to n in the series
    */
    public static int[] fib(int n)
    {
         int fib[] = new int[n];
         if (n < 3)
         {
     
              fib[0] = 1;
              if ( n == 2 ) 
                 fib[1] = 1;
         }
         for (int i = 3; i < n; i++)
         {
              fib[i] = fib[i-1] + fib[i-2];
         }
         return fib;
    }
     
    int fib[] = fib(n);//even better to do this upon construction or whenever the value of n is set
    for ( int i = 0; i < n; i++ ){
        int num = fib[i];
         ///do the calculations and drawing
    }
    Last edited by copeg; January 12th, 2010 at 09:24 PM.

Similar Threads

  1. Replies: 1
    Last Post: October 20th, 2009, 12:53 AM
  2. [SOLVED] Problem in generating Fibonacci sequence in java
    By big_c in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 24th, 2009, 09:52 AM