97WaterPolo
97WaterPolo

Reputation: 1

Use loops to create a pyramid in Java

I am trying to create a hollow pyramid of sorts using a loop. The way I would like to approach this is to start at a certain Y, and decrease it by 1, while expanding the X and the Z out by one while covering the perimeter.

    *
   * *
  *   *
 *     *
*       *

Granted this would be 3D, using XYZ.

So my main question is, if I have 3 points, X=50 Y=50 Z=50, how would I go down each level of Y while getting the points around the center?

So far

for (int y = 1; y<15; y++) {
    l.setY(l.getY() - 1);
    l.setX(l.getX() + 1);
    l.setZ(l.getZ() + 1);
    l.getBlock().setType(Material.GLASS);
}

will only generate a staircase of sorts going 15 blocks down from the starting position

Upvotes: 0

Views: 509

Answers (2)

LMG
LMG

Reputation: 976

Please imagine the graph of the 3 axis, if you are not practice with it just take a look at this graph.

x y z axis

We will use z as height, by rotating the axis in order to have a clear picture of what is happening. You may as well use z as the depth parameter but it will be harder for you to understand what is going on. You obtain a wrong plot because you are not moving towards the space in the right way..

What you are looking for is starting by a point, say S=(x,y,z) = (5,5,5) and you want to go down each level by printing your edges for the piramid. the starting point is plotted as shown in the following figure enter image description here

By moving down decrementing z you want to draw 4 points (you may want to draw some more, I just draw the edges in this case). so in the next cycle you have z = z-1; thus you have to move in the 4 directions allowed by the graph. enter image description here

The point in the middle keeps the center of the pyramid, while the others are respectively

(5,4,4) (5,6,4) --> moving along y axis

(4,5,4) (6,5,4) --> moving along x axis

while you cycle you keep moving this, by using as metrics the difference between the center, the level(height) and the axis while drawing the pyramid..

HOW? see the last following picture, I added some color to help you keeping track of what is happening. Further I added axis and main points coordinates. enter image description here Red points keep the center of the pyramid, by starting at S = (5,5,5) (which is the top) you go down to S1 =(5,5,4) and S2 = (5,5,3).. so imagine this are 2 cicles of your while loop. Green points keep track of the first cycle, when z = 4, the center of the pyramid is S1 this time. You want to expand in the space by moving on the y and x axis.

By the center you move -1 and -+1 on y.. so obtaining (5,4,4) and (5,6,4). So far you obtain 2 edges. Let's move on the x axis keeping the center as is, the same this time you move by 1 unit toward such plane so that you have (6,5,4) and (4,5,4).. The same happens with the second cycle. The distance you move is obtained by

starting height - actual level height

in the case of z = 3 you have to from the center by 5 - 3 = 2 unit (by previous statement) so on and so forth for the rest of the cycle up to the point you end up. You may want to stop at 0 unit of shift from center (when starting height == actual level height) by obtaining a nice and well drawn pyramid, other wise you can keep cycling but you have to find another way to stop, the previous subtraction in fact works also with negative height, you can notice that if you reach z = -1 the previous will tell you to shift of 6 units, in this case you have to find another way to stop and break your cycle.

This said you have to adjust your cycle to draw the 4 points, instead of those that you actually draw =)

Upvotes: 3

Patricia
Patricia

Reputation: 2865

You go the staircase at only one side down. On the other side, x and z grow in the other direction.

Upvotes: 0

Related Questions