/*
 * 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 cz.muni.fi.pb162.project.geometry;
import java.util.Arrays;

/**
 * STILL IN PROCESS !!!
 * 
 * @author xvalchar
 */
public class ArrayPolygon extends SimplePolygon {
    
    private final Vertex2D[] vertices;
    
    /**
     * Check if parameter vertices is not null and copy array of vertices to  
     * private final Vertex2D vertices
     * 
     * @param vertices array of points (Vertex2D)
     * @throws IllegalArgumentException if vertices or any vertice in array is null 
     */
    public ArrayPolygon(Vertex2D[] vertices) throws IllegalArgumentException {
        if(vertices == null) {
            throw new IllegalArgumentException("Vertices is null!");
        }
        
        for(int i = 0; i < vertices.length; i++) {
            if(vertices[i] == null) {
                throw new IllegalArgumentException("Vertice " + i + " is null!");
            }
        }
        
        this.vertices = Arrays.copyOf(vertices, vertices.length);
    }
    
    @Override
    public Vertex2D getVertex(int index) {
        if(index < 0) {
            throw new IllegalArgumentException("Index i is < 0!");
        }
     
        return vertices[index % vertices.length];
    }
 
    @Override
    public int getNumVertices() {
        return vertices.length;
    }
 
    public ArrayPolygon invert() {
        Vertex2D[] vertexes = new Vertex2D[vertices.length];
        
        for(int i = 0; i < getNumVertices(); i++) {
            vertexes[i] = new Vertex2D(getVertex(i).getX() * (-1), getVertex(i).getY() * (-1));
        }
 
        ArrayPolygon invertPolygon = new ArrayPolygon(vertexes);
        return invertPolygon;
    }
    
    public boolean compareShiftFirstElement(ArrayPolygon pol) {
        int indexFirstElement = -1;
        
        for(int i = 0; i < getNumVertices(); i++) {
            if((getVertex(0).getX() == pol.getVertex(i).getX()) && 
               (getVertex(0).getY() == pol.getVertex(i).getY())) {
                indexFirstElement = i;
            }
        }
   
        int i, j;
        
        for(i = 0, j = indexFirstElement; i < getNumVertices(); i++, j++) {
            if((getVertex(i).getX() != pol.getVertex(j).getX()) || 
               (getVertex(i).getY() != pol.getVertex(j).getY())) {
                return false;
            }
            if(j == getNumVertices() - 1) {
                j = -1;
            }
        }
        
        return true;
    }
    
    public ArrayPolygon reverse(ArrayPolygon pol) {
        Vertex2D[] reversePol = new Vertex2D[getNumVertices()];
        
        int i, j;
        
        for(i = 0, j = getNumVertices() - 1; i < getNumVertices(); i++, j--) {
            reversePol[i] = pol.getVertex(j);
        }
        
        ArrayPolygon reversePolygon = new ArrayPolygon(reversePol); 
        return reversePolygon;
    }
    
    public boolean compareReverse(ArrayPolygon reversePolygon) {
        for(int i = 0; i < getNumVertices(); i++) {
            if((getVertex(i).getX() != reversePolygon.getVertex(i).getX()) || 
               (getVertex(i).getY() != reversePolygon.getVertex(i).getY())) {
                return false;
            }
        }
        
        return true;
    }
    
    public boolean compare(ArrayPolygon pol) {
        if(getNumVertices() != pol.getNumVertices()) {
            return false;
        }
        
        if(compareShiftFirstElement(pol)) {
            return true;
        }
        
        ArrayPolygon reversePolygon;
        reversePolygon = reverse(pol);
        
        if(compareReverse(reversePolygon)) {
            return true;
        }
        
        if(compareShiftFirstElement(reversePolygon)) {
            return true;
        }
          
        return false;
    }
}

@@ -1518,24 +1518,125 @@
 %0D%0A    %7D%0D%0A %0D%0A
+    /**%0D%0A     * Invert x and y coordinates%0D%0A     * %0D%0A     * @return inverted ArrayPolygon %0D%0A     */%0D%0A
     public A
@@ -1655,24 +1655,24 @@
 invert() %7B%0D%0A
-
         Vert
@@ -1997,32 +1997,215 @@
 ;%0D%0A    %7D%0D%0A    %0D%0A
+    /**%0D%0A     * Check if polygon have shift first element   %0D%0A     * %0D%0A     * @param pol one of the polygons%0D%0A     * @return false if polygons are different, true otherwise%0D%0A     */%0D%0A
     public boole
@@ -2983,32 +2983,183 @@
 ;%0D%0A    %7D%0D%0A    %0D%0A
+    /**%0D%0A     * Reverse points of polygon%0D%0A     * %0D%0A     * @param pol polygon that need to be reversed%0D%0A     * @return reverse arrayPolygon %0D%0A     */%0D%0A
     public Array
@@ -3554,32 +3554,233 @@
 ;%0D%0A    %7D%0D%0A    %0D%0A
+    /**%0D%0A     * Compare origin polygon and reverse polygon%0D%0A     * %0D%0A     * @param reversePolygon reverse polygon%0D%0A     * @return true if origin and reverse polygon are same, false otherwise%0D%0A     */%0D%0A
     public boole
@@ -4139,24 +4139,182 @@
    %7D%0D%0A    %0D%0A
+    /**%0D%0A     * Compare two polygons%0D%0A     * %0D%0A     * @param pol one of the polygons%0D%0A     * @return true if two polygons are same, false otherwise%0D%0A     */%0D%0A
     public b
@@ -4726,35 +4726,39 @@
       %0D%0A        
-if(
+return 
 compareShiftFirs
@@ -4785,82 +4785,8 @@
 gon)
-) %7B%0D%0A            return true;%0D%0A        %7D%0D%0A          %0D%0A        return false
 ;%0D%0A 

simplify compare method