001/*
002 * Renderer Models. The MIT License.
003 * Copyright (c) 2022 rlkraft@pnw.edu
004 * See LICENSE for details.
005*/
006
007package renderer.models_L.turtlegraphics;
008
009import renderer.scene.Model;
010
011/**
012   https://www.clear.rice.edu/comp360/lectures/fall2008/TurtleFractalsL2New.pdf#page=5
013*/
014public class SierpinskiTurtle extends Turtle
015{
016   /**
017      @param model   a reference to the {@link Model} that this {@code Turtle} is builing
018      @param n       number of levels for the Sierpinski triangle
019      @param length  side length
020   */
021   public SierpinskiTurtle(final Model model, final int n, final double length)
022   {
023      this(model, n, length, 0.0, 0.0, 0.0);
024   }
025
026
027   /**
028      @param model   a reference to the {@link Model} that this {@code Turtle} is builing
029      @param n       number of levels for the Sierpinski triangle
030      @param length  side length
031      @param xPos    the intial x-coordinate for this {@link Turtle}
032      @param yPos    the intial y-coordinate for this {@link Turtle}
033   */
034   public SierpinskiTurtle(final Model model, final int n, final double length,
035                           final double xPos, final double yPos)
036   {
037      this(model, n, length, xPos, yPos, 0.0);
038   }
039
040
041   /**
042      @param model   a reference to the {@link Model} that this {@code Turtle} is builing
043      @param n       number of levels for the Sierpinski triangle
044      @param length  side length
045      @param xPos    the intial x-coordinate for this {@link Turtle}
046      @param yPos    the intial y-coordinate for this {@link Turtle}
047      @param z       the z-plane for this {@code Turtle}
048   */
049   public SierpinskiTurtle(final Model model, final int n, final double length,
050                           final double xPos, final double yPos, final double z)
051   {
052      super(model, xPos, yPos, z);
053      sierpinski(n, length);
054   }
055
056
057   private void sierpinski(final int level, final double length)
058   {
059      if (0 == level)
060      {
061         // draw an equlateral triangle
062         for (int i = 0; i < 3; ++i)
063         {
064            forward(length);
065            turn(120);
066         }
067      }
068      else
069      {
070         for (int i = 0; i < 3; ++i)
071         {
072            resize(0.5);
073            sierpinski(level - 1, length);
074            resize(2.0);
075            move(length);
076            turn(120);
077         }
078      }
079   }
080
081}//SierpinskiTurtle