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://en.wikipedia.org/wiki/Sierpi%C5%84ski_curve#Arrowhead_curve
013*/
014public class SierpinskiCurveTurtle 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 curve
019      @param length  side length
020   */
021   public SierpinskiCurveTurtle(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 curve
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 SierpinskiCurveTurtle(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 curve
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 SierpinskiCurveTurtle(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      curve(n, length, 60);
054   }
055
056
057   private void curve(final int n, final double length, final int angle)
058   {
059      if ( 0 == n )
060      {
061         forward(length);
062      }
063      else
064      {
065         turn(angle);
066         curve(n - 1, length / 2.0, -angle);
067         turn(-angle);
068         curve(n - 1, length / 2.0,  angle);
069         turn(-angle);
070         curve(n - 1, length / 2.0, -angle);
071         turn(angle);
072      }
073   }
074
075}//SierpinskiCurveTurtle
076
077
078/* Wikipedia version.
079   public SierpinskiCurveTurtle(final Model model, final int n, final double length,
080                                final double xPos, final double yPos, final double z)
081   {
082      super(model, xPos, yPos, z);
083      if ( 0 == n % 2 )  // n is even
084      {
085         curve(n, length, 60);
086      }
087      else  // n is odd
088      {
089         turn(60);
090         curve(n, length, -60);
091      }
092   }
093
094   private void curve(final int n, final double length, final int angle)
095   {
096      if ( 0 == n )
097      {
098         forward(length);
099      }
100      else
101      {
102         curve(n - 1, length / 2.0, -angle);
103         turn(angle);
104         curve(n - 1, length / 2.0,  angle);
105         turn(angle);
106         curve(n - 1, length / 2.0, -angle);
107      }
108   }
109*/