Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created March 1, 2018 21:55
Show Gist options
  • Select an option

  • Save KrabCode/e43ed45d786b2c5dc0e23479e98ea2d0 to your computer and use it in GitHub Desktop.

Select an option

Save KrabCode/e43ed45d786b2c5dc0e23479e98ea2d0 to your computer and use it in GitHub Desktop.
almost random walkers for Processing on Eclipse or IDEA
import processing.core.PApplet;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class Main extends PApplet{
public static void main(String[] args) {
PApplet.main("Main");
}
ArrayList<Crawler> crawlers = new ArrayList<Crawler>();
int cols = 1920/25;
int rows = 1080/25;
int[] map = new int[cols * rows];
/*
values:
-1: border
0-255: color
*/
public void settings(){
// size(800,600);
fullScreen();
}
public void setup(){
background(0);
colorMode(HSB);
rectMode(CENTER);
setBorders();
frameRate(60);
makeCrawler(0,255,255);
makeCrawler(30,255,255);
makeCrawler(60,255,255);
makeCrawler(90,255,255);
}
void makeCrawler(int h, int s, int b){
Crawler c = new Crawler();
c.xPos = randomInteger(2,cols-1);
c.yPos = randomInteger(2,rows-1);
c.h = h;
c.s = s;
c.b = b;
crawlers.add(c);
}
public void draw(){
drawNodes();
for(Crawler c : crawlers){
c.step();
c.draw();
}
}
class Coord{
public Coord(int x, int y){
this.x = x;
this.y = y;
}
int x, y;
}
//crawler
class Crawler{
int xPos, yPos;
List<Coord> visited;
int h, s, b;
Crawler(){
visited = new ArrayList<Coord>();
}
void step(){
List<Coord> options = getStepOptions(true, xPos, yPos);
if(options.size()==0){
options = getStepOptions(false, xPos, yPos);
}
if(options.size() > 0){
Coord neu = options.get(randomInteger(0, options.size()-1));
Coord old = new Coord(xPos, yPos);
setNode(old.x, old.y, 200);
xPos = neu.x;
yPos = neu.y;
visited.add(old);
}
}
List<Coord> wall = new ArrayList<>();
List<Coord> getStepOptions(boolean strict, int x, int y){
if(!strict && visited.size()!=0){
Coord newPos = visited.remove(visited.size()-1);
println("removed" + newPos.x +":"+ newPos.y );
x = newPos.x;
y = newPos.y;
wall.add(newPos);
}
List<Coord> options = new ArrayList<>();
int xL = x-1;
int xR = x+1;
int yU = y-1;
int yD = y+1;
if(getNode(xL, y) == 0){
options.add(new Coord(xL, y));
}
if(getNode(xR, y) == 0){
options.add(new Coord(xR, y));
}
if(getNode(x, yD) == 0){
options.add(new Coord(x, yD));
}
if(getNode(x, yU) == 0){
options.add(new Coord(x, yU));
}
if(options.size()==0 && visited.size()!=0){
return getStepOptions(false, x,y);
}
return options;
}
void draw(){
fill(h,s,b);
drawRectOnGrid(xPos, yPos);
fill(h,s-100,b);
for(Coord waypoint : visited){
drawRectOnGrid(waypoint.x, waypoint.y);
}
stroke(0);
strokeWeight(2);
drawWall();
}
private void drawWall() {
strokeWeight(5);
for (int i = 1; i < wall.size(); i++){
float horScl = width/cols;
float verScl = height/rows;
Coord a = wall.get(i);
Coord b = wall.get(i-1);
if(a.x == b.x || a.y == b.y){
line(a.x*horScl+horScl/2, a.y*verScl+verScl/2,
b.x*horScl+horScl/2, b.y*verScl+verScl/2);
}
}
}
}
int randomInteger(int min, int max){
if(max+1>min){
return ThreadLocalRandom.current().nextInt(min, max + 1);
}else{
return 0;
}
}
//environment
void setBorders(){
for(int x = 0; x < cols; x++){
setNode(x, 0, -1);
setNode(x, rows-1, -1);
}
for(int y = 1; y < rows-1; y++){
setNode(0, y, -1);
setNode(cols-1, y, -1);
}
}
int getNode(int x, int y){
if(x < cols && y < rows){
return map[y*cols+x];
}else return 0;
}
void setNode(int x, int y, int value){
if(x < cols && y < rows){
map[y*cols+x] = value;
}
}
void drawNodes(){
strokeWeight(1);
for(int x = 0; x < cols; x++){
for(int y = 0; y < rows; y++){
int val = getNode(x,y);
if(val == -1){
fill(50);
}else{
fill(val%255);
}
//drawRectOnGrid(x,y);
}
}
}
void drawRectOnGrid(int x, int y){
float horScl = width/cols;
float verScl = height/rows;
rect(x*horScl+horScl/2, y*verScl+verScl/2, horScl,verScl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment