LJ/examples/Processing/squares/squares.pde

68 lines
2.8 KiB
Plaintext

import redis.clients.jedis.Jedis;
import java.util.ArrayList;
ArrayList<PVector> points = new ArrayList<PVector>();
int rgb2int(int r, int g, int b) {
return (r << 16) + (g << 8) + b;
}
void setup() {
size(400, 200);
noStroke();
rectMode(CENTER);
}
void draw() {
background(51);
stroke(255, 204);
// Draw first rectangle using lines
noFill();
int rect1Width = mouseY/2+10;
int rect1Height = mouseY/2+10;
line(mouseX-rect1Width/2, height/2-rect1Height/2, mouseX+rect1Width/2, height/2-rect1Height/2);
line(mouseX+rect1Width/2, height/2-rect1Height/2, mouseX+rect1Width/2, height/2+rect1Height/2);
line(mouseX+rect1Width/2, height/2+rect1Height/2, mouseX-rect1Width/2, height/2+rect1Height/2);
line(mouseX-rect1Width/2, height/2+rect1Height/2, mouseX-rect1Width/2, height/2-rect1Height/2);
points.add(new PVector(mouseX-rect1Width/2, height/2-rect1Height/2));
points.add(new PVector(mouseX+rect1Width/2, height/2-rect1Height/2));
points.add(new PVector(mouseX+rect1Width/2, height/2+rect1Height/2));
points.add(new PVector(mouseX-rect1Width/2, height/2+rect1Height/2));
points.add(new PVector(mouseX-rect1Width/2, height/2-rect1Height/2));
// Draw second rectangle using lines
int inverseX = width-mouseX;
int inverseY = height-mouseY;
int rect2Width = (inverseY/2)+10;
int rect2Height = (inverseY/2)+10;
line(inverseX-rect2Width/2, height/2-rect2Height/2, inverseX+rect2Width/2, height/2-rect2Height/2);
line(inverseX+rect2Width/2, height/2-rect2Height/2, inverseX+rect2Width/2, height/2+rect2Height/2);
line(inverseX+rect2Width/2, height/2+rect2Height/2, inverseX-rect2Width/2, height/2+rect2Height/2);
line(inverseX-rect2Width/2, height/2+rect2Height/2, inverseX-rect2Width/2, height/2-rect2Height/2);
points.add(new PVector(inverseX-rect2Width/2, height/2-rect2Height/2));
points.add(new PVector(inverseX+rect2Width/2, height/2-rect2Height/2));
points.add(new PVector(inverseX+rect2Width/2, height/2+rect2Height/2));
points.add(new PVector(inverseX-rect2Width/2, height/2+rect2Height/2));
points.add(new PVector(inverseX-rect2Width/2, height/2-rect2Height/2));
if (points.size() > 0) {
println("point size : "+points.size());
println("pointlist");
String pointList = "["; // initialize the point list string
for (int i = 0; i < points.size(); i++) {
PVector p = points.get(i);
int[] rgb = {255, 255, 255};
pointList += "(" + p.x + "," + p.y + "," + rgb2int(rgb[0], rgb[1], rgb[2]) + "),";
}
points.clear();
pointList = pointList.substring(0, pointList.length() - 1); // remove the last comma
pointList += "]"; // close the point list string
System.out.println(pointList);
Jedis jedis = new Jedis("localhost");
jedis.set("/pl/0/0", pointList);
jedis.close();
}
}