View Javadoc
1   package org.woehlke.computer.kurzweil.tabs.dla.model;
2   
3   import org.woehlke.computer.kurzweil.tabs.dla.DiffusionLimitedAggregation;
4   
5   import java.util.ArrayList;
6   import java.util.Date;
7   import java.util.List;
8   import java.util.Random;
9   
10  /**
11   * Diffusion Limited Aggregation.
12   *
13   * (C) 2006 - 2013 Thomas Woehlke.
14   * https://thomas-woehlke.blogspot.com/2016/01/diffusion-limited-aggregation.html
15   * @author Thomas Woehlke
16   *
17   * Date: 27.08.13
18   * Time: 14:57
19   */
20  public class Particles implements DiffusionLimitedAggregation {
21  
22      static final long serialVersionUID  = 242L;
23  
24  
25      private Point worldDimensions;
26  
27      private List<Point> particles = new ArrayList<Point>();
28  
29      private Random random;
30  
31      private Dendrite dendrite;
32  
33      public Particles(Point worldDimensions) {
34          this.worldDimensions=worldDimensions;
35          random = new Random(new Date().getTime());
36          for(int i=0; i<NUMBER_OF_PARTICLES;i++){
37              int x = random.nextInt(worldDimensions.getX());
38              int y = random.nextInt(worldDimensions.getY());
39              particles.add(new Point(x>=0?x:-x,y>=0?y:-y));
40          }
41          this.dendrite = new Dendrite(worldDimensions);
42      }
43  
44      public List<Point> getParticles() {
45          return particles;
46      }
47  
48      public void move() {
49          List<Point> newParticles = new ArrayList<Point>();
50          for(Point particle:particles){
51              int x = particle.getX()+worldDimensions.getX();
52              int y = particle.getY()+worldDimensions.getY();
53              int direction = random.nextInt(4);
54              switch (direction>=0?direction:-direction){
55                  case 0: y--; break;
56                  case 1: x++; break;
57                  case 2: y++; break;
58                  case 3: x--; break;
59              }
60              x %= worldDimensions.getX();
61              y %= worldDimensions.getY();
62              particle.setX(x);
63              particle.setY(y);
64              if(!dendrite.hasDendriteNeighbour(particle)){
65                  newParticles.add(particle);
66              }
67          }
68          particles=newParticles;
69      }
70  
71      public int getDendriteColor(int x, int y) {
72          return dendrite.getAgeForPixel(x,y);
73      }
74  }