import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.Polygon;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import tiburcio.lib2D.s2D;
import tiburcio.lib2D.v2D;
import tiburcio.lib3D.m3D;
import tiburcio.lib3D.v3D;


public class NvuAl extends s2D{

    double Ox,Oy,Oz,Rcx,Rcy,Rcz,D=300;
    int perspectiva=0;
    String archivo;

    public static void main(String[] args) throws IOException{
        // TODO code application logic here
        NvuAl p=new NvuAl(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
    }
    
    
    public NvuAl(String ox, String oy, String oz, String rx, String ry, String rz, String persp, String d,String arch){

        this.Ox=Double.parseDouble(ox);
        this.Oy=Double.parseDouble(oy);
        this.Oz=Double.parseDouble(oz);
        this.Rcx=Double.parseDouble(rx);
        this.Rcy=Double.parseDouble(ry);
        this.Rcz=Double.parseDouble(rz);
        this.perspectiva=Integer.parseInt(persp);
        this.D=Double.parseDouble(d);
        this.archivo=arch;
        DEF_SISTEMA("Pruebas NVU",600,600,new Color(200,255,200));
    }
    
    public void paint(Graphics g){
        VENTANA(-300,-300,300,300);
        MIRILLA(0,0,1,1);
        g.setColor(Color.green);
        ejes(g);
        g.setColor(Color.white);
        
        double D=300;
        v3D O=new v3D(this.Ox,this.Oy,this.Oz);
        v3D R=new v3D(this.Rcx,this.Rcy,this.Rcz);
        
        v3D N=new v3D();
        
        N=O.resta(R).unitario();
        
        v3D UP=new v3D(0,1,0);
        v3D UPP=UP.resta(N.multiplica(UP.ppunto(N)));
        v3D V=UPP.multiplica(1.0/UPP.modulo());
       
        v3D U=V.pcruz(N).multiplica(1.0/V.pcruz(N).modulo());
       
        System.out.println("D = "+D);
        System.out.println("O = "+despVectorDec(O));
        System.out.println("R = "+despVectorDec(R));
        System.out.println("N = "+despVectorDec(N));
        System.out.println("V = "+despVectorDec(V));
        System.out.println("U = "+despVectorDec(U));
        
        v3D puntos[]=new v3D[8];
        v3D puntost[]=new v3D[8];
        v2D p2d[]=new v2D[8];
        v2D p2dP[]=new v2D[8];
        puntos[0]=new v3D(-100,-100,-100);
        puntos[1]=new v3D(100,-100,-100);
        puntos[2]=new v3D(100,100,-100);
        puntos[3]=new v3D(-100,100,-100);
        puntos[4]=new v3D(-100,-100,100);
        puntos[5]=new v3D(100,-100,100);
        puntos[6]=new v3D(100,100,100);
        puntos[7]=new v3D(-100,100,100);
        m3D mr=new m3D();
        m3D me=new m3D();
        me.escalacion(1, 1, 1);
        
        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(1));
        System.out.println(String.format("%3s %13s %13s %15s %10s %10s %5s %5s %5s %5s"," No.","Vértice (P)","P-R","(P-R).U","(P-R).V","(P-R).N","Pvx","Pvy","PvxP","PvyP"));
        for (int teta=0;teta<=0;teta+=10){
            mr.rotacionY(teta);
            for (int i=0;i<=7;i++){
              puntost[i]=puntos[i].multiplica(me).multiplica(mr);
              double vx=puntost[i].resta(R).ppunto(U);
              double vy=puntost[i].resta(R).ppunto(V);
              double vz=puntost[i].resta(R).ppunto(N);
              //System.out.println("ZZZZ "+vz);
              
              if (this.perspectiva==0)
                // SIN PERSPECTIVA
                p2d[i]= new v2D(coorDX(vx),coorDY(vy));
              else if (this.perspectiva==1)
                // CON PERSPECTIVA
                p2d[i]= new v2D(coorDX(vx*D/vz),coorDY(vy*D/vz)); 
              
              
              p2dP[i]=new v2D(coorDX(vx*D/vz),coorDY(vy*D/vz)); 
              System.out.println(String.format("%3d %16s %16s %10.2f %10.2f %10.2f %5.0f %5.0f %5.0f %5.0f",i,despVector(puntost[i]),despVector(puntost[i].resta(R)),puntost[i].resta(R).ppunto(U),puntost[i].resta(R).ppunto(V),puntost[i].resta(R).ppunto(N),vx,vy,vx/vz*D,vy/vz*D));
            }
            int col=30;
            if (true){
              g2d.setColor(new Color(0,col,0));
              dibujaCuadrilatero(p2d[7],p2d[6],p2d[5],p2d[4], g2d);
            }
            if (true){
              g2d.setColor(new Color(0,col,0));
              dibujaCuadrilatero(p2d[4],p2d[5],p2d[1],p2d[0], g2d);
            }
            if (true){
              g2d.setColor(new Color(0,col,0));
              dibujaCuadrilatero(p2d[3],p2d[7],p2d[4],p2d[0], g2d);
            } 
            if (true){
              g2d.setColor(new Color(0,col,0));
              dibujaCuadrilatero(p2d[2],p2d[6],p2d[7],p2d[3], g2d);
            }
            if (true){
              g2d.setColor(new Color(0,col,0));
              dibujaCuadrilatero(p2d[0],p2d[1],p2d[2],p2d[3], g2d);
            }
            if (true){
              g2d.setColor(new Color(0,col,0));
              dibujaCuadrilatero(p2d[1],p2d[5],p2d[6],p2d[2], g2d);
            }
                  

        }
            /*BufferedImage image = new BufferedImage(p.getWidth(), p.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = image.createGraphics();

            // Renderizamos el contenido del Frame en la imagen
            p.paint(g2d);

            // Guardamos la imagen en un archivo (por ejemplo, en formato PNG)
            ImageIO.write(image, "PNG", new File(p.archivo+".png"));
            */
    

        } //paint
        
        boolean dibujar(int a, int b, int c, int d, v3D pts[], v3D vR){
           v3D v1=pts[a].resta(pts[b]);
           v3D v2=pts[c].resta(pts[b]);
           v3D v12=v2.pcruz(v1);
           double pp=pts[a].resta(vR).ppunto(v12);
           System.out.println(" "+(pp>=0));
           vR.imprime();
           //return true; //Para dibujar todas la caras 
           return pp>=0;
        }

        int queColor(int a, int b, int c, int d, v3D pts[], v3D vR){
           v3D v1=pts[a].resta(pts[b]);
           v3D v2=pts[c].resta(pts[b]);
           v3D v12=v2.pcruz(v1);
           double incid=180-Math.abs(180*Math.acos(v12.unitario().ppunto(vR.unitario()))/Math.PI);
           int col=(int)(255-incid/90.0*255);
           return col;
        }

        
        void dibujaCuadrilatero(v2D p1, v2D p2, v2D p3, v2D p4, Graphics2D g2d){
            int xV[]={(int)p1.v[0],(int)p2.v[0],(int)p3.v[0],(int)p4.v[0]};

            int yV[]={(int)p1.v[1],(int)p2.v[1],(int)p3.v[1],(int)p4.v[1]};
            
            Polygon cara=new Polygon(xV,yV,4);
            //xg2d.fillPolygon(cara);
            g2d.setColor(Color.black);
            g2d.drawLine((int)p1.v[0],(int)p1.v[1],(int)p2.v[0],(int)p2.v[1]);
            g2d.drawLine((int)p2.v[0],(int)p2.v[1],(int)p3.v[0],(int)p3.v[1]);
            g2d.drawLine((int)p3.v[0],(int)p3.v[1],(int)p4.v[0],(int)p4.v[1]);
            g2d.drawLine((int)p4.v[0],(int)p4.v[1],(int)p1.v[0],(int)p1.v[1]);
        }

        String despVector(v3D p){
            return String.format("<%4.0f,%4.0f,%4.0f>",p.v[0],p.v[1],p.v[2]);
        }
        
        String despVectorDec(v3D p){
            return String.format("<%.4f,%.4f,%.4f>",p.v[0],p.v[1],p.v[2]);
        }
        
    }
