Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: First GD Experiment

1. ## First GD Experiment

Well, I've been bored at work and experimenting with GD to see if I can draw some pretty cool stuff. I've so far only gotten a small graph up, but I've managed to make it rainbow colored and it's very easy to change the side and viewing area of the graph in the code(it's an experiment not a hr project), also the func variable in the for loop will let you change the actual line.

Oh yeah, the viewing area so far appears to have to be centered on the origin. I'm not sure why, but if you set it to {(6,12),(4,8)} or something, you get blank no matter what you put in so, whatever. I'm not sure, but I tried to make it handle those. I've only worked on this for about a day or two so whatever.

If you think it's cool, or have any ideas let me know. This is my first post here but I've been coding for a few years, just took about a year or two break so I'm just starting up in relearning things.

PHP Code:
``` <?php \$color_cur=0; \$xcur=0;\$xlas=0; \$ycur=0;\$ylas=0;        // Variables I use later \$func=0;\$ii=0;            // From C++ OCD. \$pi=3.14159265358979323;                 // Define sizes and whatnot \$sizw=1400;            // Width of the image \$sizh=600;            // Height of the image \$xmin=-60;            // Minimum x Value \$xmax=60;            // Maximum x Value \$ymin=-3;            // Minimum y Value \$ymax=3;            // Maximum y Value                 // These values should give me a value                 //  that I can use to exchange between                 //  the graph and image values \$normx= \$sizw / abs(\$xmax - \$xmin);    // Normalizing x minimum value \$normy= \$sizh / abs(\$ymax - \$ymin);    // Normalizing y maximum value \$adjux= \$sizw / 2;            // x Adjustment \$adjuy= \$sizh / 2;            // y Adjustment \$incr = (abs(\$xmax - \$xmin))/\$sizw;    // The Increment, or how accurate the graph is             // Now for some GD colors and line drawing \$image = imagecreatetruecolor(\$sizw,\$sizh);    // Create a blank image \$color_white = imagecolorallocate(\$image, 255, 255, 255);    // The color white \$color_red   = imagecolorallocate(\$image, 255,   0,   0);    // The color red \$color_blue  = imagecolorallocate(\$image,   0,   0, 255);    // The color blue \$color_green = imagecolorallocate(\$image,   0, 128,   0);    // The color green \$color_yellow= imagecolorallocate(\$image, 255, 255,   0);    // The color yellow \$color_purple= imagecolorallocate(\$image,   0, 255, 255);    // The color purple \$color_light = imagecolorallocate(\$image, 164,   0,   0);    // Opaque white \$color_orange= imagecolorallocate(\$image, 255,  65,   0);    // The color orange \$color_indigo= imagecolorallocate(\$image,  75,   0, 130);    // The color indigo \$color_violet= imagecolorallocate(\$image, 238, 130, 238);    // The color violet // Draw the axes and the grid // This just draws it in the center of the image. // Right now I'm too busy making the line rainbow // to be concerned about zooming in on non-origin // points.  Also a polar version incoming. imageline( \$image , \$adjux , 0 , \$adjux , \$sizh , \$color_white ); imageline( \$image , 0 , \$adjuy , \$sizw , \$adjuy , \$color_white ); function adjustacolor(\$inpout) {     global    \$color_red,    // See, this is the whole reason that I like         \$color_orange,    // C++ more than PHP, because it has global         \$color_yellow,    // variables that don't have to be defined         \$color_green,    // in every function they're used in, and if         \$color_blue,    // you're using, say, 7 of them then the         \$color_indigo;    // line that would list them all is going to         \$color_violet;    // be too long so they have to be on 7 lines   switch(round(abs(\$inpout))%6) {    // For fun, set this to rand()%6     case 0:     return \$color_red;    // I think     break;            // this one     case 1:            // is very     return \$color_orange;    // self ex-     break;            // -planat-     case 2:            // -ory     return \$color_yellow;     break;     case 3:     return \$color_green;     break;     case 4:     return \$color_blue;     break;     case 5:     return \$color_indigo;     break;     case 6:     return \$color_violet;     break;   } } // Draw the function for( \$ii = \$xmin ; \$ii < \$xmax ; \$ii += \$incr ) {   \$func = cos(\$ii/2);    // \$func is y, \$ii represents x   \$xcur = \$ii * \$normx + \$adjux;    // Normalize and adjust X   \$ycur = \$func * \$normy + \$adjuy;    // Normalize and adjust Y         \$ycur = \$sizh - \$ycur;    // Y Reversal   if(\$ii == \$xmin) { \$xlas = \$xcur; \$ylas = \$ycur; }    // Prevent line from origin   \$color_cur = adjustacolor(\$ii);    // Make the line rainbow function   imageline( \$image , \$xlas , \$ylas , \$xcur , \$ycur , \$color_cur );    // Draw   \$xlas = \$xcur;    // Store X   \$ylas = \$ycur;    // Store Y } // Output the picture to the browser header('Content-type: image/png');    // It's a PNG! imagepng(\$image);            // Make it one! imagedestroy(\$image);            // DESTROY!!!!11 ?> ```

• Originally Posted by enhancin
PHP Code:
``` <?php     global    \$color_red,    // See, this is the whole reason that I like         \$color_orange,    // C++ more than PHP, because it has global         \$color_yellow,    // variables that don't have to be defined         \$color_green,    // in every function they're used in, and if         \$color_blue,    // you're using, say, 7 of them then the         \$color_indigo;    // line that would list them all is going to         \$color_violet;    // be too long so they have to be on 7 lines // Output the picture to the browser header('Content-type: image/png');    // It's a PNG! imagepng(\$image);            // Make it one! imagedestroy(\$image);            // DESTROY!!!!11 ?> ```
Pretty cool.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•