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

public class ASTPolygon extends SimpleNode {
  public ASTPolygon(int id) {
    super(id);
  }

  public ASTPolygon(drawphase1 p, int id) {
    super(p, id);
  }
  
  public String generate()
  {
  	String temp = "";
  	int[] coords = new int[10];
  	int coordNumber = 0;
  	//Calculates all the points for a rectangle and puts the X and Y
  	//parts 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 all the points for a square and puts the X and Y
  	//parts 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;  	    
  	} 
  	//sets the polygon to be a triangle. 	
  	else if (jjtGetNumChildren() == 6)
  	{
  	   coordNumber = 6;
  	}
  	//sets the polygon to be a polygon with coordNumber of X and Y points.
  	else
  	{
  	   coordNumber = jjtGetNumChildren();
  	}
  	
  	//Prints out and returns the SVG code for rectangles and squares.
    if(jjtGetNumChildren() == 3 || jjtGetNumChildren() == 4)
    {  	  
  	  for (int i = 0; i < 8; i = i+2)
   	  {
   	    temp += ("<line x1=\"" + coords[i] +
        "\" y1=\"" + coords[i+1] +
        "\" x2=\"" + coords[i+2] +
        "\" y2=\"" + coords[i+3] +
        "\" stroke=\"black\" stroke-width=\"1\"/>\n");
   	  }     
 	}
 	//Prints out and returns the SVG code for triangles and Polygons.
 	else if(jjtGetNumChildren() > 5)
 	{ 	  
 	   for (int i = 0; i < coordNumber; i = i+2)
   	   {
   	    int last = i+2;
   	    if(last >= jjtGetNumChildren())
   	    {
   	       last = 0;
   	    }
   	    
   	    temp += ("<line x1=\"" + ((ASTInt) jjtGetChild(i)).val +
        "\" y1=\"" + ((ASTInt) jjtGetChild(i+1)).val +
        "\" x2=\"" + ((ASTInt) jjtGetChild(last)).val +
        "\" y2=\"" + ((ASTInt) jjtGetChild(last+1)).val +
        "\" stroke=\"black\" stroke-width=\"1\"/>\n");
   	  }
 	}
 	System.out.println(temp);
 	return temp;
  }

}
