How can I rotate all of this

I want to rotate my snowflake with mouseX, here is the full code;

float angle = 0;
float cx;
float cy;
 
void setup() {
  size(640, 480);
  cx = 50;
  cy = height/2;
  noLoop();
  
}
 
void draw() {
  background(255);
  stroke(0);
  //kochCurve(243,4);
  translate(100,-100);
  kochSnowflake(360,4);
  
}

void kochCurve(float lngth, int level) {
  float newlngth;
  if (level == 0){
    forward(lngth);
    return;
  }
  else {
    newlngth = lngth/3.0;
    kochCurve(newlngth,level-1);
    rotation(-60);
    kochCurve(newlngth,level-1);
    rotation(120);
    kochCurve(newlngth,level-1);
    rotation(-60);
    kochCurve(newlngth,level-1); 
  }
}

void forward(float amount) {
  float newX = cx + cos(radians(angle)) * amount;
  float newY = cy + sin(radians(angle)) * amount;
 
  line(cx, cy, newX, newY);
  
  fill(0); 
  cx =  newX;
  cy =  newY;
}
 
void rotation(float degree) {
  angle = (angle + degree);
}

void kochSnowflake(float lngth, int level) {
  kochCurve(lngth,level); 
  rotation(120);
  kochCurve(lngth,level);
  rotation(120);
  kochCurve(lngth,level); 
}

Hello,

You will have to remove the noLoop().

An example of rotating that you can integrate with your code:

angle = 0;   // Sets your snowflake angle to 0
translate(width/2, height/2);   // Try with and without this to see what it does.
float angle2 = frameCount*(TAU/360);
rotate(angle2);
translate(-200, -300);

Not perfect… you can sort out the exact values.

Now try it with mouseX and map().
You must map mouseX to an angle.
You will need to read the references for this.

Please format your code as a courtesy to our community:
https://discourse.processing.org/faq#format-your-code

:)

1 Like