简体   繁体   中英

draw images with random rotation java + processing

I'm drawing arrows using java and I can draw them straight but now I need to have the arrows pointing in different directions

in my current code, I draw a triangle and then a square.

so is there a way to group the two after they've been drawn and then rotate them at a random angle

right now I'm only able to rotate the triangle and square separately, causing some messy thing

   void setup() {
  size(400, 400);
}

void draw() {
  float r = random(24, 64);
  background(255);
 
drawArrow(r);
//drawPlus(r);
saveFrame("dataArrow/plus####.png");

  if (frameCount == 100) {
    exit();
  }
}
void drawArrow(float r){
 
 
  float base = r * 2;
  float xStart = random(1, width-base - 1);
  float xEnd = xStart + base;
  float k = 0.5 * base;
  float y = random(k, width-k);
  float middleBase = base/2 + xStart;
  float rectSide = 0.5 * base;
  float rectX1 = middleBase - rectSide/2;
  float rectX2 = middleBase + rectSide/2;
 fill(0);
  triangle(xStart, y, xEnd, y, middleBase,  y - k);
  rect(rectX1, y, rectSide, rectSide);
 }

not sure if this exactly what you mean but here is how to move things around

push and pop matrix allows you to organize things that should have the same translations

https://processing.org/reference/pushMatrix_.html

https://processing.org/reference/rotate_.html

https://processing.org/reference/translate_.html

basic example

pushMatrix();//start of new translation and rotation things
translate(xAmount,yAmount);//this moves the origin
rotate(angle);//this rotates around origin
//drawing around the point of rotation 0,0 here
//drawing...
popMatrix();//reset all translations and rotations to before

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM