Comment detail

マップの通り抜け (Nested Flatten)

Processing で。 お題「ランダムウォーク」で思いついたのですが、適当な数の粒子をばらまきます。粒子はランダムに移動します。充分な時間が経って一つでも通り抜けられればゴールです(運が良ければ、ですが・・)。

乱択アルゴリズムの一種と言っていいのでしょうか。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
int hsb = 16;
int d = 5;
Maze maze;

void setup() {
  colorMode(HSB, hsb);
  int [][] data = { { 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 0, 1, 0, 0, 0, 0, 0, 0 },
                    { 0, 1, 0, 1, 1, 1, 0, 0 },
                    { 0, 1, 0, 1, 0, 1, 0, 0 },
                    { 0, 1, 1, 1, 0, 1, 1, 1 },
                    { 0, 0, 0, 0, 0, 1, 0, 1 },
                    { 0, 0, 0, 0, 1, 1, 0, 1 },
                    { 0, 1, 1, 1, 1, 0, 0, 1 },
                    { 0, 0, 0, 1, 0, 0, 0, 0 },
                    { 0, 0, 1, 1, 0, 1, 0, 0 },
                    { 0, 0, 1, 0, 0, 1, 0, 0 },
                    { 0, 0, 1, 1, 1, 1, 0, 0 },
                    { 0, 0, 1, 0, 0, 0, 0, 0 },
                    { 0, 1, 1, 1, 1, 1, 0, 0 },
                    { 0, 0, 0, 0, 0, 1, 1, 1 },
                    { 0, 1, 1, 0, 0, 0, 0, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1 }
              };
  size(data[0].length * d, data.length * d);
  maze = new Maze(data, d, 20);
  maze.render();
}

void draw() {
  if (!maze.solved) {
    maze.render();

    rectMode(CORNER);
    fill(hsb, hsb * 0.25);
    rect(0, 0, width, height);
  }
}

class Maze {
  int [][] data;
  int block_size;
  int nparticles;
  Particle[] particles;
  boolean solved = false;
  
  Maze(int[][] data_, int block_size_, int nparticles_) {
    data = data_;
    block_size = block_size_;
    nparticles = nparticles_;
    particles = new Particle[nparticles];
    for (int i=0; i < nparticles; i++) {
      particles[i] = new Particle(0, 0, block_size, this);
    }
  }
  
  void render() {
    rectMode(CORNER);
    fill(0);
    for (int i=0; i < data.length; i++) {
      for (int j=0; j < data[i].length; j++) {
        if (data[i][j] != 1) {
          rect(j * block_size, i * block_size, block_size, block_size);
        }
      }
    }
    
    for (int i=0; i < nparticles; i++) {
      Particle p = particles[i];
      p.move();
      p.render();
      if (isGoal(p)) {
        solved = true;
        break;
      }
    }
  }
  
  boolean isBlock(int x, int y) {
    int xx = x / block_size;
    int yy = y / block_size;
    
    if (xx >= data[0].length || yy >= data.length) {
      return true;
    }

    return (data[yy][xx] != 1);
  }

  boolean isGoal(Particle p) {
    return p.y + block_size >= height;
  }
}

class Particle {
  int x;
  int y;
  int d;
  Maze m;

  Particle(int x_, int y_, int d_, Maze m_) {
    x = x_;
    y = y_;
    d = d_;
    m = m_;
  }
  
  void move() {
    
    float r = random(1);
    int x0 = x;
    int y0 = y;
    
    if (r < 0.25) {
       x += d;
       if (x > width) {
         x = width;
       }
    } else if (r < 0.5) {
      x -= d;
      if (x < 0) {
        x = 0;
      }
    } else if (r < 0.75) {
      y += d;
      if (y > height) {
        y = height;
      }
    } else {
      y -= d;
      if (y < 0) {
        y = 0;
      }
    }
 
    if (m.isBlock(x,y)) {
      x = x0;
      y = y0;
    }    
  }
  
  void render() {
    rectMode(CORNER);
    rect(x, y, d, d);    
  }
}

Index

Feed

Other

Link

Pathtraq

loading...