1. ## Re: Draw a pyramid

Aaaah, I found my mistake, but I don't know how to solve it.
Here it is.
My program gives 14 coordinates for i = 0, another 14 coordinates for i = 1, etc.
How can I set my i for loop to run only ONCE for each j?
Can you help me with this?
I think if I do this, the pyramid will be ok.

2. ## Re: Draw a pyramid

Think in terms of what row you are printing on and what column, not i and j.
What is the x,y value for the first shape on the first row? How many shapes on this row?
What is the x,y value for the first shape on the second row? How many shapes on this row?
and so on for all the rows of shapes.

Are you starting at the bottom and working up? Your y values are decreasing. I was starting at the top.
The logic should be very similiar. For each row you need to compute the starting x value and the number of shapes to put on that row.
Last edited by Norm; 11-20-2011 at 04:24 PM.

3. ## Re: Draw a pyramid

You are right, Norm!
Thank you very much for your help.
I am satisfied even with the point that I have arrived to right now (beginner speaking). ;)

4. Member
Join Date
Apr 2012
Posts
2
Rep Power
0

## Re: Draw a pyramid

Hello I posted my code on another post but I just saw this one now so I thought I should repost it here.
I hope it helps someone out there
Java Code:
```import acm.graphics.*;
import acm.program.*;

public class Pyramid extends GraphicsProgram {

private static final int BRICK_WIDTH = 30;  /** Width of each brick in pixels */
private static final int BRICK_HEIGHT = 12;	/** Height of each brick in pixels */
private static final int BRICKS_IN_BASE = 14; /** Number of bricks in the base of the pyramid */

public void run() {
int height=getHeight();
int width=getWidth();

/** I want to manipulate the number of BRICKS_IN_NUMBER so I assign it to a variable double bricks*/
int bricks = BRICKS_IN_BASE;
/** the first loop adds the same number of rows we have set for the number of bricks
* the number can be changed by changing the BRICK_HEIGHT*/
for (int j=0; j<BRICKS_IN_BASE; j++){
/**Its the same as doing height=height-BRICK_HEIGHT*/
height-=BRICK_HEIGHT;
int initPos=(width-(BRICK_WIDTH*bricks))/2;//calculates the initial position of each row

for (int i=bricks; i>0; i--){
/** set the size of the brick*/
GRect brick = new GRect(BRICK_WIDTH,BRICK_HEIGHT);
/** set the location of the brick*/
brick.setLocation(initPos, height);
/** this sets the position of the next brick*/
initPos+=BRICK_WIDTH;
}
/**the subtraction of bricks must be executed after the first loop has placed the row of bricks otherwise the pyramid will not be centered*/
bricks--;

}
}
}```
Last edited by Leandros; 04-02-2012 at 05:29 PM.

Page 2 of 2 First 12

#### Posting Permissions

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