Back to DFS's Workshop Page
Back to Agenda Page


Hypotenuse Problem

Write a set of scripts to be used through a web browser to calculate the length of the hypotenuse of a right triangle given the lengths of the other two sides. When reporting the results, provide a PNG to illustrate the result. To see how this could work, view this possible solution.

This proposed solution consists of four steps. You will be asked to create a total of five scripts, two of which will not be used in the final solution. They are used solely to make development easier.

For this problem, you are to cannibalize any available code and write five scripts which will do the following.

  1. Write a PHP script to be run from the Command Line: hypotenusefgets.php

    This is a preliminary test script to make sure that the mathematics and overall algorithm are correct. Running it from the Command Line eliminates any additional problems which might arise due to the use of forms and the passing of values from one script to another.

    1. Inform the user about the purpose of the script
    2. Input values for the two perpendicular sides of the triangle
    3. Calculate the hypotenuse of the triangle
      Check out the Mathematical Functions page.
    4. Report the results

  2. Write an HTML/PHP pair of scripts to elicit infomation from the user and then to calculate and report the results: hypotenuse.html & hypotenuse.php

    Create hypotenuse.html as the frontend for interacting with the user.

    1. Ask the user for the lengths of the two perpendicular sides
    2. Accept input in a form
      Use the variable names established in hypotenusefgets.php.
    3. Using the Calculate button, send the data to a PHP script called hypotenuse.php

    Create hypotenuse.php to process the user's input and report the results. At this time, you will not be attempting to produce the graphical representation of the triangle.

    1. Bulletproofing: Check the data received to ensure that the values are possible; use die or exit to report problems and terminate execution
    2. Calculate the hypotenuse.
    3. Output a placeholder picture. Code such as the following would do the job.
      echo "<p><img src=whatever.png ";
      echo "alt=\"This is the triangle\" border=4></p>\n";
      
    4. In a grammatical sentence, report the length of the hypotenuse.

  3. Write a pair of PHP scripts, one to generate the drawing of the triangle and one to call this script.

    Create hypotpiccall.php to test the PHP script which creates the picture. The following would be sufficient.

    <?php
    echo "<p><img src=drawtriangle.php?a=3&b=4&c=5 ";
    echo "alt=\"This is the triangle\" border=4></p>
    ?>

    Create drawtriangle.php to draw a picture of the triangle. Check out the Image Functions page.

    1. Generate a specification of the image type.
      Header("Content-type: image/png");
    2. Calculate the pixel locations of the vertices for the triangle.
    3. Specify the colors to be used for the background, the triangle and the lengths.
    4. Draw the triangle using the imagefilledpolygon() function.
    5. Label side a.
    6. Label side b.
    7. Label the hypotenuse.
    8. Output the image using imagepng().

  4. Once you are satisfied with the output generated by the hypotenuse.php and drawtriangle.php scripts, replace the placeholder picture call in hypotenuse.php with the one developed in hypotpiccall.php.

To see how this could work, view this possible solution.


© 2005 DFStermole
Created 5 Nov 05
Last Modified 9 Nov 05