View Javadoc
1   package org.woehlke.computer.kurzweil.mandelbrot.zoom.model.turing;
2   
3   import org.woehlke.computer.kurzweil.mandelbrot.zoom.model.common.Point;
4   
5   /**
6    * Mandelbrot Set drawn by a Turing Machine.
7    * (C) 2006 - 2022 Thomas Woehlke.
8    * @author Thomas Woehlke
9    *
10   * @see <a href="https://thomas-woehlke.blogspot.com/2016/01/mandelbrot-set-drawn-by-turing-machine.html">Blog Article</a>
11   * @see <a href="https://github.com/Computer-Kurzweil/mandelbrot-julia">Github Repository</a>
12   * @see <a href="https://java.woehlke.org/mandelbrot-julia/">Maven Project Repository</a>
13   *
14   * @see Point
15   * @see TuringDirection
16   *
17   * Created by tw on 16.12.2019.
18   */
19  public class TuringPositions {
20  
21      private volatile Point turingPosition;
22      private volatile Point worldDimensions;
23      private volatile Point firstSetPosition;
24  
25      private volatile TuringDirection turingDirection;
26  
27      private volatile int steps;
28  
29      public TuringPositions(Point worldDimensions) {
30          this.worldDimensions = worldDimensions;
31          start();
32      }
33  
34      public void start() {
35          this.steps = 0;
36          this.turingPosition = new Point((worldDimensions.getX()-2),(worldDimensions.getY()/2+11));
37          this.turingDirection = TuringDirection.LEFT;
38      }
39  
40      public synchronized void markFirstSetPosition(){
41          this.firstSetPosition = turingPosition;
42          this.steps = 0;
43      }
44  
45      public synchronized Point getTuringPosition() {
46          return turingPosition;
47      }
48  
49      public synchronized void goForward() {
50          this.steps++;
51          switch (this.turingDirection){
52              case UP:
53                  this.turingPosition.moveUp();
54                  break;
55              case RIGHT:
56                  this.turingPosition.moveRight();
57                  break;
58              case DOWN:
59                  this.turingPosition.moveDown();
60                  break;
61              case LEFT:
62                  this.turingPosition.moveLeft();
63                  break;
64              default:
65                  break;
66          }
67      }
68  
69      public synchronized void turnRight() {
70          TuringDirection newTuringDirection;
71          switch (this.turingDirection){
72              case UP: newTuringDirection = TuringDirection.RIGHT; break;
73              case RIGHT: newTuringDirection = TuringDirection.DOWN; break;
74              case DOWN: newTuringDirection = TuringDirection.LEFT; break;
75              case LEFT: newTuringDirection = TuringDirection.UP; break;
76              default: newTuringDirection = this.turingDirection; break;
77          }
78          this.turingDirection = newTuringDirection;
79      }
80  
81      public synchronized void turnLeft() {
82          TuringDirection newTuringDirection;
83          switch (this.turingDirection){
84              case UP: newTuringDirection = TuringDirection.LEFT; break;
85              case RIGHT: newTuringDirection = TuringDirection.UP; break;
86              case DOWN: newTuringDirection = TuringDirection.RIGHT; break;
87              case LEFT: newTuringDirection = TuringDirection.DOWN; break;
88              default: newTuringDirection = this.turingDirection; break;
89          }
90          this.turingDirection = newTuringDirection;
91      }
92  
93      public synchronized boolean isFinishedWalkAround() {
94          return (this.turingPosition.equals(this.firstSetPosition)) && (this.steps>100);
95      }
96  
97  }