How can I add or remove vertex of a polygon?

edited May 2014 in How To...

Hi everyone! Im traying to make a polygon change its form by adding more vertex and removing them, in addition to that I want to change tho fill inside each polygon something like this image.

Captura de pantalla 2014-05-12 a la(s) 12.55.42 p.m.

Im trying to add more vertex at the edge with every right click an remove them with the left click and depending on the position of the mouse I want to change the color of the fill inside each polygon.

But Im suck with the adding vertex part and the fill in each polygon.

Can anyone help me with this?

Answers

  • Show your code and indicate where the problem is in it.

  • class Tex {
      color c;
      float x, y;
      Tex(float ix, float iy){
        x=ix;
        y=iy;
        float r = map(ix,0,width,0,255);
        float g = map(iy,0,height,0,255);
        float b = (510-r-g)/2;
        c = color(r,g,b);
      }
      boolean isOver(){
        return dist(mouseX, mouseY, x, y) < 10;
      }
    }
    
    ArrayList<Tex> texs = new ArrayList();
    
    void setup(){
      size(600,600);
      noStroke();
    }
    
    void draw(){
      background(0);
      for(int i=0; i<texs.size(); i++){
        int j = (i+1)%texs.size();
        fill(texs.get(i).c);
        triangle(texs.get(i).x, texs.get(i).y, texs.get(j).x, texs.get(j).y, width/2, height/2);
      }
    }
    
    void mousePressed(){
      if(mouseButton == LEFT){
        texs.add( new Tex(mouseX, mouseY));
      }
      if( mouseButton == RIGHT){
        for(int i=texs.size()-1; i>=0; i--){
          if( texs.get(i).isOver() ){ 
            texs.remove(i);
          }
        }
      }
    }
    
Sign In or Register to comment.