Skip to content

Instantly share code, notes, and snippets.

@hutchdoescoding
Created June 5, 2012 19:24
Show Gist options
  • Save hutchdoescoding/2877162 to your computer and use it in GitHub Desktop.
Save hutchdoescoding/2877162 to your computer and use it in GitHub Desktop.
This is my solution to the 'Pyramid' assignment for the Stanford CS106A lectures.
/* File name: Pyramid.java
* This program will display a pyramid centered within the screen.
*/
import acm.program.*;
import acm.graphics.GRect;
public class Pyramid extends GraphicsProgram {
public void run() {
// I realize I didn't have to use a separate method, and could have placed all the code
// in the run() method, but I figured this would be better in case I wanted to expand the program later on
DrawPyramid();
}
// Declared the constants for the Width, Height, and amount of starting bricks.
public static final int BRICK_WIDTH = 20;
public static final int BRICK_HEIGHT = 10;
public static final int BRICKS_IN_BASE = 8;
/* Create the method for drawing the Pyramid
* This first part of this method Gathers the Canvas Height and Width and then stores them in variables.
* It then calculates the center line of the canvas, and creates variables for where the first brick should be placed.
* I realize that the 'StartingPointY' variable is a little redundant, but it was for my benefit when implementing
* further down.
*/
private void DrawPyramid() {
int CanvasWidth = getWidth();
int CanvasHeight = getHeight();
int CanvasMiddle = CanvasWidth / 2;
int StartingPointX = CanvasMiddle - (BRICK_WIDTH * (BRICKS_IN_BASE / 2));
int StartingPointY = CanvasHeight;
//Created an outer for loop for drawing as many rows as are in the BRICKS_IN_BASE
for (int i = 1; i <= BRICKS_IN_BASE; i++) {
int Y = StartingPointY - (i * BRICK_HEIGHT);
//The inner for loop will keep drawing bricks for as many bricks as are supposed to be in that row
for (int j = BRICKS_IN_BASE; j >= i; j--) {
int X = StartingPointX - (BRICK_WIDTH / 2) + (j * BRICK_WIDTH) - (i * (BRICK_WIDTH / 2));
add (new GRect(X, Y, BRICK_WIDTH, BRICK_HEIGHT));
}
}
}
}
@dsch31
Copy link

dsch31 commented Aug 3, 2018

Nice work! Here is my solution, it's almost the same :)

/*
*

  • author: Atila Sabo
  • Aug.03.2018.
  • (my first solution)

https://see.stanford.edu/materials/icspmcs106a/13-assignment-2-simple-java.pdf

TASK 01

Write a GraphicsProgram subclass that draws a pyramid consisting of bricks
arranged in horizontal rows, so that the number of bricks in each row decreases by
one as you move up the pyramid

The pyramid should be centered at the bottom of the window and should use
constants for the following parameters:

BRICK_WIDTH The width of each brick (30 pixels)
BRICK_HEIGHT The height of each brick (12 pixels)
BRICKS_IN_BASE The number of bricks in the base (14)

The numbers in parentheses show the values for this diagram, but you must be able
to change those values in your program.

*/

package assigment2;

import acm.graphics.*;
import acm.program.GraphicsProgram;

public class BricksPyramid1 extends GraphicsProgram {

	// Pyramid has constants for the following parameters:


private static final int BRICK_WIDTH = 30;
private static final int BRICK_HEIGHT = 12;
private static final int BRICKS_IN_BASE = 14;


public void run () {
	
// Coordinates calculated to place the Pyramid in the center of the screen(x)
// and at the bottom (y)	
			
			
	int x = (getWidth () - (BRICK_WIDTH * BRICKS_IN_BASE)) / 2 ;	
	int y = getHeight () - BRICK_HEIGHT - 1;
	
	int z = BRICKS_IN_BASE; // int z is used in second loop to decrease number of bricks in 									every next row
	
									
	for (int a = 0; a < BRICKS_IN_BASE ; a++ ) {  
		
			for (int b = 0; b < z; b++ ){
		
		GRect brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
		add (brick);
		
		x += BRICK_WIDTH; // changes the position of nex brick in a row
		
		}
	  	
	//changes the coordinates in next upper row 
	
	    x -= BRICK_WIDTH * z - BRICK_WIDTH/2;  
	  	y -= BRICK_HEIGHT;
	  	
	  	z--; //reduces number of bricks in next row for 1
	  
	  	
	}

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment