/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Examen;

import java.util.ArrayList;

/**
 *
 * @author felix
 */
public class Poligono {
    private final ArrayList <Linea> lineas ;
    private final ArrayList <Triangulo>  triangulos; 
    
    public Poligono(ArrayList <Punto> puntos) {
        lineas = new ArrayList <Linea>();
        triangulos = new ArrayList<Triangulo>();
        int i, N;
        
        Punto ant=null;
        
        N =  puntos.size();
        
        for(i =0; i<N-1; i++) 
            lineas.add(new Linea(puntos.get(i), puntos.get(i+1)));
        
        lineas.add(new Linea(puntos.get(N-1), puntos.get(0)));
            
    }
    
    public double Perimetro() {
        double suma;
        
        suma =0;
        
        for(Linea l: lineas)
            suma += l.distancia();
        
        return suma;
    }
    
    public double Area() {
        double suma =0;
        double x0, y0, x1, y1, x2, y2, a, b, c;
        
        Punto p0 = lineas.get(0).getInicial();
        int i, N;
        
        
        x0 = p0.getX();
        y0 = p0.getY();
        
        N = lineas.size();
        
        for(i=1; i<N-1; i++){
            x1 = lineas.get(i).getInicial().getX();
            y1 = lineas.get(i).getInicial().getY();
            x2 = lineas.get(i+1).getInicial().getX();
            y2 = lineas.get(i+1).getInicial().getY();
            
            a = Math.sqrt((x0-x1)*(x0-x1) + (y0-y1)*(y0-y1));
            b = Math.sqrt((x0-x2)*(x0-x2) + (y0-y2)*(y0-y2));
            c = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
            
    
            triangulos.add(new Triangulo(a, b, c));
        }
        
        for (Triangulo t : triangulos) 
            suma += t.Area();
         
        return suma;
    }
    
    @Override
    public String toString() {
        String aux = "";
        int k=1;
        for(Linea l : lineas) {
           aux += k + ".-  " + l + "\n";
           k++;
        }
       aux += "Perimetro = " + Perimetro() +"\n";
       aux += "Area      = " + Area() +"\n";
       return aux; 
    }
}
