/* Generated By:JJTree: Do not edit this line. ASTPolygon.java */

public class ASTPolygon extends SimpleNode {
  String name = "polygon";
  public ASTPolygon(int id) {
    super(id);
  }

  public ASTPolygon(drawphase2 p, int id) {
    super(p, id);
  }
  
  public String generate(String colour, int brushSize, int offsetX, int offsetY)
  {
  	String temp = "";
  	int[] coords = new int[10];
  	int coordNumber = 0;
  	//Calculates the points for a rectangle and places them into an array.
  	if (jjtGetNumChildren() == 4)
  	{
  		coords[0] = ((ASTInt) jjtGetChild(0)).val;
  		coords[1] = ((ASTInt) jjtGetChild(3)).val;
  		coords[2] = ((ASTInt) jjtGetChild(0)).val;
  		coords[3] = ((ASTInt) jjtGetChild(1)).val;
  		coords[4] = ((ASTInt) jjtGetChild(2)).val;
  		coords[5] = ((ASTInt) jjtGetChild(1)).val;
  		coords[6] = ((ASTInt) jjtGetChild(2)).val;
  	    coords[7] = ((ASTInt) jjtGetChild(3)).val;
  	    coords[8] = ((ASTInt) jjtGetChild(0)).val;
  	    coords[9] = ((ASTInt) jjtGetChild(3)).val;  	    
  	}
  	//Calculates the points for a square and places them into an array.
  	else if (jjtGetNumChildren() == 3)
  	{
  	    int lastPoint;
  	    if(((ASTInt) jjtGetChild(2)).val > ((ASTInt) jjtGetChild(0)).val)
  	    {
  	    	lastPoint = ((ASTInt) jjtGetChild(1)).val - (((ASTInt) jjtGetChild(2)).val - ((ASTInt) jjtGetChild(0)).val);
  	    }
  	    else
  	    {
  	    	lastPoint = ((ASTInt) jjtGetChild(1)).val + (((ASTInt) jjtGetChild(2)).val - ((ASTInt) jjtGetChild(0)).val);
  	    }
  	    coords[0] = ((ASTInt) jjtGetChild(0)).val;
  		coords[1] = lastPoint;
  		coords[2] = ((ASTInt) jjtGetChild(0)).val;
  		coords[3] = ((ASTInt) jjtGetChild(1)).val;
  		coords[4] = ((ASTInt) jjtGetChild(2)).val;
  		coords[5] = ((ASTInt) jjtGetChild(1)).val;
  		coords[6] = ((ASTInt) jjtGetChild(2)).val;
  	    coords[7] = lastPoint;
  	    coords[8] = ((ASTInt) jjtGetChild(0)).val;
  	    coords[9] = lastPoint;  	    
  	}
  	//Gives the number of X and Y coords for a triangle.  	
  	else if (jjtGetNumChildren() == 6)
  	{
  	   coordNumber = 6;
  	}
  	//Gives the number of X and Y coords for any polygon.
  	else
  	{
  	   coordNumber = jjtGetNumChildren();
  	}
  	
  	//Creates the lines for squares and rectangles.
    if(jjtGetNumChildren() == 3 || jjtGetNumChildren() == 4)
    {  	  
  	  //Creates each line in the polygon.
  	  for (int i = 0; i < 8; i = i+2)
   	  {   	    
        temp += ASTLine.makeLine(coords[i], coords[i+1], coords[i+2], coords[i+3], colour, brushSize, offsetX, offsetY);
   	  }     
 	}
 	//Creates the lines for triangles and polygons.
 	else if(jjtGetNumChildren() > 5)
 	{ 	  
 	   //Creates each line in the polygon.
 	   for (int i = 0; i < coordNumber; i = i+2)
   	   {
   	    int last = i+2;
   	    if(last >= jjtGetNumChildren())
   	    {
   	       last = 0;
   	    }  	    
   	    
        temp += ASTLine.makeLine(((ASTInt) jjtGetChild(i)).val, ((ASTInt) jjtGetChild(i+1)).val, ((ASTInt) jjtGetChild(last)).val, ((ASTInt) jjtGetChild(last+1)).val, colour, brushSize, offsetX, offsetY);
   	  }
 	} 	
 	return temp;
  }
  
  public String getName()
  {
     return name;
  }

}
