Rotate the whole sketch, including drawed pixels

edited February 2017 in Questions about Code

Hi there,

is there a possibility to rotate a whole sketch? I have this code and want also the drawed pixels (spirals) to rotate. I tried rotate() but it doesn't work for drawed pixels...

Thanks for your help!

Joseph

Spirale_Joe meineSpirale;   //Declare
Spirale_Joe meineSpirale2;  //Declare
Spirale_Joe meineSpirale3;  //Declare
Spirale_Joe meineSpirale4;  //Declare
Spirale_Joe meineSpirale5;  //Declare
Spirale_Joe meineSpirale6;  //Declare
Spirale_Joe meineSpirale7;  //Declare


int mouseclicks = 0;
int R, G, B;

void setup()
{
  background(0);
  size(800, 600);
  smooth();
  frameRate(120);
  noStroke(); 
  fill(255);

  meineSpirale = new Spirale_Joe(255, 255, 0);  
  meineSpirale2 = new Spirale_Joe(0, 255, 255);  //Instanzierung
  meineSpirale3 = new Spirale_Joe(0, 0, 255);  //Instanzierung
  meineSpirale4 = new Spirale_Joe(255, 0, 255);  //Instanzierung
  meineSpirale5 = new Spirale_Joe(255, 255, 50);  //Instanzierung
  meineSpirale6 = new Spirale_Joe(255, 140, 200);  //Instanzierung
  meineSpirale7 = new Spirale_Joe(10, 255, 5);  //Instanzierung
}


void mouseClicked() {
  mouseclicks = mouseclicks + 1;
}

void draw()
{

  if (mouseclicks >= 1)
  {
    meineSpirale.run();
  }
  if (mouseclicks >= 2)
  {
    meineSpirale2.run();
  }
  if (mouseclicks >= 3)
  {
    meineSpirale3.run();
  }
  if (mouseclicks >= 4)
  {
    meineSpirale4.run();
  }
  if (mouseclicks >= 5)
  {
    meineSpirale5.run();
  }
  if (mouseclicks >= 6)
  {
    meineSpirale6.run();
  }
  if (mouseclicks >= 7)
  {
    meineSpirale7.run();
  }
}

class Spirale_Joe
{  
  float dist1 = 20, radius1 = dist1/2, alpha1 = 10, speed1;
  float dist2 = 20, radius2 = dist2/2, alpha2 = 10, speed2;
  float speed = 40;
  float px, py, x1, y1, x2, y2, k1, k2;    
  int PX1, PY1, PX2, PY2, X1, Y1, X2, Y2, XS, YS;
  int pc;
  int R, G, B;
  int M = 0;     //for counting the mouseclicks for savedMouse
  int runs = 0;
  int lifetime = 3000;
  PVector savedMouse; //The saved mouse position

  Spirale_Joe(int _R, int _G, int _B)
  { 
    R = _R;
    G = _G;
    B = _B;

    savedMouse = new PVector(0, 0); //Initialize the PVector
  }    

  void run()
  {
    mouseClicked();
    spirale();
    runcounter();

    if (runs > 6)
    {
      spirale2();
    }
  }

  void mouseClicked()
  {
    M = M -1;      // for saving the different mouse position
  }

  void runcounter()
  {    
    runs = runs +1;
    println("runs:  " +runs);
  }

  void spirale()
  { 
    if ((M == -1))
    {
      savedMouse.x = mouseX; 
      savedMouse.y = mouseY;
      M = M - 1;
    }

    if (runs >= lifetime) {
      radius1 = 0;
      speed1 = 0;
      dist1 = 0;
    } else
    {
      //spirale forward, check:
      noFill();
      noStroke();
      speed1 = speed;
      dist1 = 20;
      k1 = speed1/radius1;
      alpha1 += k1;
      radius1 += dist1/(360/k1);
      x1 = radius1*cos(radians(alpha1));
      y1 = -radius1*sin(radians(alpha1));
      X1 = (int)x1;
      Y1 = (int)y1;
      XS = int(savedMouse.x) + X1;
      YS = int(savedMouse.y) + Y1;
      //colordectection:
      pc = get(XS, YS);
      println(pc);
      if (pc != -16777216) {
        fill(255);
        rect(random(0, width), 20, 20, 20);
      }
    }
  }

  void spirale2()
  {      
    if (runs >= lifetime)
    {             
      //spirale 2 reverse, display:
      strokeWeight(2);
      stroke(0);
      rect(20, 20, 20, 20);
      speed2 =  -20;
      dist2 =  - speed;
      k2 = speed2/radius2;
      alpha2 += k2;
      radius2 -= dist2/(360/k2);
      x2 = radius2*cos(radians(alpha2));
      y2 = - radius2*sin(radians(alpha2));
      X2 = (int)x2;
      Y2 = (int)y2;
      if (radius2 >=8) 
      {
        line(PX2  + savedMouse.x, PY2  + savedMouse.y, X2 + savedMouse.x, Y2 + savedMouse.y);
      }
      PX2 = X2;
      PY2 = Y2;
    } else
    {
      //spirale 2 forward, display:
      strokeWeight(1);
      stroke(R, G, B);
      speed2 = speed;
      k2 = speed2/radius2;
      alpha2 += k2;
      radius2 += dist2/(360/k2);
      x2 = radius2*cos(radians(alpha2));
      y2 = -radius2*sin(radians(alpha2));
      X2 = (int)x2;
      Y2 = (int)y2;
      if (radius2 >= 11)
      {
        line(PX2  + savedMouse.x, PY2  + savedMouse.y, X2 + savedMouse.x, Y2 + savedMouse.y);
      }
      PX2 = X2;
      PY2 = Y2;
    }
  }
}
Tagged:

Answers

  • Answer ✓

    (i've made your two files into one, to make it easier for anyone cutting and pasting from here)

    rotate() rotates the current frame, it won't affect pixels drawn in previous frames.

    you might be able to draw onto an offscreen buffer and then rotate this buffer whilst copying it to the screen. look at createGraphics().

  • alternatively, use background() to clear the screen and redraw everything you've done so far every frame, if that's suitable. it's a lot more work for the cpu though.

Sign In or Register to comment.