Turtle Java squares Pyramid

Viewed 60

I have to make a pyramid that can have its parameters changed, but my code keeps not allowing the next row after to start at the proper point, I need help! I'm sorry if its messy, I'm very new. picture at bottom is result. I need it to be centered.

public class Pyramid2 {
    public static void main(String[] args) {
        Turtle t = new Turtle();
        t.delay(100);
        pyramid(t, 200, 5);
    }

    public static void square(Turtle t, double base, int levels) {
        for (int i = 0; i < 4; i++) {
            t.forward(base / ((levels * 2) - 1));
            t.left(90);
        }
    }

    public static void pyramid(Turtle t, double base, int levels) {
        //for (int p = 1; (p-1)< levels; p++){
        for (int j = 0; j < levels; j++) {
            for (int i = 0; i < ((levels) * 2) - (j * 2) - 1; i++) {
                square(t, base, levels);
                t.forward(base / ((levels * 2) - 1));
            }

            t.penup();
            t.left(90);
            t.forward(base / ((levels * 2) - 1));
            t.right(90);

            t.backward(base - (base / (((levels * 2) - (j * 2)) - 1)));

            t.pendown();
        }
    }
}

what I get

1 Answers

I couldn't figure out how to set up a turtle Java environment (link to a guide in the comments would be appreciated!), but the basic algorithm is pretty much language-agnostic. I'll use JavaScript with var so it should be compatible with Java 10+.

I'm not sure if your turtle has goto, but if it does, that makes rewinding to the left side of the canvas to prepare the next row easier. I'll assume you don't have this and only use forward, right, pendown and penup commands.

My approach is this:

  • Set size and levels constants which represent the box/grid size and number of levels, respectively. (If you want to use base, fine--compute size = base / (levels * 2 - 1) as you've already done, but save it in a variable).
  • Start from the top level
  • For each level i:
    • Compute the number of boxes for the level: boxes = i * 2 + 1
    • Compute the left x-offset to start drawing boxes from: offset = (levels - i - 1) * size
    • Move forward offset pixels
    • Draw the boxes for the level
    • Move to the beginning of the next line and point rightward (goto(0, turtle.y + size) would be handy here to avoid some maneuvering)

Here's the code:

var size = 20;
var levels = 5;
var t = new Turtle();
t.penup();

for (var i = 0; i < levels; i++) {
  var boxes = i * 2 + 1;
  var offset = (levels - i - 1) * size;
  t.forward(offset);
  t.pendown();

  for (var j = 0; j < boxes; j++) {
    for (var k = 0; k < 4; k++) {
      t.forward(size);
      t.right(90);
    }

    t.forward(size);
  }

  t.penup();
  t.right(90);
  t.forward(size);
  t.right(90);
  t.forward(offset + size * boxes);
  t.right(180);
}








///////////////////////////////////////
// turtle library; ignore code below //
function Turtle() {
  const rad = d => d * Math.PI / 180;
  const canvas = document.querySelector("#turtle-pond");
  canvas.width = innerWidth;
  canvas.height = innerHeight;
  const ctx = canvas.getContext("2d");
  ctx.translate(0.5, 0.5);
  let x = 0, y = 0, angle = 0;
  let penDown = true;
  return {
    pendown() {
      penDown = true;
    },
    penup() {
      penDown = false;
    },
    forward(dist) {
      if (penDown) {
        ctx.beginPath();
        ctx.moveTo(x, y);
      }

      x += Math.cos(rad(angle)) * dist;
      y += Math.sin(rad(angle)) * dist;

      if (penDown) {
        ctx.lineTo(x, y);
        ctx.stroke();
      }
    },
    right(ang) {
      angle += ang;
    },
    left(ang) {
      angle -= ang;
    },
  };
}
<canvas id="turtle-pond"></canvas>

Related