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