Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Processing/Java Loop Issuse

Options
  • 06-11-2006 7:14pm
    #1
    Registered Users Posts: 1,987 ✭✭✭


    I have the below code, the loop goes on forever, i want its just to run 100 times but it goes onto infinity! Can't seem to see why!?
    float x;
    float y;
    
    void setup()
    {
      size(800,600);
      background(0,0,0);
    }
    
    void draw()
    {
      translate(width/2, height/2);
      stars();
    }
    
    void stars()
    {
       fill(255,255,255);
       stroke(100);
       x = random(-800, 800);
       y = random(-600, 600);
       for(int i=0;i<100;i++)
       {
         line(x,y,x,y);
       }
    }
    


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    At a guess, i'd say calling: line(x,y,x,y); ends up calling "Draw" which in turn calls "Star" which in turn calls line(x,y,x,y); again. Thus an infinite loop.

    Of course, there's nowhere near enough code to see the problem. Also, you're drawing the same line with the same XY coordinates 100 times in that loop.


  • Registered Users Posts: 1,375 ✭✭✭DoesNotCompute


    Yeah, shouldn't the lines:

    x = random(-800, 800);
    y = random(-600, 600);

    be in the for loop?


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    Of course, there's nowhere near enough code to see the problem. Also, you're drawing the same line with the same XY coordinates 100 times in that loop.
    Thats all the code there is in the prog. so far! Also they xy cords are ment to be the same.
    Yeah, shouldn't the lines:
    
    x = random(-800, 800);
    y = random(-600, 600);
    
    be in the for loop?
    
    Does'nt make any diff!


  • Registered Users Posts: 1,636 ✭✭✭henbane


    If that's all the code there is, I don't think the for loop is the problem. What are you trying to achieve and is there a standard package of some kind that you're using with this code?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    This is where you get to learn the wonderful art of debugging. Before each line in the stars() method, tell it to output what it is doing, so you can keep track.

    E.g.
    void stars()
    {
       System.out.println("Doing fill....");
       fill(255,255,255);
       System.out.println("Doing stroke...");
       stroke(100);
       System.out.println("Setting X & Y Vars....");
       x = random(-800, 800);
       y = random(-600, 600);
       System.out.println("Entering loop....");
       for(int i=0;i<100;i++)
       {
           System.out.println("Iteration " + i + " ....");
         line(x,y,x,y);
       }
    }
    


  • Advertisement
  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    So if that's *all* the code, how the hell are you running it?


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    So if that's *all* the code, how the hell are you running it?

    Application called 'Processing', http://www.processing.org its a java based development enviroment for animation.


Advertisement