import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.InputMismatchException;
import java.lang.Math;


class testNewAlgo {

	static float  leftup_coord_lo, leftup_coord_la, leftdn_coord_lo, leftdn_coord_la, righup_coord_lo,righup_coord_la,righdn_coord_lo,righdn_coord_la, east_lo, east_la, north_lo, north_la, west_lo, west_la, south_lo, south_la;
	
	static int struct_hi;
	
	static double coord_longitude = 0.0;
	static double coord_latitude = 20.0;
	
	
	
public static void main(String[] Args){

NumberFormat format1 = NumberFormat.getInstance();
format1.setMaximumFractionDigits(8);


setsquare(200, 50);

coord_longitude = 0.001;

		try{
			PrintWriter outp = new PrintWriter("testNEW.kml");
			
			 outp.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		outp.println("<kml xmlns=\"http://earth.google.com/kml/2.0\">\n");
	    outp.println("<Folder>");
    	outp.println("<description>Blah blah</description>");
    	outp.println("<name>PLT_kml_test</name>");
    	outp.println("<open>0</open>");
	    outp.println("<Placemark>");
	    outp.println("<name> Test SQUARE </name>");		
		outp.println(" <Polygon>");
   		outp.println("<extrude>1</extrude>");
    	outp.println("<altitudeMode>relativeToGround</altitudeMode>");
   		outp.println(" <outerBoundaryIs>");
   		outp.println("   <LinearRing>");
    	outp.println("    <coordinates>");
     	outp.println(""+format1.format(leftup_coord_lo)+','+format1.format(leftup_coord_la)+','+struct_hi);
     	outp.println(""+format1.format(righup_coord_lo)+','+format1.format(righup_coord_la)+','+struct_hi);
	outp.println(""+format1.format(righdn_coord_lo)+','+format1.format(righdn_coord_la)+','+struct_hi);
	outp.println(""+format1.format(leftdn_coord_lo)+','+format1.format(leftdn_coord_la)+','+struct_hi);
    	outp.println(""+format1.format(leftup_coord_lo)+','+format1.format(leftup_coord_la)+','+struct_hi);
  		
  		
   		outp.println(" </coordinates>");
    	outp.println("  </LinearRing>");
   		outp.println(" </outerBoundaryIs>");
  		outp.println("</Polygon>");
		outp.println("</Placemark> ");
		
		setoctogon(200,50);

		//Octogon printk
	     outp.println("<Placemark>");
	    outp.println("<name> Test OCTOGON </name>");		
		outp.println(" <Polygon>");
   		outp.println("<extrude>1</extrude>");
    	outp.println("<altitudeMode>relativeToGround</altitudeMode>");
   		outp.println(" <outerBoundaryIs>");
   		outp.println("   <LinearRing>");
    	outp.println("    <coordinates>");
     	outp.println(""+format1.format(leftup_coord_lo)+','+format1.format(leftup_coord_la)+','+struct_hi);
     	outp.println(""+format1.format(west_lo)+','+format1.format(west_la)+','+struct_hi);
	outp.println(""+format1.format(leftdn_coord_lo)+','+format1.format(leftdn_coord_la)+','+struct_hi);
	outp.println(""+format1.format(south_lo)+','+format1.format(south_la)+','+struct_hi);
	outp.println(""+format1.format(righdn_coord_lo)+','+format1.format(righdn_coord_la)+','+struct_hi);
    	outp.println(""+format1.format(east_lo)+','+format1.format(east_la)+','+struct_hi);
	outp.println(""+format1.format(righup_coord_lo)+','+format1.format(righup_coord_la)+','+struct_hi);
	outp.println(""+format1.format(north_lo)+','+format1.format(north_la)+','+struct_hi);
     	outp.println(""+format1.format(leftup_coord_lo)+','+format1.format(leftup_coord_la)+','+struct_hi);  		
  		
   		outp.println(" </coordinates>");
    	outp.println("  </LinearRing>");
   		outp.println(" </outerBoundaryIs>");
  		outp.println("</Polygon>");
		outp.println("</Placemark> ");

		//triangle
		coord_longitude = 0.002;
		settriangle(100, 50);

	    outp.println("<Placemark>");
	    outp.println("<name> Test TRIANGLE </name>");		
		outp.println(" <Polygon>");
   		outp.println("<extrude>1</extrude>");
    	outp.println("<altitudeMode>relativeToGround</altitudeMode>");
   		outp.println(" <outerBoundaryIs>");
   		outp.println("   <LinearRing>");
    	outp.println("    <coordinates>");
     	outp.println(""+format1.format(west_lo)+','+format1.format(west_la)+','+struct_hi);
    	outp.println(""+format1.format(east_lo)+','+format1.format(east_la)+','+struct_hi);
        outp.println(""+format1.format(north_lo)+','+format1.format(north_la)+','+struct_hi);
     	outp.println(""+format1.format(west_lo)+','+format1.format(west_la)+','+struct_hi);

   		outp.println(" </coordinates>");
    	outp.println("  </LinearRing>");
   		outp.println(" </outerBoundaryIs>");
  		outp.println("</Polygon>");
		outp.println("</Placemark> ");


		outp.println("</Folder>");
		outp.append("</kml>\n\n");
		
		outp.close();
		}
		catch (IOException exception)
		{
			System.out.println("Error processing file:   " + exception);
		}

		
	}
		
public static void setsquare(double leng,int heig){
	
	   double dir = Math.toRadians(45.00); //turn degrees into radians for use with trig functions
	
	double cond_factor= (69.1105 * 5280.00) ;
    	double long_const =( leng / cond_factor) * ( Math.cos(dir) );
    	double lat_const  = (leng / cond_factor) * ( Math.sin(dir) );
	
	leftup_coord_lo = (float) ( coord_longitude - long_const );
    	leftup_coord_la = (float) ( coord_latitude + lat_const );
    	
    	leftdn_coord_lo = (float) (coord_longitude -long_const);
    	leftdn_coord_la = (float) (coord_latitude - lat_const);
    	
    	righup_coord_lo = (float) (coord_longitude + long_const);
    	righup_coord_la = (float) (coord_latitude +lat_const);
    	
    	righdn_coord_lo = (float) (coord_longitude + long_const);
		righdn_coord_la = (float) (coord_latitude - lat_const);   
	      	
		    	
    	struct_hi=heig;
    	}		
	
	public static void setoctogon(double leng,int heig){
	
	double dir = Math.toRadians(45.00); //turn degrees into radians for use with trig functions
	

	double cond_factor= (69.1105 * 5280.00) ;
    	double long_const = ( (leng/2) / cond_factor ) * ( Math.cos(dir) );
    	double lat_const  = ( (leng/2) / cond_factor ) * ( Math.sin(dir) );
	
	east_lo =   (float) ( ( (leng/2) / cond_factor )  + coord_longitude );
	east_la = (float) coord_latitude;

	righup_coord_lo = (float) (coord_longitude + long_const);
    	righup_coord_la = (float) (coord_latitude + lat_const);
    	
	north_lo = (float) coord_longitude;
	north_la =  (float) ( ( (leng/2) / cond_factor )  + coord_latitude );

	leftup_coord_lo = (float) ( coord_longitude - long_const );
    	leftup_coord_la = (float) ( coord_latitude + lat_const );
    	
	west_lo = (float) ( coord_longitude - (  ( (leng/2) / cond_factor ) ) );
	west_la = (float) coord_latitude;

    	leftdn_coord_lo = (float) (coord_longitude -long_const);
    	leftdn_coord_la = (float) (coord_latitude - lat_const);

	south_lo = (float) coord_longitude;
	south_la = (float) ( coord_latitude - (  (leng/2) / cond_factor )  );
    	
    	righdn_coord_lo = (float) (coord_longitude + long_const);
	righdn_coord_la = (float) (coord_latitude - lat_const);   
	      	

		    	
    	struct_hi=heig;
    	}		

	public static void settriangle(double leng,int heig){
	
	double dir = Math.toRadians(120.00); //turn degrees into radians for use with trig functions
	

	double cond_factor= (69.1105 * 5280.00) ;
    	double long_const = ( leng / cond_factor ) * ( Math.cos(dir) );
    	double lat_const  = ( leng / cond_factor ) * ( Math.sin(dir) );

	double zenith_lo  = coord_longitude;
	double zenith_la  = coord_latitude + ( ( leng / Math.sqrt(3.00) ) / cond_factor );
	
	north_lo = (float) zenith_lo;
	north_la = (float) zenith_la;

	east_lo = (float)(zenith_lo + long_const);
	east_la = (float) (zenith_la - lat_const);
	
	west_lo = (float) (zenith_lo - long_const);
	west_la = (float) (zenith_la - lat_const);

 	

		    	
    	struct_hi=heig;
    	}
		
}

