Jump to content


New Members
  • Content count

  • Joined

  • Last visited

Community Reputation

0 Neutral

About dragonmom0

  • Rank
  1. dragonmom0

    GD or SVG graphing math

    The original problem presented includes an error in how to integrate the value of PHP variables inside SVG code. Since the point of the posting was to solve this problem, I debugged the original code and present the solution below. As Barand points out, the "final" correct code posted by Possien has a no operation line "$html = $html++;" and doesn't clearly "correct" his original problem which is how to integrate PHP variables within SVG code. The coder needed to "echo" the variables $x and $y not just reference them. Let me explain Barand's correction first and then mine. Barand first corrects mathematical errors: Barand changes the size of the radius from 25 to 125. In actuality the size has to be > 48 to reach the edge of the size-50 radius circle with a stroke size of 2. Barand also corrects the calc for $x and $y. The addition of the number 50 in each x and y calculation is to take into consideration the center point of the circle which is x=50, y=50. Cosine and Sine functions require parameters in radians not degrees hence the deg2rad() function which translates the number 45 degrees to radians. The "Final" code Possien posted changes how he references variables but I felt it confused the point. He was so close originally, I thought I would point out the small correction for other's benefit. My correction is the fourth changed line. Echo the variables don't just reference them. From the first set of code, change only these four lines: $rad = 25; $x = round($rad*cos(45)); $y = round($rad*sin(45)); <line x1="50" y1="50" x2="<?php $x;?>" y2="<?php $y;?>" style="stroke:red;stroke-width:2"/ to these lines: $rad = 48; $x = 50+round($rad*cos(deg2rad(45))); $y = 50-round($rad*sin(deg2rad(45))); <line x1="50" y1="50" x2="<?php echo $x;?>" y2="<?php echo $y;?>" style="stroke:red;stroke-width:2"/ Final Code which works is as follows: <!DOCTYPE html> <head> <title>SVG</title> <meta charset="utf-8" /> </head> <body> <h2>HTML5 SVG Plot Polar to XY Line</h2> <?php $rad = 48; $x = 50+round($rad*cos(deg2rad(45))); $y = 50-round($rad*sin(deg2rad(45))); echo "Value of x: ".$x."<br>"; echo "Value of y: ".$y."<br>"; echo "Plot of a 45 deg's with PHP in red, correct plot in blue"; ?> <svg id="svgelem" height="200" xmlns="http://www.w3.org/2000/svg"> <svg width="100" height="100"> <circle cx="50" cy="50" r="48" stroke="green" stroke-width="2" fill="#EFA" /> <line x1="50" y1="50" x2="<?php echo $x;?>" y2="<?php echo $y;?>" style="stroke:red;stroke-width:2"/> </svg> </body> </html>

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.