# Thread: Trying to stagger GRect like stairs... Math issue

1. Member
Join Date
Jan 2012
Posts
2
Rep Power
0

## Trying to stagger GRect like stairs... Math issue

I am working with the acm.jar library to help learn Java. I am working on an application for school that creates an IO Dialog asking users how many stairs they would like to draw.

Then I am supposed to use a loop to draw those stairs. The first half to be blue the second half to be red.

I am lost in the math of how to get the rectangles to stagger. I have tried several things. The code below includes my latest attempt which is still failing.

If there is anyone who can help with just the math part I would be eternally grateful:

Java Code:
```import acm.program.GraphicsProgram;
import acm.io.IODialog;
import acm.graphics.GRect;
//import acm.graphics.GLabel;
//import java.awt.Color;

public class StairsGraphical extends GraphicsProgram {

public static final int APP_WIDTH = 500;
public static final int APP_HEIGHT = 300;
public static final double BOX_WIDTH = 75;
public static final double BOX_HEIGHT = 25;

public void init()
{
//setting app size to display stairs
this.setSize(APP_WIDTH, APP_HEIGHT);

//asking user for number of stairs
IODialog dialog = new IODialog();
int stairsValue = dialog.readInt("Number of Stairs: between 5 and 10");

//GRect stairRect = new GRect (BOX_WIDTH, BOX_HEIGHT);
//for loop to display the stairs
double x = 1;
double y = 1;

for (int i = 0; i <= stairsValue; i++)
{

GRect stairRect = new GRect(x, y, BOX_WIDTH, BOX_HEIGHT);
System.out.println("count is " + i);
/*x = 0;
y = stairRect.getHeight()*2;*/
stairRect.move(BOX_WIDTH/2,BOX_HEIGHT);
}
}

}```
Last edited by pbrockway2; 01-12-2012 at 10:47 PM. Reason: code tags added

2. ## Re: Trying to stagger GRect like stairs... Math issue

I am lost in the math of how to get the rectangles to stagger
Take a piece of graph paper (with a grid) and draw what you want the program to draw.
Look at the x,y points for each rectangle. Make a list of those x,y points. There must be an easy equation that will allow you to compute the next x,y given the current x,y.

If you need help finding the pattern, list the x,y points for the rectangles here.

3. Member
Join Date
Jan 2012
Posts
2
Rep Power
0

## Re: Trying to stagger GRect like stairs... Math issue

Thanks for the thought!!! I figured it out. I used i to help change the location of each rectangle

x = BOX_WIDTH/2 * i;
y = BOX_HEIGHT*i;
stairRect.setLocation(x,y);

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•