random colour change and shape change

edited December 2016 in Using Processing

Hi guys. wondering if you could help me change the smaller circles in to hexagons and also how to change the colour of each individual shape randomly when mouse pressed?

thanks

PVector[] h1 = new PVector[6];
void setup() {
size(700, 600);
background(0);
for (int i = 0; i < 6; i++) h1[i] = new PVector();
}

void draw() {
background(100);
translate(width/2, height/2);
textAlign(CENTER);
text("elliebirch", 0, 0);
rotate(radians(frameCount));
stroke(255, 43, 0);
noFill();

float theta = 0.0;
for (int i = 0; i < 6; i++) {
theta += TWO_PI/6.0;
h1[i].x = cos(theta)*100;
h1[i].y = sin(theta)*100;
}

for (int i = 0; i < 5; i++) {
int nextPoint = i+1;
line(h1[i].x, h1[i].y, h1[nextPoint].x, h1[nextPoint].y);
}
line(h1[0].x, h1[0].y, h1[5].x, h1[5].y);

for (int i = 0; i < 6; i++) ellipse(h1[i].x, h1[i].y, 50, 50);
}

Answers

  • changed question

  • I showed this in my answer in your other thread... didn't you like it?

  • this is a different question.

    thank you for your other reply :)

  • For the color, the last for loop should read this instead:

      for (int i = 0; i < 6; i++) {
        fill(random(256));
        ellipse(h1[i].x, h1[i].y, 50, 50);
      }
    

    For the second question, try this https://forum.processing.org/two/search?Search=hexagon

    Basically, you can create a function that draws a generic hexagon of an adjustable size. Your function can take also a rotation angle and a center point to translate the figure in space. You will also need pushMatrix/popMatrix calls. But first build the hexagon using vertex() calls.

    Kf

  • thanks for your response. I'm very new to this is there any way you would be able to add it to my code for me and send it back please. :)

  • PVector[] h1 = new PVector[6];
    PShape s2; 
    
    color[] ellCol = {
      color(222), 
      color(111), 
      color(255, 2, 2), 
      color(255, 255, 2), 
      color(255, 3, 225), 
      color(33)
    };
    
    void setup() {
      size(700, 600);
    
      background(0);
    
      for (int i = 0; i < 6; i++) 
        h1[i] = new PVector();
    
      float theta = 0.0;
      for (int i = 0; i < 6; i++) {
        theta += TWO_PI/6.0;
        h1[i].x = cos(theta)*13;
        h1[i].y = sin(theta)*13;
      }
    
      s2 = createShape();
      s2.beginShape();
      s2.fill(0, 0, 255);
      s2.noStroke();
    
      for (int i = 0; i < 5; i++) {
        int nextPoint = i+1;
        s2.vertex(h1[i].x, h1[i].y) ;
        s2.vertex(h1[nextPoint].x, h1[nextPoint].y) ;
      } // for 
      s2.vertex(h1[0].x, h1[0].y, h1[5].x, h1[5].y);
      s2.endShape();
    }
    
    void draw() {
      background(100);
    
      translate(width/2, height/2);
      textAlign(CENTER);
      text("elliebirch", 0, 0);
    
      rotate(radians(frameCount));
    
      stroke(255, 43, 0);
      noFill();
    
      float theta = 0.0;
      for (int i = 0; i < 6; i++) {
        theta += TWO_PI/6.0;
        h1[i].x = cos(theta)*100;
        h1[i].y = sin(theta)*100;
      }
    
      // ----
    
      for (int i = 0; i < 5; i++) {
        int nextPoint = i+1;
        line(h1[i].x, h1[i].y, 
          h1[nextPoint].x, h1[nextPoint].y);
      }
      line(h1[0].x, h1[0].y, h1[5].x, h1[5].y);
    
      //  translate(0, -210);
      for (int i = 0; i < 6; i++) {
        fill(ellCol[i]);
        pushMatrix(); 
        translate (h1[i].x, h1[i].y);
        // shapeMode(CENTER);
        s2.setFill(ellCol[i]);
        shape(s2);
        // ellipse(h1[i].x, h1[i].y, 50, 50);
        popMatrix();
      }
    }
    
  • mouse press does not work on this. sorry to be a pain guys :(

  • Answer ✓

    Add the following lines to @chrisir's code:

    void mouseReleased() {
      for (int i = 0; i < 6; i++) {
        ellCol[i]=color(random(256),random(256),random(256));
      }
    }
    

    Kf

  • you wrote

    mouse press does not work on this.

    I showed this in your previous thread but you choose to ignore it and instead open this new thread....

    just stitch it together

    Best, Chrisir ;-)

  • content restored, thread closed.

    vandalising threads is frowned upon here.

This discussion has been closed.