topical media & game development
graphic-processing-algorithm-Ch04-p96-p96.pde / pde
int numShapes = 12; //num on side of grid of shapes
MyShape[] shape = new MyShape[numShapes*numShapes];
MyGroup group;
MyControl control;
//*****************************************
void setup(){
size(300,300);
for(int y=0; y<numShapes; y++){
for(int x=0; x<numShapes; x++){
shape[y*numShapes+x] = new MyShape(5,9.,30+x*20., 30+y*20.);
}
}
group = new MyGroup(numShapes*numShapes, shape);
group.move(10.,10.);
control = new MyControl();
}
//*****************************************
void draw( ){
background(255);
group.draw();
}
int xfirst, yfirst;
//*****************************************
void mousePressed(){
xfirst = mouseX; // remember this point
yfirst = mouseY;
// Pick a shape
for(int i=0; i<group.numShapes; i++)
if(group.shapes[i].select((float)mouseX, (float)mouseY, 5.) == true){
println("Selected = " + i);
break;
}
}
//*****************************************
void mouseDragged(){
int xoff = mouseX - pmouseX; // get the offset
int yoff = mouseY - pmouseY;
MyPoint ref = new MyPoint(0.,0.);
for(int i=0; i<group.numShapes; i++)
if(group.shapes[i].isSelected){
ref = group.shapes[i].centroid();
if(control.status.equals("Move"))
group.shapes[i].move(( float)xoff, ( float)yoff);
if(control.status.equals("Rotate"))
group.shapes[i].rotate(( float)xoff, ref);
if(control.status.equals("Scale"))
group.shapes[i].scale((float)mouseX/(float)xfirst, (float)mouseY/(float)yfirst, ref);
}
}
(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.