Rotation of the entire code/impossible to do
in
Programming Questions
•
1 month ago
Hello guys I have this program and I only need to make all the squares rotate 45*
is it possible to leave the old squares in the screen and in top of them the new rotated squares?
thanks
and I have 2 classes too, I am new at this and it is very cool
//squares stores the 9 squares in a 3x3 array
nSquare[][] squares = new nSquare[3][3];
//set square size and grid spacing
int square_size = 90;
int grid_spacing = 10;
//nSquare stores the currently selected square
nSquare selected_square;
boolean square_selected = false;
//set snaps and grid visibility
boolean snap_to_grid = true;
boolean show_grid = true;
//sets rules about deformation
boolean global_deform = true;
float global_fact = .5;
//global ints indicate square sides with a number
int TOP_SIDE = 1;
int RIGHT_SIDE = 2;
int BOTTOM_SIDE = 3;
int LEFT_SIDE = 4;
void setup(){
size(9*square_size, 9*square_size);
createSquares();
rotate(HALF_PI);
}
void draw(){
background(0);
stroke(255);
//render grid
if(show_grid){
draw_grid();
}
//render squares
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
squares[i][j].render();
}
}
//render the selected square last, so color shows on top of others
selected_square.render();
}
void createSquares(){
for(int i = 0; i<3; i++){
//set y coordinate for top of squares
int y_top = (3+i)*square_size;
for(int j = 0; j<3; j++){
//set x coordinate for left side of squares
int x_left = (3+j)*square_size;
//create square and add to squares array
nSquare s = new nSquare(y_top, x_left, square_size, i, j);
squares[i][j] = s;
//initially set the selected_square to upper left
if((i==0)&(j==0)){
selected_square = s;
}
}
}
}
//render the grid
void draw_grid(){
strokeWeight(.001);
stroke(25);
int x_coord = grid_spacing;
while(x_coord<width){
line(x_coord, 0, x_coord, height);
x_coord = x_coord + grid_spacing;
}
int y_coord = grid_spacing;
while(y_coord<height){
line(0, y_coord, width, y_coord);
y_coord = y_coord + grid_spacing;
}
}
void mousePressed() {
//get mouse coordinates
sPoint mousePos = new sPoint(mouseX, mouseY);
//if a square is already selected
if(square_selected){
//deal with snapping
if(snap_to_grid){
int x_remainder = mouseX % grid_spacing;
if(x_remainder<=(grid_spacing/2)){
mousePos.x = mousePos.x - x_remainder;
}else{
mousePos.x = mousePos.x - x_remainder + grid_spacing;
}
int y_remainder = mouseY % grid_spacing;
if(y_remainder<=(grid_spacing/2)){
mousePos.y = mousePos.y - y_remainder;
}else{
mousePos.y = mousePos.y - y_remainder + grid_spacing;
}
}
//move the side of the square
selected_square.move_side(mousePos);
//setup for global deformation
sPoint deform = selected_square.deform;
int selected_sum = selected_square.sum;
int current_side = 0;
if(selected_square.top_side_selected){
current_side = TOP_SIDE;
}else if(selected_square.right_side_selected){
current_side = RIGHT_SIDE;
}else if(selected_square.bottom_side_selected){
current_side = BOTTOM_SIDE;
}else if(selected_square.left_side_selected){
current_side = LEFT_SIDE;
}
if(global_deform){
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
nSquare s = squares[i][j];
if(s!=selected_square){
float p_fact = abs(s.sum-selected_sum);
float fact = pow(global_fact, p_fact);
sPoint t_deform = new sPoint(deform.x, deform.y);
t_deform.x = deform.x*fact;
t_deform.y = deform.y*fact;
s.globalDeform(t_deform, current_side);
}
}
}
}
}else{
//iterate through squares and see if any contain the mouse
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
nSquare s = squares[i][j];
s.contains(mousePos);
if(s.selected){
selected_square = s;
square_selected = true;
}
}
}
}
}
void keyPressed(){
//deselect all squares and sides
if(key == 'd'){
square_selected = false;
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
nSquare s = squares[i][j];
s.selected = false;
s.top_side_selected = false;
s.right_side_selected = false;
s.bottom_side_selected = false;
s.left_side_selected = false;
}
}
}
//save a tif of the current scene
if(key == 's'){
saveFrame(frameCount + ".tif");
}
}
class nSquare{
sPoint upper_left;
sPoint upper_right;
sPoint lower_left;
sPoint lower_right;
int square_size;
boolean selected = false;
boolean top_side_selected = false;
boolean right_side_selected = false;
boolean bottom_side_selected = false;
boolean left_side_selected = false;
int TOP_SIDE = 1;
int RIGHT_SIDE = 2;
int BOTTOM_SIDE = 3;
int LEFT_SIDE = 4;
int i;
int j;
int sum;
sPoint deform = new sPoint(0,0);
//Constructor stores the x and y coordinates of the upper left corner, the square size
//along with the i, j locations in the squares array
nSquare(int x, int y, int _square_size, int _i, int _j){
i = _i;
j = _j;
sum = i + j;
square_size = _square_size;
//create four corners
upper_left = new sPoint(x, y);
upper_right = new sPoint(x + square_size, y);
lower_left = new sPoint(x, y + square_size);
lower_right = new sPoint(x + square_size, y + square_size);
}
void render(){
strokeWeight(.001);
//render sides
//if side is selected, draw it in red
stroke(100);
if(top_side_selected){
stroke(255,0,0);
}
line(upper_left.x, upper_left.y, upper_right.x, upper_right.y);
stroke(100);
if(right_side_selected){
stroke(255,0,0);
}
line(upper_right.x, upper_right.y, lower_right.x, lower_right.y);
stroke(100);
if(bottom_side_selected){
stroke(255,0,0);
}
line(lower_right.x, lower_right.y, lower_left.x, lower_left.y);
stroke(100);
if(left_side_selected){
stroke(255,0,0);
}
line(lower_left.x, lower_left.y, upper_left.x, upper_left.y);
stroke(100);
//render corners
upper_left.render();
upper_right.render();
lower_left.render();
lower_right.render();
}
void contains(sPoint p){
//check whether p is inside of the square
if((p.x>upper_left.x)&(p.y>upper_left.y)&(p.x<lower_right.x)&(p.y<lower_right.y)){
selected = true;
//find which side is closest to the point
float min_dist = 10000;
//look at top side
float top_dist = p.y-upper_left.y;
if(top_dist<=min_dist){
min_dist = top_dist;
}
//look at right side
float right_dist = upper_right.x-p.x;
if(right_dist<=min_dist){
min_dist = right_dist;
}
//look at bottom side
float bottom_dist = lower_left.y - p.y;
if(bottom_dist<=min_dist){
min_dist = bottom_dist;
}
//look at right side
float left_dist = p.x-lower_left.x;
if(left_dist<=min_dist){
min_dist = left_dist;
}
//set which side is selected
if(top_dist<=min_dist){
top_side_selected = true;
}else{
top_side_selected = false;
}
if(right_dist<=min_dist){
right_side_selected = true;
}else{
right_side_selected = false;
}
if(bottom_dist<=min_dist){
bottom_side_selected = true;
}else{
bottom_side_selected = false;
}
if(left_dist<=min_dist){
left_side_selected = true;
}else{
left_side_selected = false;
}
}else{
//if point isn't inside square, set everything to false
selected = false;
top_side_selected = false;
right_side_selected = false;
bottom_side_selected = false;
left_side_selected = false;
}
}
//global deformation function
void globalDeform(sPoint p, int side){
if(side == TOP_SIDE){
//upper_left.y = upper_left.y-p.y;
//upper_right.y = upper_right.y-p.y;
upper_left.y = upper_left.y+p.y;
upper_right.y = upper_right.y+p.y;
}else if(side == RIGHT_SIDE){
//upper_right.x = upper_right.x-p.x;
//lower_right.x = lower_right.x-p.x;
upper_right.x = upper_right.x+p.x;
lower_right.x = lower_right.x+p.x;
}else if(side == BOTTOM_SIDE){
//lower_left.y = lower_left.y-p.y;
//lower_right.y = lower_right.y-p.y;
lower_left.y = lower_left.y+p.y;
lower_right.y = lower_right.y+p.y;
}else if(side == LEFT_SIDE){
//upper_left.x = upper_left.x-p.x;
//lower_left.x = lower_left.x-p.x;
upper_left.x = upper_left.x+p.x;
lower_left.x = lower_left.x+p.x;
}
}
//move the selected side
void move_side(sPoint p){
if(top_side_selected){
deform = new sPoint(0, p.y - upper_left.y);
upper_left.y = p.y;
upper_right.y = p.y;
}
if(right_side_selected){
deform = new sPoint(p.x - upper_right.x, 0);
upper_right.x = p.x;
lower_right.x = p.x;
}
if(bottom_side_selected){
deform = new sPoint(0, p.y - lower_left.y);
lower_left.y = p.y;
lower_right.y = p.y;
}
if(left_side_selected){
deform = new sPoint(p.x - upper_left.x, 0);
upper_left.x = p.x;
lower_left.x = p.x;
}
}
}
class sPoint{
float x;
float y;
//different constructors for dealing with floats and ints
sPoint(int _x, int _y){
x = _x;
y = _y;
}
sPoint(float _x, int _y){
x = _x;
y = _y;
}
sPoint(int _x, float _y){
x = _x;
y = _y;
}
sPoint(float _x, float _y){
x = _x;
y = _y;
}
void render(){
stroke(255);
strokeWeight(4);
point(x, y);
}
}
1