package p5;
import anar.*;




import java.util.*;

import processing.core.PApplet;
import voronoi.*;

public class Test04aVoronoi extends PApplet {

  /*
   * Example for Anar library by Guillaume LaBelle + Julien Nembrini
   * http://anar.ch
   */


  Anar      myScene;
  Obj       myObj;

  ArrayList megaContainer;

  public void setup(){
    size(800,400,OPENGL);
    Anar.init(this);
    Anar.drawAxis();

    initForm();
  }

  public void initForm(){
    myObj = new Obj();


    // RANDOMS POINTS
    Obj inPts = new Obj();
    for (int i = 0; i<200; i++)
      inPts.add(Anar.PtRnd(400,400));


    // INIT THE TRIANGULATION
    int sz = 1000000;

    Simplex firstTriangle = new Simplex(new Pnt[]{new Pnt( -sz, -sz), new Pnt(sz, -sz), new Pnt(0,sz)});
    DelaunayTriangulation dt = new DelaunayTriangulation(firstTriangle);

    // ADD THE POINTS
    for (int i = 0; i<inPts.numOfPts(); i++)
      dt.delaunayPlace(new Pnt(inPts.pt(i).x(),inPts.pt(i).y()));


    // int ii = 0;

    // RETURN EDGES
    megaContainer = new ArrayList();

    ArrayList simplexComparator = new ArrayList();

    for (Iterator it = dt.iterator(); it.hasNext();){
      Simplex triangle = (Simplex)it.next();
      for (Iterator otherIt = dt.neighbors(triangle).iterator(); otherIt.hasNext();){

        Simplex other = (Simplex)otherIt.next();

        Simplex[] ss = new Simplex[2];
        ss[0] = triangle;
        ss[1] = other;

        if( !isInList(simplexComparator,ss)){
          simplexComparator.add(ss);

          Simplex[] ss2 = new Simplex[2];
          ss2[0] = other;
          ss2[1] = triangle;
          simplexComparator.add(ss2);


          // println(simplexComparator.size());


          Pnt p = Pnt.circumcenter((Pnt[])triangle.toArray(new Pnt[0]));
          Pnt q = Pnt.circumcenter((Pnt[])other.toArray(new Pnt[0]));
          // readColor(voronoiColor,0);
          // draw(p,q);
          float v[] = new float[4];

          v[0] = (float)p.coord(0);
          v[1] = (float)p.coord(1);
          v[2] = (float)q.coord(0);
          v[3] = (float)q.coord(1);

          // println(ii++);

          megaContainer.add(v);
        }
        // else
        // println("-");


      }
    }


    println(megaContainer.size());


    myObj.add(inPts);

  }


  boolean isInList(ArrayList arr, Simplex[] ss){
    boolean out = false;

    for (int i = 0; i<arr.size(); i++){
      Simplex[] sss = (Simplex[])arr.get(i);
      if(sss[0]==ss[0]&&sss[1]==ss[1]){
        out = true;
        // println("+");
      }
    }
    return out;
  }


  Pt filterPts(ArrayList pts, float[] v){
    return filterPts(pts,v[0],v[1]);
  }


  Pt filterPts(ArrayList pts, float x, float y){
    Pt out = Anar.Pt(x,y);

    for (int i = 0; i<pts.size(); i++){
      Pt q = (Pt)pts.get(i);

      if(Math.abs(q.x()-x)<0.0001&&Math.abs(q.y()-y)<0.0001)
        out = q;
    }

    return out;
  }


  public void draw(){
    background(255);
    myObj.draw();

    stroke(255,0,0);
    for (int i = 0; i<megaContainer.size(); i++){
      float v[] = (float[])megaContainer.get(i);
      // if(i%2==0)
      line(v[0],v[1],v[2],v[3]);
    }
  }

  public void keyPressed(){
    if(key==' ')
      initForm();
  }

  public static void main(String[] args){
    PApplet.main(new String[]{Test04aVoronoi.class.getName()});
  }
}

