Zend - The PHP Company




Graphics

Add Code


Star Generator  

Type: class
Added by: rustybrick
Entered: 12/03/2001
Last modified: 03/12/2000
Rating: - (fewer than 3 votes)
Views: 6034
a class to generate stars given a radius and the amount of points on the star


<?  

/* ****************************************************
   STAR - class and example to generate a sky of stars
   
   Written By:     Ronnie Schwartz <ronnie@rustybrick.com>
                   3/2001
   ****************************************************
*/


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


// CONSTANTS
//
    
$AMT_STARS  40;            // how many stars do you want
    
$APP_WIDTH  150;            // width of the applet
    
$APP_HEIGHT 150;            // height of the applet
        
    
srand((double)microtime()*1000000);    // init random generator
    
    
$im imagecreate ($APP_WIDTH$APP_HEIGHT);
    
    
$black ImageColorAllocate ($im000);
    
    
ImageRectangle($im00$APP_WIDTH$APP_HEIGHT$black); // set the background to black (starfield)
    
    // produce AMT_STARS stars at random locations, sizes and shapes
    //
    
for ($x=0$x<$AMT_STARS$x++) {
        
$x_offset rand(0,$APP_WIDTH);
        
$y_offset rand(0,$APP_HEIGHT);
        
$radius   rand(1,$APP_WIDTH/10);
        
$points   rand(4,8);
            
        
$myStar = new Star($radius,$points);                    // init star object
            
        
$myStar->generateStar($x_offset,$y_offset,$im);            // generate star
    
}
    
    
ImageJpeg($im,'',100);






//***********************************************************************//
// Star - used to generate a star, given the radius and amount of points //
//***********************************************************************//
class Star
{
    var 
$radius;        // radius of the star
    
var $amtPoints;        // amount of points
    
var $amtSides;        // amount of sides (points * 2)


    // Constructor - sets the radius, amtPoints and amtSides variables
    //
    
function Star($r$n)
    {
        
$this->radius $r;
        
$this->amtPoints $n;
        
$this->amtSides $this->amtPoints 2;
    }
    
    
// Generates a star given the location of the star and the graphics object to paint on
    //
    
function generateStar($x_offset$y_offset, &$img)
    {
        
$pointA=0;        // used to count the point number from 0 to amtPoints-1 - for outside points
        
$pointB=0;        // used to count the point number from 0 to amtPoints-1 - for inside points
        
        
for ($i=0$i<$this->amtSides$i++) {
            if (
$i == 0) {                                                        // even = outside points
                
$x_coords[$i] = $this->getX($this->amtPoints$pointA$this->radius$x_offset0.0);
                
$y_coords[$i] = $this->getY($this->amtPoints$pointA$this->radius$y_offset0.0);
                
$pointA++;
            }
            else {                                                                    
// odd = inside points
                
$x_coords[$i] = $this->getX($this->amtPoints$pointB$this->radius/2, ($this->radius/2) + $x_offset0.5);
                
$y_coords[$i] = $this->getY($this->amtPoints$pointB$this->radius/2, ($this->radius/2) + $y_offset0.5);
                
$pointB++;
            }
        }
        
        
// merge the x_coords and y_coords array into one
        
$point 0;
        for (
$i=0$i<$this->amtSides*2$i+=2) {
            
$coords[$i] = $x_coords[$point];
            
$coords[$i+1] = $y_coords[$point];
            
            
$point++;
        }
        
        
$white ImageColorAllocate ($img255255255);              // set stars color to white
        
imageFilledPolygon($img$coords$this->amtSides$white);        // paint the star
    
}
    
    
// Two methods that get the x and y coordinates of a circle given the following information:
    //     totalPoints - the total amount of points to generate on a circumference
    //     pointNum    - the number of the point you want to get (0 to totalPoints-1)
    //     radius        - the radius of the circle
    //     offset        - the amount of pixels the point should be offset
    //   rotate        - the amount to rotate the circle (needed to draw inner circle and make star)
    
    //    getX - generates x coordinates using cos()
    
function getX($totalPoints$pointNum$radius$offset$rotate)
    {
        
$PI 3.14;
        return (int) (
cos$PI * ($pointNum $rotate) / $totalPoints) * $radius ) + $radius $offset;
    }
    
    
//    getY - generates y coordinates using sin()
    
function getY($totalPoints$pointNum$radius$offset$rotate)
    {
        
$PI 3.14;
        return (int) (
sin$PI * ($pointNum $rotate) / $totalPoints) * $radius ) + $radius $offset;
    }
}

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories