Skip to content

Commit

Permalink
Modified colordetect example with kinect and processing
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshredcobra committed Oct 28, 2012
1 parent cc2b2d9 commit 7377fd6
Show file tree
Hide file tree
Showing 9 changed files with 606 additions and 0 deletions.
37 changes: 37 additions & 0 deletions kinect_codes/color_detect/Catcher.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Catcher {
float r; // radius
color col; // color
float x,y; // location

Catcher(float tempR) {
r = tempR;
col = color(50,10,10,150);
x = 0;
y = 0;
}

void setLocation(float tempX, float tempY) {
x = tempX;
y = tempY;
}

void display() {
stroke(0);
fill(col);
ellipse(x,y,r*2,r*2);
}

// A function that returns true or false based on
// if the catcher intersects a raindrop
boolean intersect(Drop d) {
// Calculate distance
float distance = dist(x,y,d.x,d.y);

// Compare distance to sum of radii
if (distance < r + d.r) {
return true;
} else {
return false;
}
}
}
48 changes: 48 additions & 0 deletions kinect_codes/color_detect/Drop.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Drop {
float x,y; // Variables for location of raindrop
float speed; // Speed of raindrop
color c;
float r; // Radius of raindrop

Drop() {
r = 8; // All raindrops are the same size
x = random(width); // Start with a random x location
y = -r*4; // Start a little above the window
speed = random(1,5); // Pick a random speed
c = color(50,100,150); // Color
}

// Move the raindrop down
void move() {
// Increment by speed
y += speed;
}

// Check if it hits the bottom
boolean reachedBottom() {
// If we go a little beyond the bottom
if (y > height + r*4) {
return true;
} else {
return false;
}
}

// Display the raindrop
void display() {
// Display the drop
fill(c);
noStroke();
for (int i = 2; i < r; i++ ) {
ellipse(x,y + i*4,i*2,i*2);
}
}

// If the drop is caught
void caught() {
// Stop it from moving by setting speed equal to zero
speed = 0;
// Set the location to somewhere way off-screen
y = - 1000;
}
}
87 changes: 87 additions & 0 deletions kinect_codes/color_detect/Ribbon.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class Ribbon
{
int ribbonAmount;
float randomness;
int ribbonParticleAmount; // length of the Particle Array (max number of points)
int particlesAssigned = 0; // current amount of particles currently in the Particle array
float radiusMax = 8; // maximum width of ribbon
float radiusDivide = 10; // distance between current and next point / this = radius for first half of the ribbon
float gravity = .03; // gravity applied to each particle
float friction = 1.1; // friction applied to the gravity of each particle
int maxDistance = 40; // if the distance between particles is larger than this the drag comes into effect
float drag = 2; // if distance goes above maxDistance - the points begin to grag. high numbers = less drag
float dragFlare = .008; // degree to which the drag makes the ribbon flare out
RibbonParticle[] particles; // particle array
color ribbonColor;

Ribbon(int ribbonParticleAmount, color ribbonColor, float randomness)
{
this.ribbonParticleAmount = ribbonParticleAmount;
this.ribbonColor = ribbonColor;
this.randomness = randomness;
init();
}

void init()
{
particles = new RibbonParticle[ribbonParticleAmount];
}

void update(float randX, float randY)
{
addParticle(randX, randY);
drawCurve();
}

void addParticle(float randX, float randY)
{
if(particlesAssigned == ribbonParticleAmount)
{
for (int i = 1; i < ribbonParticleAmount; i++)
{
particles[i-1] = particles[i];
}
particles[ribbonParticleAmount - 1] = new RibbonParticle(randomness, this);
particles[ribbonParticleAmount - 1].px = randX;
particles[ribbonParticleAmount - 1].py = randY;
return;
}
else
{
particles[particlesAssigned] = new RibbonParticle(randomness, this);
particles[particlesAssigned].px = randX;
particles[particlesAssigned].py = randY;
++particlesAssigned;
}
if (particlesAssigned > ribbonParticleAmount) ++particlesAssigned;
}

void drawCurve()
{
smooth();
for (int i = 1; i < particlesAssigned - 1; i++)
{
RibbonParticle p = particles[i];
p.calculateParticles(particles[i-1], particles[i+1], ribbonParticleAmount, i);
}

fill(30);
for (int i = particlesAssigned - 3; i > 1 - 1; i--)
{
RibbonParticle p = particles[i];
RibbonParticle pm1 = particles[i-1];
fill(ribbonColor, 255);
if (i < particlesAssigned-3)
{
noStroke();
beginShape();
vertex(p.lcx2, p.lcy2);
bezierVertex(p.leftPX, p.leftPY, pm1.lcx2, pm1.lcy2, pm1.lcx2, pm1.lcy2);
vertex(pm1.rcx2, pm1.rcy2);
bezierVertex(p.rightPX, p.rightPY, p.rcx2, p.rcy2, p.rcx2, p.rcy2);
vertex(p.lcx2, p.lcy2);
endShape();
}
}
}
}
58 changes: 58 additions & 0 deletions kinect_codes/color_detect/RibbonManager.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class RibbonManager
{
PImage img;
int ribbonAmount;
int ribbonParticleAmount;
float randomness;
String imgName;
Ribbon[] ribbons; // ribbon array

RibbonManager(int ribbonAmount, int ribbonParticleAmount, float randomness, String imgName)
{
this.ribbonAmount = ribbonAmount;
this.ribbonParticleAmount = ribbonParticleAmount;
this.randomness = randomness;
this.imgName = imgName;
init();
}

void init()
{
img = loadImage(imgName);
addRibbon();
}

void addRibbon()
{
ribbons = new Ribbon[ribbonAmount];
for (int i = 0; i < ribbonAmount; i++)
{
int xpos = int(random(img.width));
int ypos = int(random(img.height));
color ribbonColor = img.get(xpos, ypos);
ribbons[i] = new Ribbon(ribbonParticleAmount, ribbonColor, randomness);
}
}

void update(int currX, int currY)
{
for (int i = 0; i < ribbonAmount; i++)
{
//float randX = currX + (randomness / 2) - random(randomness);
//float randY = currY + (randomness / 2) - random(randomness);

float randX = currX;
float randY = currY;

ribbons[i].update(randX, randY);
}
}

void setRadiusMax(float value) { for (int i = 0; i < ribbonAmount; i++) { ribbons[i].radiusMax = value; } }
void setRadiusDivide(float value) { for (int i = 0; i < ribbonAmount; i++) { ribbons[i].radiusDivide = value; } }
void setGravity(float value) { for (int i = 0; i < ribbonAmount; i++) { ribbons[i].gravity = value; } }
void setFriction(float value) { for (int i = 0; i < ribbonAmount; i++) { ribbons[i].friction = value; } }
void setMaxDistance(int value) { for (int i = 0; i < ribbonAmount; i++) { ribbons[i].maxDistance = value; } }
void setDrag(float value) { for (int i = 0; i < ribbonAmount; i++) { ribbons[i].drag = value; } }
void setDragFlare(float value) { for (int i = 0; i < ribbonAmount; i++) { ribbons[i].dragFlare = value; } }
}
99 changes: 99 additions & 0 deletions kinect_codes/color_detect/RibbonParticle.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
class RibbonParticle
{
float px, py; // x and y position of particle (this is the bexier point)
float xSpeed, ySpeed = 0; // speed of the x and y positions
float cx1, cy1, cx2, cy2; // the avarage x and y positions between px and py and the points of the surrounding Particles
float leftPX, leftPY, rightPX, rightPY; // the x and y points of that determine the thickness of this segment
float lpx, lpy, rpx, rpy; // the x and y points of the outer bezier points
float lcx1, lcy1, lcx2, lcy2; // the avarage x and y positions between leftPX and leftPX and the left points of the surrounding Particles
float rcx1, rcy1, rcx2, rcy2; // the avarage x and y positions between rightPX and rightPX and the right points of the surrounding Particles
float radius; // thickness of current particle
float randomness;
Ribbon ribbon;

RibbonParticle(float randomness, Ribbon ribbon)
{
this.randomness = randomness;
this.ribbon = ribbon;
}

void calculateParticles(RibbonParticle pMinus1, RibbonParticle pPlus1, int particleMax, int i)
{
float div = 2;
cx1 = (pMinus1.px + px) / div;
cy1 = (pMinus1.py + py) / div;
cx2 = (pPlus1.px + px) / div;
cy2 = (pPlus1.py + py) / div;

// calculate radians (direction of next point)
float dx = cx2 - cx1;
float dy = cy2 - cy1;

float pRadians = atan2(dy, dx);

float distance = sqrt(dx*dx + dy*dy);

if (distance > ribbon.maxDistance) // && i > 1
{
float oldX = px;
float oldY = py;
px = px + ((ribbon.maxDistance/ribbon.drag) * cos(pRadians));
py = py + ((ribbon.maxDistance/ribbon.drag) * sin(pRadians));
xSpeed += (px - oldX) * ribbon.dragFlare;
ySpeed += (py - oldY) * ribbon.dragFlare;
}

ySpeed += ribbon.gravity;
xSpeed *= ribbon.friction;
ySpeed *= ribbon.friction;
px += xSpeed + random(.3);
py += ySpeed + random(.3);

float randX = ((randomness / 2) - random(randomness)) * distance;
float randY = ((randomness / 2) - random(randomness)) * distance;
px += randX;
py += randY;

//float radius = distance / 2;
//if (radius > radiusMax) radius = ribbon.radiusMax;

if (i > particleMax / 2)
{
radius = distance / ribbon.radiusDivide;
}
else
{
radius = pPlus1.radius * .9;
}

if (radius > ribbon.radiusMax) radius = ribbon.radiusMax;
if (i == particleMax - 2 || i == 1)
{
if (radius > 1) radius = 1;
}

// calculate the positions of the particles relating to thickness
leftPX = px + cos(pRadians + (HALF_PI * 3)) * radius;
leftPY = py + sin(pRadians + (HALF_PI * 3)) * radius;
rightPX = px + cos(pRadians + HALF_PI) * radius;
rightPY = py + sin(pRadians + HALF_PI) * radius;

// left and right points of current particle
lpx = (pMinus1.lpx + lpx) / div;
lpy = (pMinus1.lpy + lpy) / div;
rpx = (pPlus1.rpx + rpx) / div;
rpy = (pPlus1.rpy + rpy) / div;

// left and right points of previous particle
lcx1 = (pMinus1.leftPX + leftPX) / div;
lcy1 = (pMinus1.leftPY + leftPY) / div;
rcx1 = (pMinus1.rightPX + rightPX) / div;
rcy1 = (pMinus1.rightPY + rightPY) / div;

// left and right points of next particle
lcx2 = (pPlus1.leftPX + leftPX) / div;
lcy2 = (pPlus1.leftPY + leftPY) / div;
rcx2 = (pPlus1.rightPX + rightPX) / div;
rcy2 = (pPlus1.rightPY + rightPY) / div;
}
}
27 changes: 27 additions & 0 deletions kinect_codes/color_detect/Timer.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Timer {

int savedTime; // When Timer started
int totalTime; // How long Timer should last

Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}

// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}

// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
Loading

0 comments on commit 7377fd6

Please sign in to comment.