Skip to content

Instantly share code, notes, and snippets.

@takawo
Created February 4, 2016 09:53
Show Gist options
  • Select an option

  • Save takawo/7d937336d7aefcb3f17a to your computer and use it in GitHub Desktop.

Select an option

Save takawo/7d937336d7aefcb3f17a to your computer and use it in GitHub Desktop.
Mover m;
float num = 20;
int xSize, ySize;
ArrayList<Mover> movers = new ArrayList<Mover>();
void setup() {
size(500, 500);
colorMode(HSB, 360, 100, 100, 100);
background(0, 0, 100);
rectMode(CENTER);
xSize = (int)(width/num);
ySize = (int)(height/num);
for (int i = 0; i < 50; i++) {
int x = (int)(random(num))*xSize;
int y = (int)(random(num))*ySize;
m = new Mover(x, y, 0);
movers.add(m);
}
}
void draw() {
fill(0, 0, 100, 100);
rect(width/2, height/2, width, height);
float sum = 0;
for (Mover m1 : movers) {
m1.update();
int n = m1.checkMovers(movers);
m1.draw();
sum += n;
}
drawGrid();
}
void mousePressed() {
redraw();
}
void drawGrid() {
stroke(0, 0, 0);
strokeWeight(0.1);
for (int i = 0; i < ySize; i ++) {
line(0, i*ySize, width, i*ySize);
}
for (int i = 0; i < xSize; i ++) {
line(i*xSize, 0, i*xSize, height);
}
}
public class Mover extends PVector {
int id;
int status;
color c;
public Mover(float x, float y, int id) {
this.x = x;
this.y = y;
this.id = id;
this.status = 0;
c = color(220, 80, 100);
}
public void update() {
int direction = (int)random(100);
switch(direction) {
case 0:
break;
case 1:
x -= xSize;
y -= ySize;
break;
case 2:
y -= ySize;
break;
case 3:
x += xSize;
y -= ySize;
break;
case 4:
x -= xSize;
break;
case 5:
x += xSize;
break;
case 6:
x -= xSize;
y += ySize;
break;
case 7:
y += ySize;
break;
case 8:
x += xSize;
y += ySize;
break;
}
if (x < 0) {
x += width;
}
if (y < 0) {
y += height;
}
if (x >= width) {
x -= width;
}
if (y >= height) {
y -= height;
}
}
public int checkMovers(ArrayList<Mover> movers) {
int closeNum = 0;
for (Mover m2 : movers) {
if (this.equals(m2) == false) {
float dist = this.dist(m2);
if (dist < 50) {
closeNum++;
}
}
switch(closeNum) {
case 0:
this.c = color(180, 80, 0);
break;
case 1:
this.c = color(150, 80, 20);
break;
case 2:
this.c = color(120, 80, 40);
break;
case 3:
this.c = color(90, 80, 80);
break;
case 4:
this.c = color(60, 80, 100);
break;
case 5:
this.c = color(30, 80, 100);
break;
case 6:
this.c = color(0, 80, 100);
break;
}
}
return closeNum;
}
public void draw() {
fill(c);
rect(x+xSize/2, y+ySize/2, 25, 25);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment