How to Draw Sinusoids with PHP?

Discussion in 'PHP' started by tankard, Oct 28, 2009.

  1. #1
    Hi,

    I'm learning to draw with PHP :D I can do shapes and lines and all sort of stuff. Can I also draw sinusoids or other wave-form shapes that are generated by mathematical formulas? Any ideas please!
     
    tankard, Oct 28, 2009 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    Yes, you can.
    change x from 0 to 100, for example, get y=100*sin(x) and place dots on those coordinates.
     
    AsHinE, Oct 28, 2009 IP
  3. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that's cool, i always wanted to use PHP for math :D

    @Ashine: btw, can you give a simple example of that?
     
    szalinski, Oct 28, 2009 IP
  4. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #4
    Try this :)
    
    <?php
    //create our image 1000*400 pixels
    $im = imagecreatetruecolor(1000, 400);
    // set white color
    $w = imagecolorallocate($im,255,255,255);
    for ($x=1;$x<=1000;$x++) {
    	$y = 200 + 200*sin($x/100);
            //put pixels
    	imagesetpixel($im,$x,$y,$w);
    }
    //output image
    header('Content-Type: image/png');
    imagepng($im);
    ?>
    
    PHP:
    $y = 200 + 200*sin($x/100);
    first 200 is to place graph in middle of image, cause sin range is -1..1, so 200*sin($x/100); will give -200..200
    play with 100 - change it to 50 or 200 to set horizontal scale,
     
    AsHinE, Oct 28, 2009 IP
    tankard likes this.
  5. tankard

    tankard Well-Known Member

    Messages:
    1,018
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    185
    #5
    Wow, AsHinE, that is marvelous! Get some greens for that!!!
     
    tankard, Oct 28, 2009 IP