I want to create a grid of polyhedron and then rotate them by interacting with mouse. However, the highlight part is what I want to rotate each polyhedron by its center.
Code:int n = 24;
MyShape[] shape = new MyShape[n*n];
void setup(){
size(750,750); //make the screen big enough to see
for(int y=0; y<n; y++){ //for 12 steps in y
for(int x=0; x<n; x++){ //for 12 steps in x
//make a shape (calling the polygon constructor)
shape[y*n+x] = new MyShape(3, 10.,x*20., y*20.);
shape[y*n+x].move(10.*x, 10.*y);
shape[y*n+x].move(30.,30.);
}
}
}
void draw(){
background(255);
for(int y=0; y<n; y++){ //for 12 steps in y
for(int x=0; x<n; x++){ //for 12 steps in x
shape[y*n+x].plot(); // plot the shapes
}
}
}
MyPoint[] ref = new MyPoint[n*n];
void mouseDragged(){
for(int y=0; y<n; y++){ //for 12 steps in y
for(int x=0; x<n; x++){ //for 12 steps in x
ref[y*n+x].x = x;
ref[y*n+x].y = y;
//println(ref[y*n+x]);
int xoff = mouseX - pmouseX;
int yoff = mouseY - pmouseY;
shape[y*n+x].rotate(xoff, ref[y*n+x]);
shape[y*n+x].rotate(yoff, ref[y*n+x]);
}
}
}
class MyPoint {
float x, y; // members of class
//Constructor
MyPoint(float xin, float yin){
x = xin;
y = yin;
}
//Move
void move(float xoff, float yoff){
x += xoff;
y += yoff;
}
//Rotate
void rotate(float angle, MyPoint ref){
float cosa, sina;
cosa = cos(radians(angle));
sina = sin(radians(angle));
float newx = (x-ref.x) * cosa - (y-ref.y) * sina + ref.x;
float newy = (y-ref.y) * cosa + (x-ref.x) * sina + ref.y;
x = newx;
y = newy;
}
//Scale
void scale(float xs, float ys, MyPoint ref){
x = (x-ref.x)*xs + ref.x;
y = (y-ref.y)*ys + ref.y;
}
}
class MySegment {
MyPoint start; // members of class
MyPoint end;
//Constructor
MySegment(MyPoint p1, MyPoint p2){
start = new MyPoint(p1.x, p1.y);
end = new MyPoint(p2.x, p2.y);
}
//Move
void move(float xoff, float yoff){
start.move(xoff, yoff);
end.move(xoff, yoff);
}
//Rotate
void rotate (float angle, MyPoint ref) {
start.rotate(angle, ref);
end.rotate(angle, ref);
}
//Scale
void scale(float xs, float ys, MyPoint ref){
start.scale(xs, ys, ref);
end.scale(xs, ys, ref);
}
// plot
void plot (){
line(start.x, start.y, end.x, end.y);
}
}
class MyShape {
MySegment[] segs; // members of class
int numSegments;
//Constructor
MyShape(int numInputSegments, MySegment[] inputSegments){
numSegments = numInputSegments;
segs = new MySegment[numSegments];
for(int i=0; i<numSegments; i++)
segs[i] = inputSegments[i];
}
MyShape(int numSides, float radius, float xoff, float yoff){
numSegments = numSides;
segs = new MySegment[numSegments];
// divide the full circle in nsides sections
float angle = 2 * (float)Math.PI / numSegments;
// create two points to store the segment points
MyPoint p = new MyPoint(0.,0.);
MyPoint pnext = new MyPoint(0.,0.);
// loop to assign values to the points
for(int i =0; i<numSegments; i++){
p.x = xoff + radius * sin(angle*i);
p.y = yoff + radius * cos(angle*i);
pnext.x = xoff + radius * sin(angle*(i+1));
pnext.y = yoff + radius * cos(angle*(i+1));
segs[i] = new MySegment(p, pnext);
}
}
// Move
void move(float xoff, float yoff){
for(int i=0; i<numSegments; i++)
segs[i].move(xoff, yoff);
}
// Rotate
void rotate (float angle, MyPoint ref) {
for(int i=0; i<numSegments; i++)
segs[i].rotate(angle, ref);
}
// Scale
void scale(float xs, float ys, MyPoint ref){
for(int i=0; i<numSegments; i++)
segs[i].scale(xs, ys, ref);
}
// Plot
void plot(){
for(int i=0; i<numSegments; i++)
segs[i].plot();
}
}