このウィキの読者になる
更新情報がメールで届きます。
このウィキの読者になる
カテゴリー
最近更新したページ
2011-12-05
2010-02-05
2008-01-31
2007-12-09
2007-11-22
2007-11-04
2007-10-06
2007-05-17
2007-05-13
2007-05-11
2007-05-10
最新コメント
1-14 by awesome things!
117 by stunning seo guys
Processing4 Data by stunning seo guys
送信ボタンの仕組み by stunning seo guys
511 by stunning seo guys
510 by music production software
ProStr by awesome things!
FrontPage by check it out
CSV形式とは by check it out
Menu

Processing10Simulate1

/*--Simulate--*/
/* Spring */
int s_height = 16; // Height
int left = 50; // Left position
int right = 150; // Right position
int max = 100; // Maximum Y value
int min = 20; // Minimum Y value
boolean over = false; // If mouse over
boolean move = false; // If mouse down and over

float M = 0.8; // Mass
float K = 0.2; // Spring constant
float D = 0.92; // Damping
float R = 60; // Rest position

float ps = 60.0; // Position
float vs = 0.0; // Velocity
float as = 0; // Acceleration
float f = 0; // Force


void setup()
{
size(200, 200); rectMode(CORNERS); noStroke(); frameRate(60);
}

void draw()
{
background(102); updateSpring(); drawSpring();
}

void drawSpring()
{
// Draw base fill(0.2); float b_width = 0.5 * ps + -8; rect(width/2 - b_width, ps + s_height, width/2 + b_width, 150);

// Set color and draw top bar if(over || move) { fill(255); } else { fill(204); } rect(left, ps, right, ps + s_height);
}


void updateSpring()
{
// Update the spring position if(!move) { f = -K * (ps - R); // f=-ky as = f / M; // Set the acceleration, f=ma == a=f/m vs = D * (vs + as); // Set the velocity ps = ps + vs; // Updated position } if(abs(vs) < 0.1) { vs = 0.0; }

// Test if mouse is over the top bar if(mouseX > left && mouseX < right && mouseY > ps && mouseY < ps + s_height) { over = true; } else { over = false; } // Set and constrain the position of top bar if(move) { ps = mouseY - s_height/2; if (ps < min) { ps = min; } if (ps > max) { ps = max; } }
}

void mousePressed() {
if(over) { move = true; }
}

void mouseReleased()
{
move = false;
}

int num = 3;
Spring[] springs = new Spring[num];

void setup()
{
size(200, 200); noStroke(); smooth(); springs[0] = new Spring( 70, 160, 20, 0.98, 8.0, 0.1, springs, 0); springs[1] = new Spring(150, 110, 60, 0.95, 9.0, 0.1, springs, 1); springs[2] = new Spring( 40, 70, 120, 0.90, 9.9, 0.1, springs, 2); frameRate(60);
}

void draw()
{
background(51); for(int i=0; i<num; i++) { springs[i].update(); springs[i].draw(); }
}

void mousePressed()
{
for(int i=0; i<num; i++) { springs[i].pressed(); }
}

void mouseReleased()
{
for(int i=0; i<num; i++) { springs[i].released(); }
}

class Spring
{
// Screen values float xpos, ypos; float tempxpos, tempypos; int size = 20; boolean over = false; boolean move = false;

// Spring simulation constants float mass; // Mass float k = 0.2; // Spring constant float damp; // Damping float rest_posx; // Rest position X float rest_posy; // Rest position Y

// Spring simulation variables //float pos = 20.0; // Position float velx = 0.0; // X Velocity float vely = 0.0; // Y Velocity float accel = 0; // Acceleration float force = 0; // Force

Spring[] friends; int me; // Constructor Spring(float x, float y, int s, float d, float m, float k_in, Spring[] others, int id) { xpos = tempxpos = x; ypos = tempypos = y; rest_posx = x; rest_posy = y; size = s; damp = d; mass = m; k = k_in; friends = others; me = id; }

void update() { if(move) { rest_posy = mouseY; rest_posx = mouseX; }

force = -k * (tempypos - rest_posy); // f=-ky accel = force / mass; // Set the acceleration, f=ma == a=f/m vely = damp * (vely + accel); // Set the velocity tempypos = tempypos + vely; // Updated position

force = -k * (tempxpos - rest_posx); // f=-ky accel = force / mass; // Set the acceleration, f=ma == a=f/m velx = damp * (velx + accel); // Set the velocity tempxpos = tempxpos + velx; // Updated position

if((over() || move) && !otherOver() ) { over = true; } else { over = false; } } // Test to see if mouse is over this spring boolean over() { float disX = tempxpos - mouseX; float disY = tempypos - mouseY; if(sqrt(sq(disX) + sq(disY)) < size/2 ) { return true; } else { return false; } } // Make sure no other springs are active boolean otherOver() { for(int i=0; i<num; i++) { if(i != me) { if (friends[i].over == true) { return true; } } } return false; }

void draw() { if(over) { fill(153); } else { fill(255); } ellipse(tempxpos, tempypos, size, size); }

void pressed() { if(over) { move = true; } else { move = false; } }

void released() { move = false; rest_posx = xpos; rest_posy = ypos; }
}

int sx, sy;
float density = 0.5;
int[][][] world;
void setup()
{
size(200, 200); frameRate(12); sx = width; sy = height; world = new int[sx][sy][2]; stroke(255); // Set random cells to 'on' for (int i = 0; i < sx * sy * density; i++) { world[(int)random(sx)][(int)random(sy)][1] = 1; }
}
void draw()
{
background(0); // Drawing and update cycle for (int x = 0; x < sx; x=x+1) { for (int y = 0; y < sy; y=y+1) { //if (world[x][y][1] == 1) // Change recommended by The.Lucky.Mutt if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1)) { world[x][y][0] = 1; point(x, y); } if (world[x][y][1] == -1) { world[x][y][0] = 0; } world[x][y][1] = 0; } } // Birth and death cycle for (int x = 0; x < sx; x=x+1) { for (int y = 0; y < sy; y=y+1) { int count = neighbors(x, y); if (count == 3 && world[x][y][0] == 0) { world[x][y][1] = 1; } if ((count < 2 || count > 3) && world[x][y][0] == 1) { world[x][y][1] = -1; } } }
}
int neighbors(int x, int y)
{
return world[(x + 1) % sx][y][0] + world[x][(y + 1) % sy][0] + world[(x + sx - 1) % sx][y][0] + world[x][(y + sy - 1) % sy][0] + world[(x + 1) % sx][(y + 1) % sy][0] + world[(x + sx - 1) % sx][(y + 1) % sy][0] + world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] + world[(x + 1) % sx][(y + sy - 1) % sy][0];
}
World w;
int numcells = 0;
int maxcells = 4700;
Cell[] cells = new Cell[maxcells];
color spore_color;
int runs_per_loop = 10000;
color black = color(0, 0, 0);
void setup()
{
size(200, 200); frameRate(24); clearscr(); w = new World(); spore_color = color(172, 255, 128); seed();
}

void seed()
{
// Add cells at random places for (int i = 0; i < maxcells; i++) { int cX = (int)random(width); int cY = (int)random(height); if (w.getpix(cX, cY) == black) { w.setpix(cX, cY, spore_color); cells[numcells] = new Cell(cX, cY); numcells++; } }
}

void draw()
{
// Run cells in random order for (int i = 0; i < runs_per_loop; i++) { int selected = min((int)random(numcells), numcells - 1); cells[selected].run(); }
}

void clearscr()
{
for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { set(x, y, color(0)); } }
}

class Cell
{
int x, y; Cell(int xin, int yin) { x = xin; y = yin; }

// Perform action based on surroundings void run() { // Fix cell coordinates while(x < 0) { x+=width; } while(x > width - 1) { x-=width; } while(y < 0) { y+=height; } while(y > height - 1) { y-=height; } // Cell instructions if (w.getpix(x + 1, y) == black) { move(0, 1); } else if (w.getpix(x, y - 1) != black && w.getpix(x, y + 1) != black) { move((int)random(9) - 4, (int)random(9) - 4); } } // Will move the cell (dx, dy) units if that space is empty void move(int dx, int dy) { if (w.getpix(x + dx, y + dy) == black) { w.setpix(x + dx, y + dy, w.getpix(x, y)); w.setpix(x, y, color(0)); x += dx; y += dy; } }
}

class World
{
void setpix(int x, int y, int c) { while(x < 0) x+=width; while(x > width - 1) x-=width; while(y < 0) y+=height; while(y > height - 1) y-=height; set(x, y, c); } color getpix(int x, int y) { while(x < 0) x+=width; while(x > width - 1) x-=width; while(y < 0) y+=height; while(y > height - 1) y-=height; return get(x, y); }
}

void mousePressed()
{
setup();
}
World w;
int maxcells = 4000;
int numcells;
Cell[] cells = new Cell[maxcells];
color spore1, spore2, spore3, spore4;
color black = color(0, 0, 0);
int runs_per_loop = 10000;

void setup()
{
size(200, 200); frameRate(24); clearscr(); w = new World(); spore1 = color(128, 172, 255); spore2 = color(64, 128, 255); spore3 = color(255, 128, 172); spore4 = color(255, 64, 128); numcells = 0; seed();
}

void seed()
{
// Add cells at random places for (int i = 0; i < maxcells; i++) { int cX = int(random(width)); int cY = int(random(height)); int c; float r = random(1); if (r < 0.25) c = spore1; else if (r < 0.5) c = spore2; else if (r < 0.75) c = spore3; else c = spore4; if (w.getpix(cX, cY) == black) { w.setpix(cX, cY, c); cells[numcells] = new Cell(cX, cY); numcells++; } }
}

void draw()
{
// Run cells in random order for (int i = 0; i < runs_per_loop; i++) { int selected = min((int)random(numcells), numcells - 1); cells[selected].run(); }
}

void clearscr()
{
for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { set(x, y, color(0)); } }
}

class Cell
{
int x, y; Cell(int xin, int yin) { x = xin; y = yin; } // Perform action based on surroundings void run() { // Fix cell coordinates while(x < 0) { x+=width; } while(x > width - 1) { x-=width; } while(y < 0) { y+=height; } while(y > height - 1) { y-=height; }

// Cell instructions int myColor = w.getpix(x, y); if (myColor == spore1) { if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1); else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1); else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1); else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1); else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1); else move((int)random(3) - 1, 0); } else if (myColor == spore2) { if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1); else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1); else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1); else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1); else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1); else move((int)random(3) - 1, 0); } else if (myColor == spore3) { if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1); else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1); else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1); else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1); else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1); else move((int)random(3) - 1, 0); } else if (myColor == spore4) { if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1); else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1); else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1); else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1); else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1); else move((int)random(3) - 1, 0); } } // Will move the cell (dx, dy) units if that space is empty void move(int dx, int dy) { if (w.getpix(x + dx, y + dy) == black) { w.setpix(x + dx, y + dy, w.getpix(x, y)); w.setpix(x, y, color(0)); x += dx; y += dy; } }
}

class World
{
void setpix(int x, int y, int c) { while(x < 0) x+=width; while(x > width - 1) x-=width; while(y < 0) y+=height; while(y > height - 1) y-=height; set(x, y, c); } color getpix(int x, int y) { while(x < 0) x+=width; while(x > width - 1) x-=width; while(y < 0) y+=height; while(y > height - 1) y-=height; return get(x, y); }
}

void mousePressed()
{
setup();
}
CA ca; // An instance object to describe the Wolfram basic Cellular Automata

void setup() {
size(200,200); frameRate(30); background(0); int[] ruleset = {0,1,0,1,1,0,1,0}; // An initial rule system ca = new CA(ruleset); // Initialize CA
}

void draw() {
ca.render(); // Draw the CA ca.generate(); // Generate the next level if (ca.finished()) { // If we're done, clear the screen, pick a new ruleset and restart background(0); ca.randomize(); ca.restart(); }
}

void mousePressed() {
background(0); ca.randomize(); ca.restart();
}


class CA {

int[] cells; // An array of 0s and 1s int generation; // How many generations? int scl; // How many pixels wide/high is each cell?

int[] rules; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1}

CA(int[] r) { rules = r; scl = 1; cells = new int[width/scl]; restart(); } CA() { scl = 1; cells = new int[width/scl]; randomize(); restart(); } // Set the rules of the CA void setRules(int[] r) { rules = r; } // Make a random ruleset void randomize() { for (int i = 0; i < 8; i++) { rules[i] = int(random(2)); } } // Reset to generation 0 void restart() { for (int i = 0; i < cells.length; i++) { cells[i] = 0; } cells[cells.length/2] = 1; // We arbitrarily start with just the middle cell having a state of "1" generation = 0; }

// The process of creating the new generation void generate() { // First we create an empty array for the new values int[] nextgen = new int[cells.length]; // For every spot, determine new state by examing current state, and neighbor states // Ignore edges that only have one neighor for (int i = 1; i < cells.length-1; i++) { int left = cells[i-1]; // Left neighbor state int me = cells[i]; // Current state int right = cells[i+1]; // Right neighbor state nextgen[i] = rules(left,me,right); // Compute next generation state based on ruleset } // Copy the array into current value cells = (int[]) nextgen.clone(); generation++; } // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' void render() { for (int i = 0; i < cells.length; i++) { if (cells[i] == 1) fill(255); else fill(0); noStroke(); rect(i*scl,generation*scl, scl,scl); } } // Implementing the Wolfram rules // Could be improved and made more concise, but here we can explicitly see what is going on for each case int rules (int a, int b, int c) { if (a == 1 && b == 1 && c == 1) return rules[0]; if (a == 1 && b == 1 && c == 0) return rules[1]; if (a == 1 && b == 0 && c == 1) return rules[2]; if (a == 1 && b == 0 && c == 0) return rules[3]; if (a == 0 && b == 1 && c == 1) return rules[4]; if (a == 0 && b == 1 && c == 0) return rules[5]; if (a == 0 && b == 0 && c == 1) return rules[6]; if (a == 0 && b == 0 && c == 0) return rules[7]; return 0; } // The CA is done if it reaches the bottom of the screen boolean finished() { if (generation > height/scl) { return true; } else { return false; } }
}
CA ca; // An instance object to describe the Wolfram basic Cellular Automata

void setup() {
size(200,200); frameRate(30); background(0); int[] ruleset = {0,1,0,1,1,0,1,0}; // An initial rule system ca = new CA(ruleset); // Initialize CA
}

void draw() {
ca.render(); // Draw the CA ca.generate(); // Generate the next level if (ca.finished()) { // If we're done, clear the screen, pick a new ruleset and restart background(0); ca.randomize(); ca.restart(); }
}

void mousePressed() {
background(0); ca.randomize(); ca.restart();
}


class CA {

int[] cells; // An array of 0s and 1s int generation; // How many generations? int scl; // How many pixels wide/high is each cell?

int[] rules; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1}

CA(int[] r) { rules = r; scl = 1; cells = new int[width/scl]; restart(); } CA() { scl = 1; cells = new int[width/scl]; randomize(); restart(); } // Set the rules of the CA void setRules(int[] r) { rules = r; } // Make a random ruleset void randomize() { for (int i = 0; i < 8; i++) { rules[i] = int(random(2)); } } // Reset to generation 0 void restart() { for (int i = 0; i < cells.length; i++) { cells[i] = 0; } cells[cells.length/2] = 1; // We arbitrarily start with just the middle cell having a state of "1" generation = 0; }

// The process of creating the new generation void generate() { // First we create an empty array for the new values int[] nextgen = new int[cells.length]; // For every spot, determine new state by examing current state, and neighbor states // Ignore edges that only have one neighor for (int i = 1; i < cells.length-1; i++) { int left = cells[i-1]; // Left neighbor state int me = cells[i]; // Current state int right = cells[i+1]; // Right neighbor state nextgen[i] = rules(left,me,right); // Compute next generation state based on ruleset } // Copy the array into current value cells = (int[]) nextgen.clone(); generation++; } // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' void render() { for (int i = 0; i < cells.length; i++) { if (cells[i] == 1) fill(255); else fill(0); noStroke(); rect(i*scl,generation*scl, scl,scl); } } // Implementing the Wolfram rules // Could be improved and made more concise, but here we can explicitly see what is going on for each case int rules (int a, int b, int c) { if (a == 1 && b == 1 && c == 1) return rules[0]; if (a == 1 && b == 1 && c == 0) return rules[1]; if (a == 1 && b == 0 && c == 1) return rules[2]; if (a == 1 && b == 0 && c == 0) return rules[3]; if (a == 0 && b == 1 && c == 1) return rules[4]; if (a == 0 && b == 1 && c == 0) return rules[5]; if (a == 0 && b == 0 && c == 1) return rules[6]; if (a == 0 && b == 0 && c == 0) return rules[7]; return 0; } // The CA is done if it reaches the bottom of the screen boolean finished() { if (generation > height/scl) { return true; } else { return false; } }
}
int res = 2;
int penSize = 30;
int lwidth;
int lheight;
int pnum = 30000;
vsquare[][] v;
vbuffer[][] vbuf;
particle[] p = new particle[pnum];
int pcount = 0;
int mouseXvel = 0;
int mouseYvel = 0;

void setup()
{
size(200,200); noStroke(); frameRate(30); lwidth = width/res; lheight = height/res; v = new vsquare[lwidth+1][lheight+1]; vbuf = new vbuffer[lwidth+1][lheight+1]; for(int i = 0; i < pnum; i++) { p[i] = new particle(random(res,width-res),random(res,height-res)); } for(int i = 0; i <= lwidth; i++) { for(int u = 0; u <= lheight; u++) { v[i][u] = new vsquare(i*res,u*res); vbuf[i][u] = new vbuffer(i*res,u*res); } }
}

void draw()
{
background(#666666); int axvel = mouseX-pmouseX; int ayvel = mouseY-pmouseY;

mouseXvel = (axvel != mouseXvel) ? axvel : 0; mouseYvel = (ayvel != mouseYvel) ? ayvel : 0;

for(int i = 0; i < lwidth; i++) { for(int u = 0; u < lheight; u++) { vbuf[i][u].updatebuf(i,u); v[i][u].col = 32; } } for(int i = 0; i < pnum-1; i++) { p[i].updatepos(); } for(int i = 0; i < lwidth; i++) { for(int u = 0; u < lheight; u++) { v[i][u].addbuffer(i, u); v[i][u].updatevels(mouseXvel, mouseYvel); v[i][u].display(i, u); } }
}

class particle {
float x; float y; float xvel; float yvel; int pos; particle(float xIn, float yIn) { x = xIn; y = yIn; }

void updatepos() { float col1; if(x > 0 && x < width && y > 0 && y < height) { int vi = (int)(x/res); int vu = (int)(y/res); vsquare o = v[vi][vu]; float ax = (x%res)/res; float ay = (y%res)/res; xvel += (1-ax)*v[vi][vu].xvel*0.05; yvel += (1-ay)*v[vi][vu].yvel*0.05; xvel += ax*v[vi+1][vu].xvel*0.05; yvel += ax*v[vi+1][vu].yvel*0.05; xvel += ay*v[vi][vu+1].xvel*0.05; yvel += ay*v[vi][vu+1].yvel*0.05;

o.col += 4; x += xvel; y += yvel; } else { x = random(0,width); y = random(0,height); xvel = 0; yvel = 0; }

xvel *= 0.5; yvel *= 0.5; }
}

class vbuffer {
int x; int y; float xvel; float yvel; float pressurex = 0; float pressurey = 0; float pressure = 0;

vbuffer(int xIn,int yIn) { x = xIn; y = yIn; pressurex = 0; pressurey = 0; }

void updatebuf(int i, int u) { if(i>0 && i<lwidth && u>0 && u<lheight) { pressurex = (v[i-1][u-1].xvel*0.5 + v[i-1][u].xvel + v[i-1][u+1].xvel*0.5 - v[i+1][u-1].xvel*0.5 - v[i+1][u].xvel - v[i+1][u+1].xvel*0.5); pressurey = (v[i-1][u-1].yvel*0.5 + v[i][u-1].yvel + v[i+1][u-1].yvel*0.5 - v[i-1][u+1].yvel*0.5 - v[i][u+1].yvel - v[i+1][u+1].yvel*0.5); pressure = (pressurex + pressurey)*0.25; } } }

class vsquare {
int x; int y; float xvel; float yvel; float col;

vsquare(int xIn,int yIn) { x = xIn; y = yIn; }

void addbuffer(int i, int u) { if(i>0 && i<lwidth && u>0 && u<lheight) { xvel += (vbuf[i-1][u-1].pressure*0.5 +vbuf[i-1][u].pressure +vbuf[i-1][u+1].pressure*0.5 -vbuf[i+1][u-1].pressure*0.5 -vbuf[i+1][u].pressure -vbuf[i+1][u+1].pressure*0.5 )*0.25; yvel += (vbuf[i-1][u-1].pressure*0.5 +vbuf[i][u-1].pressure +vbuf[i+1][u-1].pressure*0.5 -vbuf[i-1][u+1].pressure*0.5 -vbuf[i][u+1].pressure -vbuf[i+1][u+1].pressure*0.5 )*0.25; } }

void updatevels(int mvelX, int mvelY) { if(mousePressed) { float adj = x - mouseX; float opp = y - mouseY; float dist = sqrt(opp*opp + adj*adj); if(dist < penSize) { if(dist < 4) dist = penSize; float mod = penSize/dist; xvel += mvelX*mod; yvel += mvelY*mod; } }

xvel *= 0.99; yvel *= 0.99; } void display(int i, int u) { float tcol = 0; if(col > 255) col = 255; if(i>0 && i<lwidth-1 && u>0 && u<lheight-1) { tcol = (+ v[i][u+1].col + v[i+1][u].col + v[i+1][u+1].col*0.5 )*0.4; tcol = (int)(tcol+col*0.5); } else { tcol = (int)col; } fill(tcol, tcol, tcol); rect(x,y,res,res); }
}
2006年11月18日(土) 10:54:30 Modified by ID:hElD8hu5PA




スマートフォン版で見る