HashSet cuts ;
Cut currentCut = null;

color flameColor; 
boolean toReset = false;
void keyPressed(){
  if(key == ' '){
  toReset = true;
  }
}  

void reset(){
    cuts = new HashSet();
    currentCut = null;
 stem1 = random(-15,15);
  stem2 = random(-15,15);
  stem3 = random(-15,15);
  stem4 = random(-15,15);
}

void makeFlameColor(){
  float r = random(.3,.5);
  flameColor = color(255*r,100*r,33*r); 
}
float stem1,stem2,stem3,stem4;



void setup() {
  size(400,400); 
  frameRate(20);

 reset();

}
void draw(){

  background(60);


  makeFlameColor();

  strokeWeight(3);
  stroke(0);

  fill(100,120,0);
  beginShape();
  vertex(180+stem1,20+stem2);
  vertex(220+stem3,20+stem4);
  vertex(220,80);
  vertex(180,80);
  endShape(CLOSE);

  //rect(180,20,40,100);  

  fill(255,153,51);

  ellipse(200,200,300,300);

  noFill();
  stroke(128,75,25);
  ellipse(200,200,200,300);
  ellipse(200,200,100,300);

  //I put this in the main loop rather than
  //the event handler 'cause I'm worried about 
  //the iterator...
  if(handleCut){
    if(currentCut == null){
      currentCut = new Cut();
    } 
    if(currentCut.newPoint(mouseX,mouseY)){
      cuts.add(currentCut);
      currentCut = null;
    } 
    handleCut = false;  
  }
  if(toReset){
    reset();
    toReset = false;
  }  

  Iterator i = cuts.iterator();
  while(i.hasNext()){
    Cut c = (Cut) i.next(); 
    c.draw();
  }

  if(currentCut != null){
    currentCut.draw(); 
  }




}
boolean handleCut = false;
void mousePressed(){
  handleCut = true;
}



class Cut{
  ArrayList points = new ArrayList();
  boolean isDone = false;

  boolean isNear(Point p,float x, float y){
    return (sqrt(   pow(x - p.x,2) + pow(y-p.y,2)) < 5);
  }

  //return true if this closes the thing
  boolean newPoint(float x, float y){
    if(points.size() > 0){
      Point p = (Point)points.get(0);
      if(isNear(p,x,y)){
        isDone = true;
        return true; 
      }
    }
    points.add(new Point(x,y));
    return false;
  }

  void drawAsShape(){
    beginShape();
    Iterator i = points.iterator();
    while(i.hasNext()){
      Point p = (Point)i.next();
      vertex(p.x,p.y);  
    }
    endShape(CLOSE);   
  }

  void draw(){
    stroke(0);
    if(isDone){
      noStroke();
      fill(128,77,26) ;
    } 
    else {
      noFill(); 
    }
    if(isDone){
      fill(128,77,26) ;
      pushMatrix();
      for(int i = 1; i < 8; i++){
        translate(1,1);

        drawAsShape();
      }

      popMatrix();
      fill(flameColor) ;

      drawAsShape();

    } 
    else {
      if(points.size()< 1){
        return;
      }
      Iterator i = points.iterator();
      Point lastPoint = (Point)i.next();
      while(i.hasNext()){
        Point p = (Point) i.next();
        line(lastPoint.x,lastPoint.y,p.x,p.y);
        lastPoint = p;
      }
      stroke(128);
      fill(128);
      line(lastPoint.x,lastPoint.y, mouseX,mouseY);
      if(points.size() > 0){
        Point p = (Point)points.get(0);
        if(isNear(p,mouseX,mouseY)){
          ellipse(p.x,p.y,5,5);
        } 
      }
    }
  }
}

class Point {
  float x,y; 
  Point(float x, float y){
    this.x = x;
    this.y = y;
  } 
}