Sunday, June 2, 2013

Graphing trigonometric functions

Graphing trigonometric functions

I've come across something that I'd like to accomplish without using any frameworks or other graphing tools that are out there on the internet, which is graphing trigonometric functions using only PHP and if needed, SQL. I'm aware of the GD library, but none of the functions were helpful. I wrote a small script, although it doesn't really work either. My goal is to do the following:
1) Allow for trig function name, starting value and end value to be added as parameters. 2) Check whether a function is cos, tan or sin. 3) Loop through all values given starting and end values in degrees, and convert to radians. 4) "Add" all values to an array, if needed, and graph the function given the points.
After looping through all the values of the function, what needs to be done to graph the function? Do the values have to be in a seperate array? What functions need to be used for graphing?
<?php

header("Content-type: image/png");

function graphFunction($function, $startDegree, $endDegree)
{

$functionList = array('cos', 'sin', 'tan');

if (strtolower($function) == 'cos')
{
    $cosValues = array();
    for ($c = $startDegree; $c < $endDegree; $c++)
    {

        array_push($cosValues, cos(deg2rad($c)));

        $graph = imagecreatetruecolor(500,250);
            $col_poly = imagecolorallocate($graph, 255, 255, 255);
            imagepolygon($graph, [the cosine values] , 34, $col_poly);
            imagepng($graph);
            imagedestroy($graph);

       }

   }

}

echo graphFunction('cos', 0, 360);

?>
This is supposed to be a sample function, so no need to be criticizing the useless control structure as there are ways to store many things in a database, whatsoever. I hope for some feedback, and hopefully it is possible with PHP.

No comments:

Post a Comment