How to get a for loop to stop with break/return

• 07-23-2013, 03:43 AM
shaeway
How to get a for loop to stop with break/return
Hi! I had such success last time that instead of beating my head against my keyboard and yelling profanity at my chinchillas, I decided to see if anyone can guide me through this.

Ok. The project is: Based on an initial (x, y) position and initial velocity (vx, vy) of an object given by the user, calculate and draw the position of a falling object before it hits the ground.

So far I have everything working according to the rubric except my ball keeps being drawn below my ground. I've tried numerous things to end the for loop but am not having any luck. Here is what I have:

Code:

```public static void drawBall( int x, int y, int vx, int vy, Graphics g, Color c )   {   // time step calc     for( double i = 0; i < 100 ; i = i + .3)     {           // next X = Initial Position X + Initial Velocity X * Time Step     // next Y = Initial Position Y + Initial Velocity Y * Time Step + .5 * 9.8 * Time Step * Time Step           // calc x and y             double nextX = x + vx * i;       double nextY = y + vy * i + .5 * 9.8 * i * i;             //draw ball       g.setColor( c );       g.fillOval( (int)nextX, (int)nextY, 10, 10 );             //stops ball from going below horizon       double ballStop = ((double)HORIZON - 20.0);             if ( nextY > ballStop )       {         return;       }                 }```

HORIZON is a class constant of 705, and is the beginning of my ground on the drawing panel. I've tried using the equation, having HORIZON instead of ballStop in the if statement, subtracting, adding, casting everything as an int...I'm at a loss. Any guidance would help. Thanks!!
• 07-23-2013, 05:27 AM
DarrylBurke
Re: How to get a for loop to stop with break/return
http://www.java-forums.org/new-java/...tml#post346614

db
• 07-23-2013, 08:41 AM
gimbal2
Re: How to get a for loop to stop with break/return
Quote:

Originally Posted by shaeway
Hi! I had such success last time that instead of beating my head against my keyboard and yelling profanity at my chinchillas, I decided to see if anyone can guide me through this.

I do believe programming is a threat to your personal health. Perhaps you should consider seeking counseling to help you with your rage problems.
• 07-23-2013, 11:06 AM
JosAH
Re: How to get a for loop to stop with break/return

kind regards,

Jos
• 07-23-2013, 03:01 PM
shaeway
Re: How to get a for loop to stop with break/return
@ JosAH, the enchilada thing is actually quite common. Why I do not know, but you aren't the only one by any means. Thank you for having a sense of humor.
• 07-23-2013, 03:07 PM
shaeway
Re: How to get a for loop to stop with break/return
Quote:

Originally Posted by DarrylBurke
http://www.java-forums.org/new-java/...tml#post346614

db

Not quite sure where you get that idea, since I watched them like a hawk the entire time I was finishing my last project. I thought that this was similar to other forums and when a new issue arrived, and you couldn't find anything that really answered your question via searching, you simply asked your question and the members either gave advice or guided you to an already posted thread that you didn't find yourself. I was obviously mistaken.
• 07-23-2013, 03:14 PM
JosAH
Re: How to get a for loop to stop with break/return
You're drawing 'snapshots' of that ball w.r.t. a time 'i'. If nextY happens to be below ground level you still draw it ...

kind regards,

Jos
• 07-23-2013, 04:01 PM
DarrylBurke
Re: How to get a for loop to stop with break/return
Quote:

Originally Posted by shaeway
Not quite sure where you get that idea

If you had clicked the link I posted in this thread at #2 you would know exactly where I got that idea.

db
• 07-23-2013, 06:40 PM
jim829
Re: How to get a for loop to stop with break/return
Perhaps I can spell it out for you. Did you read the Forum guides and rules? Your code is still not posted within code tags.

Regards,
Jim
• 07-23-2013, 07:03 PM
shaeway
Re: How to get a for loop to stop with break/return
Ah, Jim you are correct, I did forget that. My apologies. Thank you for pointing that out.

And JosAH, that is exactly what I was not thinking about. Testing AFTER the ball was drawn was allowing it to proceed past the horizon. I needed to test the value before I drew the ball so that it would stop at the appropriate time.
• 07-23-2013, 07:13 PM
JosAH
Re: How to get a for loop to stop with break/return
Quote:

Originally Posted by shaeway
And JosAH, that is exactly what I was not thinking about. Testing AFTER the ball was drawn was allowing it to proceed past the horizon. I needed to test the value before I drew the ball so that it would stop at the appropriate time.

Don't stop drawing one step before because the ball would stop moving above the horizon.

kind regards,

Jos
• 07-23-2013, 07:26 PM
shaeway
Re: How to get a for loop to stop with break/return
I ended up setting my ballStop as HORIZON - 10, and my test remaining the same. My thinking behind that is anything beyond that point would draw the ball below the horizon level, since my ball has a diameter of 10. However, you bring up a very valid point.

I'm honestly not sure if we needed it to touch the ground or not. There isn't anything in the rubric other than making sure the ball does not go past the horizon. I will double check though and run through the program several times, seeing what I come up with. I may need to add an extra drawing command that draws the last ball at the set y-coordinate with a new calculated x-coordinate.
• 07-23-2013, 07:41 PM
shaeway
Re: How to get a for loop to stop with break/return
Code:

```// draws the ball in various positions   public static void drawBall( int x, int y, int vx, int vy, Graphics g, Color c )   {   // time step calc     for( double i = 0; i < 100 ; i = i + .3)     {           // next X = Initial Position X + Initial Velocity X * Time Step     // next Y = Initial Position Y + Initial Velocity Y * Time Step + .5 * 9.8 * Time Step * Time Step           // calc x and y             double nextX = x + vx * i;       double nextY = y + vy * i + .5 * 9.8 * i * i;             //stops ball from going below horizon       double ballStop = ((double)HORIZON - 10);             if ( nextY > ballStop )       {         g.setColor( c );         g.fillOval( (int)nextX, 695, 10, 10 );         return;       }             //draw ball       g.setColor( c );       g.fillOval( (int)nextX, (int)nextY, 10, 10 );           }```
This is what I came up with as my final and so far it's done what I needed it to do.
• 07-23-2013, 07:46 PM
JosAH
Re: How to get a for loop to stop with break/return
Note that the ball runs over a parabola shaped path; if your ball hits the horizon, the parabola hits the horizon and you should be able to find the x coordinate of that intersection point. Draw the ball at that location and you're in business. You only have to do the calculations if the ball would end up below the horizon (your original problem).

kind regards,

Jos