Processing4 Math
/*--Math--*/
int a;
int b;
boolean direction;
void setup()
{
size(200, 200); colorMode(RGB, width); a = 0; b = width; direction = true; frameRate(30); }
void draw() { a++; if(a > width) { a = 0; direction = !direction; } if(direction == true){ stroke(a); } else { stroke(width-a); } line(a, 0, a, height/2);
b--; if(b < 0) { b = width; } if(direction == true) { stroke(width-b); } else { stroke(b); } line(b, height/2+1, b, height); }int num = 20;
float c;
void setup()
{
size(200,200); fill(255); frameRate(30); }
void draw() { background(0); c+=0.1; for(int i=1; i<height/num; i++) { float x = (c%i)*i*i; stroke(102); line(0, i*num, x, i*num); noStroke(); rect(x, i*num-num/2, 8, num); } }
size(200, 200);
background(51);
noFill();
stroke(51);
stroke(204);
for(int i=0; i< width-20; i+= 4) {
// The 30 is added to 70 and then evaluated // if it is greater than the current value of "i" // For clarity, write as "if(i > (30 + 70)) {" if(i > 30 + 70) { line(i, 0, i, 50); } }
stroke(255); // The 2 is multiplied by the 8 and the result is added to the 5 // For clarity, write as "rect(5 + (2 * 8), 0, 90, 20);" rect(4 + 2 * 8, 52, 90, 48); rect((4 + 2) * 8, 100, 90, 49); stroke(153); for(int i=0; i< width; i+= 2) { // The relational statements are evaluated // first, and then the logical AND statements and // finally the logical OR. For clarity, write as: // "if(((i > 10) && (i < 50)) || ((i > 80) && (i < 160))) {" if(i > 20 && i < 50 || i > 100 && i < width-20) { line(i, 151, i, height-1); } }int thin = 8;
int thick = 36;
float xpos1 = 134.0;
float xpos2 = 44.0;
float xpos3 = 58.0;
float xpos4 = 120.0;
void setup()
{
size(200, 200); noStroke(); frameRate(60); }
void draw() { background(0); float mx = mouseX * 0.4 - width/5.0; fill(102); rect(xpos2, 0, thick, height/2); fill(204); rect(xpos1, 0, thin, height/2); fill(102); rect(xpos4, height/2, thick, height/2); fill(204); rect(xpos3, height/2, thin, height/2); xpos1 += mx/16; xpos2 += mx/64; xpos3 -= mx/16; xpos4 -= mx/64; if(xpos1 < -thin) { xpos1 = width; } if(xpos1 > width) { xpos1 = -thin; } if(xpos2 < -thick) { xpos2 = width; } if(xpos2 > width) { xpos2 = -thick; } if(xpos3 < -thin) { xpos3 = width; } if(xpos3 > width) { xpos3 = -thin; } if(xpos4 < -thick) { xpos4 = width; } if(xpos4 > width) { xpos4 = -thick; } }float max_distance;
void setup() {
size(200, 200); rectMode(CENTER); noStroke(); max_distance = dist(0, 0, width, height); }
void draw() { background(51);
for(int i=20; i<width; i+=20) { for(int j=20; j<width; j+=20) { float size = dist(mouseX, mouseY, i, j); size = size/max_distance * 66; rect(i, j, size, size); } } }float spin = 0.0;
float radius = 42.0;
float angle;
float angle_rot;
int rad_points = 90;
void setup()
{
size(200, 200); noStroke(); ellipseMode(CENTER_RADIUS); smooth(); frameRate(30); }
void draw() { background(153); translate(130, 65); fill(255); ellipse(0, 0, 8, 8); angle_rot = 0; fill(51);
for(int i=0; i<5; i++) { pushMatrix(); rotate(angle_rot + -45); ellipse(-116, 0, radius, radius); popMatrix(); angle_rot += PI*2/5; }
radius = 17 * sin(angle) + 84; angle += 0.03; if (angle > TWO_PI) { angle = 0; } }int i = 45;
int j = 225;
float pos1 = 0;
float pos2 = 0;
float pos3 = 0;
float pos4 = 0;
int sc = 40;
void setup()
{
size(200, 200); noStroke(); smooth(); frameRate(60); }
void draw() { background(0); fill(51); rect(60, 60, 80, 80);
fill(255); ellipse(pos1, 36, 32, 32);
fill(153); ellipse(36, pos2, 32, 32);
fill(255); ellipse(pos3, 164, 32, 32);
fill(153); ellipse(164, pos4, 32, 32);
i += 3; j -= 3;
if(i > 405) { i = 45; j = 225; }
float ang1 = radians(i); // convert degrees to radians float ang2 = radians(j); // convert degrees to radians pos1 = width/2 + (sc * cos(ang1)); pos2 = width/2 + (sc * sin(ang1)); pos3 = width/2 + (sc * cos(ang2)); pos4 = width/2 + (sc * sin(ang2)); }
int xspacing = 8; // How far apart should each horizontal location be spaced int w; // Width of entire wave int maxwaves = 4; // total # of waves to add together
float theta = 0.0f; float[] amplitude = new float[maxwaves]; // Height of wave float[] dx = new float[maxwaves]; // Value for incrementing X, to be calculated as a function of period and xspacing float[] yvalues; // Using an array to store height values for the wave (not entirely necessary)
void setup() { size(200,200); frameRate(30); colorMode(RGB,255,255,255,100); smooth(); w = width+16;
for (int i = 0; i < maxwaves; i++) { amplitude[i] = random(10,30); float period = random(100,300); // How many pixels before the wave repeats dx[i] = (TWO_PI / period) * xspacing; }
yvalues = new float[w/xspacing]; }
void draw() { background(0); calcWave(); renderWave(); }
void calcWave() { // Increment theta (try different values for 'angular velocity' here theta += 0.02;
// Set all height values to zero for (int i = 0; i < yvalues.length; i++) { yvalues[i] = 0.0f; } // Accumulate wave height values for (int j = 0; j < maxwaves; j++) { float x = theta; for (int i = 0; i < yvalues.length; i++) { // Every other wave is cosine instead of sine if (j % 2 == 0) yvalues[i] += sin(x)*amplitude[j]; else yvalues[i] += cos(x)*amplitude[j]; x+=dx[j]; } } }
void renderWave() { // A simple way to draw the wave with an ellipse at each location noStroke(); fill(255,50); ellipseMode(CENTER); for (int x = 0; x < yvalues.length; x++) { ellipse(x*xspacing,width/2+yvalues[x],16,16); } }
Eye e1, e2, e3, e4, e5;
void setup() { size(200, 200); smooth(); noStroke(); e1 = new Eye( 50, 16, 80); e2 = new Eye( 64, 85, 40); e3 = new Eye( 90, 200, 120); e4 = new Eye(150, 44, 40); e5 = new Eye(175, 120, 80); }
void draw() { background(102); e1.update(mouseX, mouseY); e2.update(mouseX, mouseY); e3.update(mouseX, mouseY); e4.update(mouseX, mouseY); e5.update(mouseX, mouseY);
e1.display(); e2.display(); e3.display(); e4.display(); e5.display(); }
class Eye { int ex, ey; int size; float angle = 0.0; Eye(int x, int y, int s) { ex = x; ey = y; size = s; }
void update(int mx, int my) { angle = atan2(my-ey, mx-ex); } void display() { pushMatrix(); translate(ex, ey); fill(255); ellipse(0, 0, size, size); rotate(angle); fill(153); ellipse(size/4, 0, size/2, size/2); popMatrix(); } }
void setup() {
size(200,200); frameRate(30); }
void draw() { loadPixels(); float n = (mouseX * 10.0f) / width; float w = 16.0f; // 2D space width float h = 16.0f; // 2D space height float dx = w / width; // Increment x this amount per pixel float dy = h / height; // Increment y this amount per pixel float x = -w/2; // Start x at -1 * width / 2 for (int i = 0; i < width; i++) { float y = -h/2; // Start y at -1 * height / 2 for (int j = 0; j < height; j++) { float r = sqrt((x*x) + (y*y)); // Convert cartesian to polar float theta = atan2(y,x); // Convert cartesian to polar // Compute 2D polar coordinate function float val = sin(n*cos(r) + 5 * theta); // Results in a value between -1 and 1 //float val = cos(r); // Another simple function //float val = sin(theta); // Another simple function // Map resulting vale to grayscale value pixels[i+j*width] = color((val + 1.0f)* 255.0/2.0); // Scale to between 0 and 255 y += dy; // Increment y } x += dx; // Increment x } updatePixels(); }
KochFractal k;
void setup() { size(200,200); background(0); frameRate(1); // Animate slowly k = new KochFractal(); smooth(); }
void draw() { background(0); // Draws the snowflake! k.render(); // Iterate k.nextLevel(); // Let's not do it more than 5 times. . . if (k.getCount() > 5) { k.restart(); }
}
// A class to manage the list of line segments in the snowflake pattern
class KochFractal { Point start; // A point for the start Point end; // A point for the end ArrayList lines; // A list to keep track of all the lines int count; public KochFractal() { start = new Point(0,height/2 + height/4); end = new Point(width,height/2 + height/4); lines = new ArrayList(); restart(); }
void nextLevel() { // For every line that is in the arraylist // create 4 more lines in a new arraylist lines = iterate(lines); count++; }
void restart() { count = 0; // Reset count lines.clear(); // Empty the array list lines.add(new KochLine(start,end)); // Add the initial line (from one end point to the other) } int getCount() { return count; } // This is easy, just draw all the lines void render() { for(int i = 0; i < lines.size(); i++) { KochLine l = (KochLine)lines.get(i); l.render(); } }
// This is where the **MAGIC** happens // Step 1: Create an empty arraylist // Step 2: For every line currently in the arraylist // - calculate 4 line segments based on Koch algorithm // - add all 4 line segments into the new arraylist // Step 3: Return the new arraylist and it becomes the list of line segments for the structure // As we do this over and over again, each line gets broken into 4 lines, which gets broken into 4 lines, and so on. . . ArrayList iterate(ArrayList before) { ArrayList now = new ArrayList(); //Create emtpy list for(int i = 0; i < before.size(); i++) { KochLine l = (KochLine)lines.get(i); // A line segment inside the list // Calculate 5 koch points (done for us by the line object) Point a = l.start(); Point b = l.kochleft(); Point c = l.kochmiddle(); Point d = l.kochright(); Point e = l.end(); // Make line segments between all the points and add them now.add(new KochLine(a,b)); now.add(new KochLine(b,c)); now.add(new KochLine(c,d)); now.add(new KochLine(d,e)); } return now; }
}
// A class to describe one line segment in the fractal // Includes methods to calculate midpoints along the line according to the Koch algorithm
class KochLine { // Two points, // a is the "left" point and // b is the "right point Point a,b; KochLine(Point a_, Point b_) { a = a_.copy(); b = b_.copy(); } void render() { stroke(255); line(a.x,a.y,b.x,b.y); } Point start() { return a.copy(); } Point end() { return b.copy(); } // This is easy, just 1/3 of the way Point kochleft() { float x = a.x + (b.x - a.x) / 3f; float y = a.y + (b.y - a.y) / 3f; return new Point(x,y); } // More complicated, have to use a little trig to figure out where this point is! Point kochmiddle() { float x = a.x + 0.5f * (b.x - a.x) + (sin(radians(60))*(b.y-a.y)) / 3; float y = a.y + 0.5f * (b.y - a.y) - (sin(radians(60))*(b.x-a.x)) / 3; return new Point(x,y); }
// Easy, just 2/3 of the way Point kochright() { float x = a.x + 2*(b.x - a.x) / 3f; float y = a.y + 2*(b.y - a.y) / 3f; return new Point(x,y); }
}
class Point { float x,y; Point(float x_, float y_) { x = x_; y = y_; } Point copy() { return new Point(x,y); } }float xmin = -2.5;
float ymin = -2;
float wh = 4;
void setup() {
size(200,200,P2D); }
void draw() {
loadPixels(); // Maximum number of iterations for each point on the complex plane int maxiterations = 200;
// x goes from xmin to xmax float xmax = xmin + wh; // y goes from ymin to ymax float ymax = ymin + wh; // Calculate amount we increment x,y for each pixel float dx = (xmax - xmin) / (width); float dy = (ymax - ymin) / (height);
// Start y float y = ymin; for(int j = 0; j < height; j++) { // Start x float x = xmin; for(int i = 0; i < width; i++) { // Now we test, as we iterate z = z^2 + cm does z tend towards infinity? float a = x; float b = y; int n = 0; while (n < maxiterations) { float aa = a * a; float bb = b * b; float twoab = 2.0 * a * b; a = aa - bb + x; b = twoab + y; // Infinty in our finite world is simple, let's just consider it 16 if(aa + bb > 16.0f) { break; // Bail } n++; } // We color each pixel based on how long it takes to get to infinity // If we never got there, let's pick the color black if (n == maxiterations) pixels[i+j*width] = 0; else pixels[i+j*width] = color(n*16 % 255); // Gosh, we could make fancy colors here if we wanted x += dx; } y += dy; } updatePixels(); noLoop(); }size(200, 200);
noStroke();
for(int i=0; i<width; i++) {
float r = random(0, 255); stroke(r); point(i, 0); }
for(int i=1; i<width; i++) { for(int j=0; j<height; j++) { color p = get(j, i-1); stroke(red(p)-1); point(j, i); } }float xoff = 0.0;
float xincrement = 0.01;
void setup() {
size(200,200); background(0); frameRate(30); smooth(); noStroke(); }
void draw() { // Create an alpha blended background fill(0, 10); rect(0,0,width,height); //float n = random(0,width); // Try this line instead of noise // Get a noise value based on xoff and scale it according to the window's width float n = noise(xoff)*width; // With each cycle, increment xoff xoff += xincrement; // Draw the ellipse at the value produced by perlin noise fill(200); ellipse(n,height/2,16,16); }
float increment = 0.02;
void setup() { size(200,200); noLoop(); }
void draw() { background(0); // Optional: adjust noise detail here // noiseDetail(8,0.65f); loadPixels();
float xoff = 0.0; // Start xoff at 0 // For every x,y coordinate in a 2D space, calculate a noise value and produce a brightness value for (int x = 0; x < width; x++) { xoff += increment; // Increment xoff float yoff = 0.0; // For every xoff, start yoff at 0 for (int y = 0; y < height; y++) { yoff += increment; // Increment yoff // Calculate noise and scale by 255 float bright = noise(xoff,yoff)*255;
// Try using this line instead //float bright = random(0,255); // Set each pixel onscreen to a grayscale value pixels[x+y*width] = color(bright); } } updatePixels(); }
float increment = 0.01;
float zoff = 0.0;
float zincrement = 0.02;
void setup() {
size(200,200); frameRate(30); }
void draw() { background(0); // Optional: adjust noise detail here // noiseDetail(8,0.65f); loadPixels();
float xoff = 0.0; // Start xoff at 0 // For every x,y coordinate in a 2D space, calculate a noise value and produce a brightness value for (int x = 0; x < width; x++) { xoff += increment; // Increment xoff float yoff = 0.0f; // For every xoff, start yoff at 0 for (int y = 0; y < height; y++) { yoff += increment; // Increment yoff // Calculate noise and scale by 255 float bright = noise(xoff,yoff,zoff)*255;
// Try using this line instead //float bright = random(0,255); // Set each pixel onscreen to a grayscale value pixels[x+y*width] = color(bright,bright,bright); } } updatePixels(); zoff += zincrement; // Increment zoff }
int xspacing = 8; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
float yoff = 0.0f; // 2nd dimension of perlin noise
float[] yvalues; // Using an array to store height values for the wave (not entirely necessary)
void setup() {
size(200,200); frameRate(30); colorMode(RGB,255,255,255,100); smooth(); w = width+16; yvalues = new float[w/xspacing]; }
void draw() { background(0); calcWave(); renderWave();
}
void calcWave() { float dx = 0.05f; float dy = 0.01f; float amplitude = 100.0f;
// Increment y ('time') yoff += dy;
//float xoff = 0.0; // Option #1 float xoff = yoff; // Option #2
for (int i = 0; i < yvalues.length; i++) { // Using 2D noise function //yvalues[i] = (2*noise(xoff,yoff)-1)*amplitude; // Option #1 // Using 1D noise function yvalues[i] = (2*noise(xoff)-1)*amplitude; // Option #2 xoff+=dx; }
}
void renderWave() { // A simple way to draw the wave with an ellipse at each location for (int x = 0; x < yvalues.length; x++) { noStroke(); fill(255,50); ellipseMode(CENTER); ellipse(x*xspacing,width/2+yvalues[x],16,16); } }
float r;
// Angle and angular velocity, accleration float theta; float theta_vel; float theta_acc;
void setup() { size(200,200); frameRate(30); smooth(); // Initialize all values r = 50.0f; theta = 0.0f; theta_vel = 0.0f; theta_acc = 0.0001f; }
void draw() { background(0); // Translate the origin point to the center of the screen translate(width/2,height/2); // Convert polar to cartesian float x = r * cos(theta); float y = r * sin(theta); // Draw the ellipse at the cartesian coordinate ellipseMode(CENTER); noStroke(); fill(200); ellipse(x,y,16,16); // Apply acceleration and velocity to angle (r remains static in this example) theta_vel += theta_acc; theta += theta_vel;
}
2006年11月16日(木) 11:46:56 Modified by notarejini06