Loading...
Logo
Processing Forum
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);
    
  }
  
  
  
}

Replies(16)

This is a duplicate thread, you should avoid to do this.
I will delete the older thread, since no significant answer has been given (sorry, Chrisir, as your short answer (and mine!) will be lost) and no code was provided.

Note: you don't rotate the code, only what it produces...
no I have to do this!
how?
com on!
As said in the old thread, you have to use translate as well as rotate():
Copy code
  1. void draw(){
  2.   background(0);
  3.   stroke(255);
  4.  
  5.   if (rotated) {
  6.     translate(width / 2, height / 2);
  7.     rotate(PI / 2);
  8.     translate(-width / 2, -height / 2);
  9.   }
  10.  
  11.   //render grid
  12.   if(show_grid){
  13.     draw_grid();
  14.   }
Copy code
  1. void keyPressed(){
  2.   // [...] skip old code
  3.   
  4.   if (key == 'r') rotated = !rotated;
  5. }
Copy code
  1. boolean rotated; // Global...
New code is in purple, with context given to show how to insert it.

it doesn't work
maybe Idk how to insert it
man thank you a lot
you are kind at the end ; )
I deleted the old thread, not this one. The old one gave no real useful information, and I prefer to avoid having people to answer to it (like I did!), doing useless work (since answers can be already in the other thread).

Also changed the topic type, since you are in Programming Questions (not Discussions) and you ask a question (how to do this, the "impossible to do" of your subject is... odd).
well I works
I worked out to make it work but that is not what I want...
how can i leave the older image there and the new configurations that appears after pressing r?

u genius
it doesn't work
maybe Idk how to insert it
man thank you a lot
you are kind at the end ; )

well it is very complicated for me lol 
well i
" you are kind at the end ; )"
I am severe when I wear my moderator hat, kinder as a regular, helpful user of this forum.

BTW, avoid using the Comment feature of Zoho, it makes replies to appear out of order...

" how can i leave the older image there and the new configurations that appears after pressing r?"
Draw twice: once before the rotation, once after. For this, better move the drawing code in a function.

void draw(){
  background(0);
  stroke(255);

  drawStuff();

  translate & rotate, as above

  drawStuff(); // Again, drawn rotated 
}
you have an interesting tone in ur language
man drawstuff? what is that?
I am French, so perhaps my language can sound strange sometime.

drawStuff() would be the function you would create, where you would put the remainder of your code currently in draw().
it says drawStuff doesn-t exists

oh france! the best! where?



here...

I did as PhiLho said.

I made a new function drawStuff() as PhiLho said.
It contains (nearly) all that was in your function draw() before.

The new draw (by PhiLho) calls drawStuff() twice.

when you press r the same is shown rotated again.

I made bold in the code what is new by PhiLho

Hope this helps.

otherwise PM me.

Greetings, Chrisir



// =====================================


boolean rotated; // Global...

//

//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);

  drawStuff();

  if (rotated) {
    translate(width / 2, height / 2);
    rotate(1.2);
    translate(-width / 2, -height / 2);
    drawStuff(); // Again, drawn rotated
  } // if

} // func

//

void drawStuff() {

  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");
  }
  if (key == 'r') {
    rotated = !rotated;
  }

}

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);
  }
}
//

Thanks, Chrisir!

Remainder, not reminder. Check your dictionary...
guys extremely appreciated without you I wouldn't be able to make it
For now I have to stop processing but I have to come back to this in like 2 weeks
we r making architecture in the school based on processing, in how the codes can activate an architectural problem
thank deeply
I will make you see some picts