#!/usr/bin/perl
#
# Script to draw a house, and rotate it all based
# on user input. 
#

use CGI;
use Carp;
use Math::Trig;

my $page = new CGI;
my $build = $page->param("Build");
if ($build eq '1') { &print_house; } else { &get_specs; }



sub get_specs
	{

	print   $page->header    ("text/html" ),
	 	$page->start_html("Let's Build a House!"),
 	  	$page->h1        ("Let's Build A House"), 
	 	$page->start_form( -method => 'post', 
				   -action => $page->url),
		$page->br	 ("House Height"),		
	 	$page->popup_menu( -name   => 'HouseHeight', 
			   	   -values => ['100', '200', '300'] ),
		$page->br	 ("House Width"),
		$page->popup_menu( -name   => 'HouseWidth',
			           -values => ['100', '200', '300'] ),

		$page->br	 ("Roof Height"),
		$page->popup_menu( -name   => 'RoofHeight',
				   -values => ['50', '80', '100']),
		$page->br	 ("Rotate By How Many Degrees?"),
		$page->popup_menu( -name   => 'Rotation',
				   -values => [ 
						'0',
						'20', 
 						'50', 
						'80', 
						'110', 
						'140', 
						'170', 
						'200', 
						'230', 
						'260', 
						'290', 
						'320',
						'350'
					      ]),
		$page->p,
		$page->hidden    ( -name  => 'Build', 
				   -value => '1'),
		
		$page->submit(),
		$page->end_form(),
		$page->end_html;
		
	} 





sub build_house 
	{ 
	use GD;
	
	###############
	# First we grab the input about how the house should be 
	# built, there's a lot of variables, so I'll define them 
	# below.
	#
	# House Building Parameters  
	#    $HouseHeight - Height of the House
	#    $HouseWidth  - Width of House
	#    $HouseColor  - Color of the house
	#    $RoofColor   - Color of the Roof
	#    $RoofHeight  - Height of Roof (Width determined by House Size)
	#    $DoorColor   - Color of the Door
	#    $Windows     - Number of Windows, if Any
	#    $Tree        - Boolean, do we draw a tree?
	#    $Sunny       - Boolean, is the sun out or is it cloudy?
	#    $Grass	  - Boolean, do we have dirt or grass on the ground?
	###############
	my $HouseHeight = $page->param("HouseHeight");	
	my $HouseWidth  = $page->param("HouseWidth" );
	my $HouseColor  = $page->param("HouseColor" );
	my $RoofColor   = $page->param("RoofColor"  );
	my $RoofHeight  = $page->param("RoofHeight" );
	my $DoorColor   = $page->param("DoorColor"  );
	my $Windows     = $page->param("Windows"    );
	my $Tree        = $page->param("Tree"       );		 	
	my $Sunny       = $page->param("Sunny"      );
	my $Grass       = $page->param("Grass"      );
	}



sub print_house
	{
	####
	# Grab our input first, since it affects how
	# everything else is built.
	####
	my $HouseHeight =  $page->param("HouseHeight");
	my $HouseWidth  =  $page->param("HouseWidth" );
	my $RoofHeight  =  $page->param("RoofHeight" );
	my $Rotation    =  $page->param("Rotation"   );

	####
	# Take the input and make it into 
	# useable coordinates.
	####
	
	# If the house is being rotated, float it. Otherwise leave it be.	
        if ($Rotation eq "0") {$float = 0;} else {$float = 100;} 
	# Set the frame size.
	my $Frame       = 500;
	# Image size is 10 larger than frame.
	my $isize       = ($Frame + 10); 
	# Set center of the image as 1/2 of the frame
	my $center      = ($Frame / 2); 

	####
	# Calculate the x,y coordinates for the house,     
	# then build the image.
	#### 
	my $x1          = ($center -  ($HouseWidth / 2));
	my $y1          = ($Frame - $float);
	my $x2          = (($HouseWidth / 2) + $center );
	my $y2          = ($Frame - $HouseHeight - $float );
	my $image       = new GD::Image($isize, $isize);

	####
	# Allocate Our Colors so we can use them as we please
	####
	my $white  = $image->colorAllocate(255,255,255);
	my $yellow = $image->colorAllocate(255,255,  0);
	my $black  = $image->colorAllocate(  0,  0,  0);
	my $red    = $image->colorAllocate(255,  0,  0);
	my $green  = $image->colorAllocate(  0,255,  0);
	my $blue   = $image->colorAllocate(  0,  0,255);
	my $orange = $image->colorAllocate(255,153, 51);
	$image->transparent($white);
	$image->interlaced('true');
	####
	# Draw a border around the picture
	####
	$image->rectangle(0,0,500,500,$black);

	####
	# If we're rotating the house, translate
	# the coordinates before building it.
	####
	$xanchor = $x1;
	$yanchor = $y1;
	if ($Rotation ne  "0")
		{
		my $x1temp = (($x1*(cos($Rotation))) - ($x1*(sin($Rotation))));
		my $y1temp = (($y1*(sin($Rotation))) + ($y1*(cos($Rotation))));
                my $x2temp = (($x2*(cos($Rotation))) - ($x2*(sin($Rotation))));
                my $y2temp = (($y2*(sin($Rotation))) + ($y2*(cos($Rotation))));
		$x1 = $x1temp; $y1 = $y1temp; $x2 = $x2temp; $y2 = $y2temp;
		}	
	
	####
	# Build the House 
	####

	# Square of the House
#	$image->rectangle($x1,$y1,$x2,$y2,$black);
#	$image->fill(($x1 + 1), ($y1 - 1), $black);

	$house = new GD::Polygon;
		$house->addPt($x1,$y1);
		$house->addPt($x1,$y2);
		$house->addPt($x2,$y2);
		$house->addPt($x2,$y1);

	$image->filledPolygon($house, $black);
#	$roof = new GD::Polygon;
#	$roof->addPt(($x1 - 10),$y2);
#	$roof->addPt(($x2 + 10),$y2);
#	$roof->addPt($center, ($y2 - $RoofHeight) );
#	$image->filledPolygon($roof, $red);
	print "Content-type: image/png\n\n";
	binmode STDOUT;
	print $image->png;
	}


	





sub printit 
	{
	use GD;
	my $image   = new GD::Image(500,500);

	####
	# Allocate Our Colors so we can use them as we please
	####
	my $white  = $image->colorAllocate(255,255,255);
	my $yellow = $image->colorAllocate(255,255,  0);
    	my $black  = $image->colorAllocate(  0,  0,  0);       
    	my $red    = $image->colorAllocate(255,  0,  0);      
	my $green  = $image->colorAllocate(  0,255,  0);
    	my $blue   = $image->colorAllocate(  0,  0,255);
	my $orange = $image->colorAllocate(255,153, 51);
	
	my $HouseHeight = "300";
	my $HouseWidth  = "200";	
	my $RoofHeight  = "100";	
	my $Center      = "250";
 	my $HouseHalf   = ($HouseWidth / 2);
	my $xstart      = ($Center - $HouseHalf);
	my $xfinish	= ($center + $HouseHalf);
	my $ystart	= 0;
	my $yfinish	= $HouseHeight;
	
	$image->transparent($white);
	$image->filledRectangle($xstart,$ystart,$xfinish,$yfinish,$red);


	 	
			
	print "Content-type: image/png\n\n";
	binmode STDOUT;
	print $image->png;
	}





           
