#!/usr/bin/perl
# House.pl
# --------
# Script to draw a house, and rotate it all based
# on user input. 
# 
# Currently all thats really working is the drawing
# functionality, the translation formula's aren't
# working for some reason. 
#
# To-Do:
# Fix House Rotation
# Make House Drawing into one polygon 
# Perty up the code.
#

use CGI;
use Carp;
use Math::Trig;
use GD;
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 => '/cgi-bin/house.pl'),
		$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 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 
	####

	$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;
	}


