View Javadoc
1   package org.woehlke.computer.kurzweil.conwaysgameoflive.model.lattice;
2   
3   import lombok.EqualsAndHashCode;
4   import lombok.Getter;
5   import lombok.ToString;
6   import lombok.extern.log4j.Log4j2;
7   
8   import java.io.Serializable;
9   
10  /**
11   * © 2006 - 2008 Thomas Woehlke.
12   * @author Thomas Woehlke
13   *
14   * @see <a href="https://github.com/Computer-Kurzweil/conwaysgameoflife">Github Repository</a>
15   * @see <a href="https://java.woehlke.org/conwaysgameoflife/">Maven Project Repository</a>
16   */
17  @Log4j2
18  @Getter
19  @ToString
20  @EqualsAndHashCode
21  public class LatticeNeighbourhood implements Serializable {
22  
23      static final long serialVersionUID = 242L;
24  
25      private final LatticeNeighbourhoodType neighbourhoodType;
26      private final int maxX;
27      private final int maxY;
28      private final int x;
29      private final int y;
30  
31      private LatticePoint[] neighbourhood;
32  
33      public LatticeNeighbourhood(
34          int maxX,  int maxY, int x,  int y,
35          LatticeNeighbourhoodType neighbourhoodType
36      ) {
37          this.neighbourhoodType = neighbourhoodType;
38          this.maxX = maxX;
39          this.maxY = maxY;
40          this.x = x;
41          this.y = y;
42          this.neighbourhood = getNeighbourhoodPoints();
43      }
44  
45      /**
46       * Get Neighbourhood.
47       *
48       * @return The Set of Points belonging to the Neighbourhood of the position given by this Point Object.
49       */
50      private LatticePoint[] getNeighbourhoodPoints() {
51          LatticePointNeighbourhoodPosition[] positions = LatticePointNeighbourhoodPosition.getNeighbourhoodFor(neighbourhoodType);
52          this.neighbourhood = new LatticePoint[positions.length];
53          for(int i = 0; i < positions.length; i++){
54              this.neighbourhood[i] = new LatticePoint(
55                  (x + maxX + positions[i].getX()) % maxX,
56                  (y + maxY + positions[i].getY()) % maxY
57              );
58          }
59          return this.neighbourhood;
60      }
61  
62      public static LatticePoint[] get(int worldX, int worldY, int myX, int myY) {
63          LatticeNeighbourhoodType neighbourhoodType = LatticeNeighbourhoodType.MOORE_NEIGHBORHOOD;
64          LatticeNeighbourhood n = new LatticeNeighbourhood(worldX, worldY, myX, myY, neighbourhoodType);
65          return n.getNeighbourhoodPoints();
66      }
67  
68      public static LatticePoint[] getMoore(int worldX, int worldY, int myX, int myY) {
69          LatticeNeighbourhoodType neighbourhoodType = LatticeNeighbourhoodType.MOORE_NEIGHBORHOOD;
70          LatticeNeighbourhood n = new LatticeNeighbourhood(worldX, worldY, myX, myY, neighbourhoodType);
71          return n.getNeighbourhoodPoints();
72      }
73  
74      public static LatticePoint[] getVonNeumann(int worldX, int worldY, int myX, int myY) {
75          LatticeNeighbourhoodType neighbourhoodType = LatticeNeighbourhoodType.VON_NEUMANN_NEIGHBORHOOD;
76          LatticeNeighbourhood n = new LatticeNeighbourhood(worldX, worldY, myX, myY, neighbourhoodType);
77          return n.getNeighbourhoodPoints();
78      }
79  
80      public static LatticePoint[] getWoehlke(int worldX, int worldY, int myX, int myY) {
81          LatticeNeighbourhoodType neighbourhoodType = LatticeNeighbourhoodType.WOEHLKE_NEIGHBORHOOD;
82          LatticeNeighbourhood n = new LatticeNeighbourhood(worldX, worldY, myX, myY, neighbourhoodType);
83          return n.getNeighbourhoodPoints();
84      }
85  
86  }