View Javadoc
1   package org.woehlke.simulation.evolution.model;
2   
3   import java.io.Serializable;
4   
5   /**
6    * A Point is used to define the Position of Cell or as a Vector for defining Dimensions.
7    *
8    * Simulated Evolution.
9    * Artificial Life Simulation of Bacteria Motion depending on DNA.
10   *
11   * © 2006 - 2008 Thomas Woehlke.
12   * http://thomas-woehlke.de/p/simulated-evolution/
13   * @author Thomas Woehlke
14   * Date: 04.02.2006
15   * Time: 23:47:05
16   */
17  public class Point implements Serializable {
18  
19      static final long serialVersionUID = 242L;
20  
21      /**
22       * Horizontal X-Coordinate. Also used as Width;
23       */
24      private int x = 0;
25  
26      /**
27       * Vertical Y-Coordinate. Also used as Height;
28       */
29      private int y = 0;
30  
31      public Point(Point p) {
32          this.x = p.getX();
33          this.y = p.getY();
34      }
35  
36      public Point(int x, int y) {
37          this.x = x;
38          this.y = y;
39      }
40  
41      public int getX() {
42          return x;
43      }
44  
45      public void setX(int x) {
46          this.x = x;
47      }
48  
49      public int getY() {
50          return y;
51      }
52  
53      public void setY(int y) {
54          this.y = y;
55      }
56  
57      public void killNagative() {
58          if (y < 0) {
59              y *= -1;
60          }
61          if (x < 0) {
62              x *= -1;
63          }
64      }
65  
66      public void add(Point p) {
67          this.x += p.getX();
68          this.y += p.getY();
69      }
70  
71      public void normalize(Point p) {
72          this.x %= p.getX();
73          this.y %= p.getY();
74      }
75  
76  
77      /**
78       * Get Neighbourhood.
79       * @param max - limit the dimensions of the world around
80       * @return The Set of Points belonging to the Neighbourhood of the position given by this Point Object.
81       */
82      public Point[] getNeighbourhood(Point max){
83          Point neighbourhood[] = new Point[9];
84          int maxX = max.getX();
85          int maxY = max.getY();
86          neighbourhood[0]= new Point((this.x+maxX-1) % maxX,(this.y+maxY-1) % maxY);
87          neighbourhood[1]= new Point((this.x+maxX-1) % maxX,this.y);
88          neighbourhood[2]= new Point((this.x+maxX-1) % maxX,(this.y+maxY+1) % maxY);
89          neighbourhood[3]= new Point(this.x,(this.y+maxY-1) % maxY);
90          neighbourhood[4]= new Point(this.x,this.y);
91          neighbourhood[5]= new Point(this.x,(this.y+maxY+1) % maxY);
92          neighbourhood[6]= new Point((this.x+maxX+1) % maxX,(this.y+maxY-1) % maxY);
93          neighbourhood[7]= new Point((this.x+maxX+1) % maxX,this.y);
94          neighbourhood[8]= new Point((this.x+maxX+1) % maxX,(this.y+maxY+1) % maxY);
95          return neighbourhood;
96      }
97  }