topical media & game development
graphic-processing-algorithm-Ch02-p54-p54.pde / pde
int n = 5;
float[] px = new float[n];
float[] py = new float[n];
void setup(){
float angle = 2 * PI / n; //divide the circle in n sect
for(int i =0; i< n; i++){
px[i] = 50. + 30. * sin(angle*i);
py[i] = 50. + 30. * cos(angle*i);
}
rectMode(CENTER);
}
void draw (){
background(200);
beginShape(POLYGON);
for(int i = 0; i < n; i++)
vertex(px[i],py[i]);
endShape(CLOSE);
for(int i=0; i<n; i++){
if(dist(mouseX,mouseY,px[i],py[i])<20)
stroke(255,0,0);
else
stroke(0,0,0);
rect(px[i],py[i],5,5);
}
}
void mouseDragged(){
for(int i=0; i<n; i++)
if(dist(mouseX,mouseY,px[i],py[i])<20){
px[i] += (mouseX-pmouseX); //push only
py[i] += (mouseY-pmouseY);
px[i] = constrain(px[i],5,width-5);
py[i] = constrain(py[i],5,height-5);
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.