CSC220 Homework 4 solutions 2010

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 16:37, 20 October 2010 (UTC)


Part 1

  • General suggestions for the diagrams
    • Keep them simple
    • Align the boxes vertically as well as horizontally
    • Try to make the diagram fit a rectangle
    • Do not crowd the diagram. Leave breathing room for the eyes!

Two good examples

Version 1: Super Simple but Effective

CSC220ProjectDiagram1.jpg


Version 2: Sophisticated Detailed

CSC220ProjectDiagram2.jpg

Part 2

<html>

<!--
hw4b.php
Alex Cheng (220a-ag)
10/14/10
An html form that takes in information and processes it.
If inadequate information is given, the form will reload and ask
the user to enter the appropriate information.
Once the form is processed, it will display some information and
write 3 coordinates to a file called data.kml.  The 3 coordinates
correspond to the coordinates of the top-left corner of the 
rectangle, the bottom-right corner and the centre of the rectangle.

Sample output (in data.kml):
-72.70093720000000, 42.32779838333332
-72.59695311000000, 42.35373711666666
-72.648945155, 42.34076775

-->

<head>
<title>CSC 220, Homework 4</title>
<meta http-equiv="Pragma" content="no-cache">
</head>

<body bgcolor="#FFFF7E">
<font face="Arial" color="#003F87">
<h1>KML Coordinates!</h1>
<?php

  # testPage checks to see if both fields are given, if one or more is 
  # missing, then it displays a message to the user
  function testPage($email, $kml) {
  
  # display final page if neither fields are empty
  if (!empty($email) && !empty($kml)) {
    displayFinalPage($email, $kml);
    return 2;
  }
  
  # display error if either field is empty
  $params = 0;
  if (empty($email)) {
       printf("<font color='red'>Error: Please provide us with your email</font>\n");
  } else {
    $params++;
  }
  if (empty($kml)) {
    printf("<font color='red'>Error: Please provide us with some kml text</font>\n");
  } else {
    $params++;
  }
  
  return $params;
}
  
  # displayFinalPage displays final message gathered from form
  function displayFinalPage($email, $kml) {
    print "You provided us with the following email: <b>$email</b>";
    $kmlArray = explode("\n", $kml);
    $recordCoords = false;
    # loop through each line in kml and extract coordinates
    foreach ($kmlArray as $kmlLine) {
      $posStart = strpos($kmlLine, "<coordinates>");
      $posEnd = strpos($kmlLine, "</coordinates>");
      if (($posStart !== False) || ($recordCoords == true)) {
	$coords = explode(",", $kmlLine);
	if (preg_match("/[-+]?[0-9]*\.?[0-9]+/", trim($coords[0]), $matches)) {
	  $x[] = $matches[0];
	}
	if (preg_match("/[-+]?[0-9]*\.?[0-9]+/", trim($coords[1]), $matches)) {
	  $y[] = $matches[0];
	}
	$recordCoords = true;
      }
      if ($posEnd !== False) {
	$recordCoords = false;
      }
    }
    
    # compute top-left corner, bottom-right corner and centre points
    $minx = min($x);
    $miny = min($y);
    $maxx = max($x);
    $maxy = max($y);
    $middlex = ($minx+$maxx)/2;
    $middley = ($miny+$maxy)/2;
    
    echo "<br /><br />Based on the kml you entered, here is some information about the coordinates we extracted: ";
    echo "<br />Top left corner: <b>".min($x).", ".min($y)."</b>";
    echo "<br />Bottom right corner: <b>".max($x).", ".max($y)."</b>"; 
    echo "<br />Middle: <b>".$middlex.", ".$middley."</b>";  
    echo "<br />This information has been stored into a file called data.kml.";

    # write coordinates of three points to a file
    $f = fopen("uploads/data.kml", "w");
    $writeToFile = $minx.", ".$miny."\n".$maxx.", ".$maxy."\n".$middlex.", ".$middley;
    fwrite($f, $writeToFile);
  }

# displayRequestArray is debug function to show contents of $_REQUEST
function displayRequestArray() {
  print "<table width=\"50%\"><tr bgcolor=\"\#cc99cc\"><td><pre>\$_REQUEST = ";
  print_r( $_REQUEST );
  print "</pre></td></tr></table><P><P>\n";
}

# get the form parameters
$email = $_REQUEST['email'];
$kml  = $_REQUEST['kml'];

# check to see if we got both parameters
$noParamsSet = testPage($email, $kml);
if ( $noParamsSet < 2 ) {
  printf( '<form action="hw4b.php" method="post">
         <h2>Please fill out the nifty form below!</h2>
         <h3>All fields are mandatory.</h3>
         <p>
         Email: <input type="text" name="email" VALUE="%s" SIZE="40">
         <p>
         kml text: <textarea cols="35" rows="5" name="kml" value=""></textarea>
         <p>
         <input type="submit" value="Send">
         </form>', $email, $kml );
}

# for debugging
#displayRequestArray();

?>

</p>
</font>
</body>
</html>