Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Painting Mirror • Pixel by Pixel Final Project, NYU ITP Spring 2018
/*
Painting Mirror by Ellen Nickles
Final Project for The World, Pixel by Pixel with Danny Rozin
ITP NYU Spring 2018
my process: https://ellennickles.com/itpblog/2018/5/9/week-14-painting-mirror
mouse clicking from: https://processing.org/discourse/beta/num_1171457639.html
point checking from: https://forum.processing.org/one/topic/mouse-within-a-certain-area.html
*/
import java.util.*;
import processing.video.*;
Capture camera0;
Capture camera1;
int amount = 10;
Polygon0[] polygons0 = new Polygon0[amount];
Polygon1[] polygons1 = new Polygon1[amount];
int x, y, a;
int clickTime = 0;
int clickSpeed = 200;
boolean clicked = false;
void setup() {
//fullScreen(P3D); // use on laptop for testing
//size(640, 480, P3D); // recommended for ITP Planar testing
// recommended for ITP Planar & Logitech C920 and C922 webcams
// if using Windows laptop, set screen to same resolution at 125% enlargement
size(1920, 1080, P3D);
noCursor();
printArray(Capture.list());
// initialize and start the cameras
camera0 = new Capture(this, width, height, "C922 Pro Stream Webcam"); // C922
camera0.start();
camera1 = new Capture(this, width, height, "Logitech HD Pro Webcam C920"); // C920
camera1.start();
// create new polygons
for (int i = 0; i < polygons0.length; i++) {
getValues();
polygons0[i] = new Polygon0(x, y, a);
}
for (int i = 0; i < polygons1.length; i++) {
getValues();
polygons1[i] = new Polygon1(x, y, a);
}
}
void captureEvent(Capture cam) {
cam.read();
}
void draw() {
// background camera
pushMatrix();
scale(-1, 1);
blendMode(LIGHTEST);
image(camera0, -width, 0);
popMatrix();
// display the polygons and update their positions if dragged
for (int i = 0; i < polygons0.length; i++) {
if (polygons0[i].dragging == true) {
polygons0[i].update(mouseX, mouseY);
}
polygons0[i].display();
}
for (int i = 0; i < polygons1.length; i++) {
if (polygons1[i].dragging == true) {
polygons1[i].update(mouseX, mouseY);
}
polygons1[i].display();
}
if (clicked && (millis() > clickTime + clickSpeed)) {
clicked = false;
println("<single click>");
clickTime = 0;
}
}
// generate X and Y values for polygon centroids
// and generate alpha values for each polygon
void getValues() {
x = int(random(width*.95));
y = int(random(height*.95));
a = int(random(160, 255));
}
void mousePressed() {
for (int i = 0; i < polygons0.length; i++) {
polygons0[i].pressed(mouseX, mouseY);
}
for (int i = 0; i < polygons1.length; i++) {
polygons1[i].pressed(mouseX, mouseY);
}
}
void mouseReleased() {
for (int i = 0; i < polygons0.length; i++) {
polygons0[i].notPressed();
}
for (int i = 0; i < polygons1.length; i++) {
polygons1[i].notPressed();
}
}
void mouseClicked() {
clicked = false;
// take a screen shot if a double-click is detected
if (mouseEvent.getClickCount() == 1) {
clicked = true;
clickTime = millis();
} else if (mouseEvent.getClickCount() == 2) {
println("<double click>");
Date d = new Date();
save("data/screen_" + d.getTime() +".jpg");
clickTime = 0;
}
}
void keyPressed() {
// take a screenshot if the "s" key is pressed
if (key == 's') {
Date d = new Date();
save("data/screen_" + d.getTime() +".jpg");
}
// add or remove polygons sourced from camera0 feed
if (key == CODED) {
if (keyCode == UP) {
getValues();
Polygon0 r = new Polygon0(x, y, a);
polygons0 = (Polygon0[]) append(polygons0, r);
}
}
if (key == CODED) {
if (keyCode == DOWN) {
polygons0 = (Polygon0[]) shorten(polygons0);
}
}
// add or remove polygons sourced from camera1 feed
if (key == CODED) {
if (keyCode == RIGHT) {
getValues();
Polygon1 p = new Polygon1(x, y, a);
polygons1 = (Polygon1[]) append(polygons1, p);
}
}
if (key == CODED) {
if (keyCode == LEFT) {
polygons1 = (Polygon1[]) shorten(polygons1);
}
}
}
class Polygon0 {
int x, y, a, offsetX, offsetY;
boolean dragging;
float factor;
int[][] corners = new int[4][2];
PVector[] verts=new PVector[4];
Polygon0(int x, int y, int a) {
this.x = x;
this.y = y;
this.a = a;
this.offsetX = -int(this.x);
this.offsetY = -int(this.y);
this.dragging = false;
factor = random(0.1, 1);
for (int i = 0; i < 4; i++) {
int randint = (int)random (20, 260);
corners[i][0]= randint;
randint = (int)random (20, 260);
corners[i][1]= randint;
}
}
void update(int px, int py) {
this.x = width - px + this.offsetX;
this.y = py + this.offsetY;
}
void display() {
pushMatrix();
translate(width, 0);
scale(-1, 1);
stroke(0);
if (this.dragging) {
strokeWeight(3);
} else {
strokeWeight(1);
}
noFill();
beginShape();
texture(camera0);
verts[0]=new PVector(this.x- corners[0][0], this.y- corners[0][1]);
verts[1]=new PVector(this.x+ corners[1][0], this.y- corners[1][1]);
verts[2]=new PVector(this.x+ corners[2][0], this.y+ corners[2][1]);
verts[3]=new PVector(this.x- corners[3][0], this.y+ corners[3][1]);
for (PVector v : verts) {
vertex(v.x, v.y, 0, v.x*factor, v.y*factor);
}
tint(255, this.a);
blendMode(DARKEST);
endShape(CLOSE);
popMatrix();
}
void pressed(int px, int py) {
int mx = width-px;
if (containsPoint(verts, mx, py)) {
this.dragging = true;
this.offsetX = this.x - mx;
this.offsetY = this.y - py;
this.update(px, py);
}
}
void notPressed() {
this.dragging = false;
}
boolean containsPoint(PVector[] verts, float px, float py) {
int num = verts.length;
int i, j = num - 1;
boolean oddNodes = false;
for (i = 0; i < num; i++) {
PVector vi = verts[i];
PVector vj = verts[j];
if (vi.y < py && vj.y >= py || vj.y < py && vi.y >= py) {
if (vi.x + (py - vi.y) / (vj.y - vi.y) * (vj.x - vi.x) < px) {
oddNodes = !oddNodes;
}
}
j = i;
}
return oddNodes;
}
}
class Polygon1 {
int x, y, a, offsetX, offsetY;
boolean dragging;
float factor;
int[][] corners = new int[4][2];
PVector[] verts=new PVector[4];
Polygon1(int x, int y, int a) {
this.x = x;
this.y = y;
this.a = a;
this.offsetX = -int(this.x);
this.offsetY = -int(this.y);
this.dragging = false;
factor = random(0.1, 1);
for (int i = 0; i < 4; i++) {
int randint = (int)random (20, 260);
corners[i][0]= randint;
randint = (int)random (20, 260);
corners[i][1]= randint;
}
}
void update(int px, int py) {
this.x = width - px + this.offsetX;
this.y = py + this.offsetY;
}
void display() {
pushMatrix();
translate(width, 0);
scale(-1, 1);
stroke(0);
if (this.dragging) {
strokeWeight(3);
} else {
strokeWeight(1);
}
noFill();
beginShape();
texture(camera1);
verts[0]=new PVector(this.x- corners[0][0], this.y- corners[0][1]);
verts[1]=new PVector(this.x+ corners[1][0], this.y- corners[1][1]);
verts[2]=new PVector(this.x+ corners[2][0], this.y+ corners[2][1]);
verts[3]=new PVector(this.x- corners[3][0], this.y+ corners[3][1]);
for (PVector v : verts) {
vertex(v.x, v.y, 0, v.x*factor, v.y*factor);
}
tint(255, this.a);
blendMode(DARKEST);
endShape(CLOSE);
popMatrix();
}
void pressed(int px, int py) {
int mx = width-px;
if (containsPoint(verts, mx, py)) {
this.dragging = true;
this.offsetX = this.x - mx;
this.offsetY = this.y - py;
this.update(px, py);
}
}
void notPressed() {
this.dragging = false;
}
boolean containsPoint(PVector[] verts, float px, float py) {
int num = verts.length;
int i, j = num - 1;
boolean oddNodes = false;
for (i = 0; i < num; i++) {
PVector vi = verts[i];
PVector vj = verts[j];
if (vi.y < py && vj.y >= py || vj.y < py && vi.y >= py) {
if (vi.x + (py - vi.y) / (vj.y - vi.y) * (vj.x - vi.x) < px) {
oddNodes = !oddNodes;
}
}
j = i;
}
return oddNodes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment