// File: Polygon.java
// Time-stamp: "2005-09-21 23:54:38 calvanese"
// Purpose: Written exam 20/9/2005 - BSC in Production Engineering
//          solution part 2


public class Polygon {

  // representation of the objects
  private Vertex[] vertices;
  private int numVert;

  // pubblic methods

  public Polygon(int size) {
    vertices = new Vertex[size];
    numVert = 0;
  }
  
  public int getSize() {
    return vertices.length;
  }

  public int numVertices() {
    return numVert;
  }

  public Vertex getVertex(int pos) {
    if (pos >= 0 && pos < numVert)
      return vertices[pos];
    else
      return null;
  }

  public void addVertex(Vertex v, int pos) {
    if (numVert < vertices.length && pos >= 0 && pos <= numVert) {
      for (int i = numVert; i > pos; i--)
        vertices[i] = vertices[i-1];
      vertices[pos] = v;
      numVert++;
    }
  }
  
  public void removeVertex(int pos) {
    if (pos >= 0 && pos < numVert) {
      for (int i = pos; i < numVert-1; i++)
        vertices[i] = vertices[i+1];
      vertices[numVert-1] = null;
      numVert--;
    }
  }

  public int findVertex(double x, double y) {
    for (int pos = 0; pos < numVert; pos++)
      if (vertices[pos].getX() == x && vertices[pos].getY() == y)
        return pos;
    return -1;
  }
  
  public int[] verticesOfColor(String col) {
    int[] res = new int[numVerticesOfColor(col)];
    int pos = 0;
    for (int count = 0; count < numVert; count++)
      if (vertices[count].getColor().equals(col)) {
        res[pos] = count;
        pos++;
      }
    return res;
  }

  private int numVerticesOfColor(String col) {
    int count = 0;
    for (int i = 0; i < numVert; i++)
      if (vertices[i].getColor().equals(col))
        count++;
    return count;
  }
  
}
