513
edits
|  (Created page with "<syntaxhighlight lang="javaScript">     </syntaxhighlight>") | No edit summary | ||
| Line 1: | Line 1: | ||
| <syntaxhighlight lang="javaScript"> | <syntaxhighlight lang="javaScript"> | ||
| import processing.video.*; | |||
| Capture video; | |||
| int x, y; | |||
| Mover [] mover = new Mover [20]; | |||
| void setup() { | |||
|   size(640, 480); | |||
|   video = new Capture(this, width, height); | |||
|   video.start(); | |||
|   for (int i = 0; i < mover.length; i++) { | |||
|     mover [i]=new Mover (); | |||
|   } | |||
| } | |||
| void draw() { | |||
|   if (video.available()) { | |||
|     video.read(); | |||
|     image(video, 0, 0, width, height); | |||
|     int brightestX = 0; // X-coordinate of the brightest video pixel | |||
|     int brightestY = 0; // Y-coordinate of the brightest video pixel | |||
|     float brightestValue = 0; // Brightness of the brightest video pixel | |||
|     video.loadPixels(); | |||
|     int index = 0; | |||
|     for (int y = 0; y < video.height; y++) { | |||
|       for (int x = 0; x < video.width; x++) { | |||
|         int pixelValue = video.pixels[index]; | |||
|         float pixelBrightness = brightness(pixelValue); | |||
|         if (pixelBrightness > brightestValue) { | |||
|           brightestValue = pixelBrightness; | |||
|           brightestY = y; | |||
|           brightestX = x; | |||
|         } | |||
|         index++; | |||
|       } | |||
|     } | |||
|     for (int j = 0; j < mover.length; j++) { | |||
|       // Update the position | |||
|       mover[j].update(brightestX, brightestY); | |||
|       // Display the Mover | |||
|       mover[j].display(); | |||
|     } | |||
|   } | |||
| } | |||
| // The Nature of Code | |||
| // Daniel Shiffman | |||
| // http://natureofcode.com | |||
| class Mover { | |||
|   // The Mover tracks position, velocity, and acceleration  | |||
|   PVector position; | |||
|   PVector velocity; | |||
|   PVector dir; | |||
|   // The Mover's maximum speed | |||
|   float topspeed; | |||
|   Mover() { | |||
|     // Start in the center | |||
|     position = new PVector(width/2,height/2); | |||
|     velocity = new PVector(0,0); | |||
|     topspeed = 5; | |||
|   } | |||
|   void update(float x, float y) { | |||
|     // Compute a vector that points from position to mouse | |||
|     PVector mouse = new PVector(x,y); | |||
|     PVector dir = PVector.sub(mouse,position); | |||
|     // Set magnitude of acceleration | |||
|     dir.setMag(0.3); | |||
|     // Velocity changes according to acceleration | |||
|     velocity.add(dir); | |||
|     // Limit the velocity by topspeed | |||
|     velocity.limit(topspeed); | |||
|     // position changes by velocity | |||
|     position.add(velocity); | |||
|   } | |||
|   void display() { | |||
|     stroke(0); | |||
|     strokeWeight(2); | |||
|     //fill(127); | |||
|     //ellipse(position.x,position.y,48,48); | |||
|     line (position.x,position.y, position.x+velocity.x,position.y+velocity.y); | |||
|   } | |||
| } | |||
| </syntaxhighlight> | </syntaxhighlight> | ||
edits