Jump to content

Mathematical help


murfitUK

Recommended Posts

I am attempting to use php and a pdf creator to generate a chart. It is the mathematical bits I need help with. Basically, the chart has 7 lines (spokes) radiating at equal angles from the centre.

<?php// create the pdfinclude ("class.ezpdf.php");$pdf =& new Cezpdf();$pdf -> selectFont("./fonts/Helvetica.afm"); // these value determine the position and size of the graph// (0,0 is the bottom left hand corner of the page)// x1 is the x centre of the graph$x1 = 200;// y1 is the y centre of the graph$y1 = 200;// s is the scalar$s = 100; // create the skeleton of the graphfor ($spoke=1; $spoke<=7; $spoke++)  {  $angle = ($spoke-1) * (360/7);  $x2 = (sin($angle) * $s) + $x1;  $y2 = (cos($angle) * $s) + $y1;  // draw a line from (x1,y1) to (x2,y2)  $pdf -> line($x1, $y1, $x2, $y2);  } // output the result$pdf -> ezStream();?>

The first spoke works OK but it all goes downhill from there. I have attached the pdf output so you can see what I mean. What makes this worse is that my university degree is in mathematics!!! But it was 30 years ago and I've forgotten everything I ever learned.

graph.pdf

Link to comment
Share on other sites

The sin() and cos() functions use radians. Instead of 360/7 use 2*PI / 7. Also, since this is a value that never changes, calculate it once outside of the loop.

Link to comment
Share on other sites

I'm so glad it turned out to be a php problem and not that I'd forgotten everything I had ever learned! I didn't realise that sin and cos needed radians and not degrees. Interestingly, my apache server returns PI as 0. I have to use M_PI. Don't know why. The succesful result is attached as graph2. The eventual aim is for my users to be able to generate a chart like that attached as example. At the moment they use excel but want the data stored on their client database instead. I have already got the database set up for them to enter the data and now concentrating on creating the results. Thanks everyone.

graph2.pdf

example.pdf

Link to comment
Share on other sites

Interestingly, my apache server returns PI as 0. I have to use M_PI. Don't know why. The succesful result is attached as graph2.
That's not Apache, that's PHP. http://php.net/manual/en/math.constants.php PHP has support for predefined constants, and some specifically for Math, which is where the M_ comes from, presumably in attempt to namespace these constants.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...