imagepolygon

(PHP 4, PHP 5)

imagepolygonDraws a polygon

Description

bool imagepolygon ( resource $image , array $points , int $num_points , int $color )

imagepolygon() creates a polygon in the given image.

Parameters

image

An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

points

An array containing the polygon's vertices, e.g.:

points[0] = x0
points[1] = y0
points[2] = x1
points[3] = y1

num_points

Total number of points (vertices).

color

A color identifier created with imagecolorallocate().

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example #1 imagepolygon() example

<?php
// Create a blank image
$image imagecreatetruecolor(400300);

// Allocate a color for the polygon
$col_poly imagecolorallocate($image255255255);

// Draw the polygon
imagepolygon($image, array(
        
0,   0,
        
100200,
        
300200
    
),
    
3,
    
$col_poly);

// Output the picture to the browser
header('Content-type: image/png');

imagepng($image);
imagedestroy($image);
?>

The above example will output something similar to:

Output of example : imagepolygon()

See Also

Коментарии

Here are some handy routines for rotation and translation of polygons.  Scaling could be added easily as well.

<?php
function translate_point(&$x,&$y,$angle,$about_x,$about_y,$shift_x,$shift_y)
{
   
$x -= $about_x;
   
$y -= $about_y;
   
$angle = ($angle 180) * M_PI;
/* math:
[x2,y2] = [x,  *  [[cos(a),-sin(a)],
           y]      [sin(a),cos(a)]]
==>
x = x * cos(a) + y*sin(a) 
y = x*-sin(a) + y*cos(a)
*/

   
$new_x $x cos($angle) - $y sin($angle);
   
$new_y $x sin($angle) + $y cos($angle);
   
$x $new_x$about_x $shift_x ;
   
$y $new_y $about_y $shift_y;
}

function 
translate_poly($point_array$angle$about_x$about_y,$shift_x,$shift_y)
{
   
$translated_poly = Array();
    while(
count($point_array) > 1)
    {
       
$temp_x array_shift($point_array);
       
$temp_y array_shift($point_array);
       
translate_point($temp_x$temp_y$angle$about_x$about_y,$shift_x$shift_y);
       
array_push($translated_poly$temp_x);
       
array_push($translated_poly$temp_y);
    }
    return 
$translated_poly;
}
?>
2001-02-17 21:07:01
http://php5.kiev.ua/manual/ru/function.imagepolygon.html
Function to get 5-sided polygon (pentagon) or star (pentagram) co-ords. 
<?php
function _makeFiveSidedStar$x$y$radius$shape='polygon'$spiky=NULL ) {
   
$point = array() ; // new array                                                                                                                   
   
$angle 360 ;                                                                                                                   
   
$point[0]['x'] = $x ;                                                                                                                 
   
$point[0]['y'] = $y $radius ;                                                                                                       
   
$point[2]['x'] = $x + ( $radius cosdeg2rad90 $angle ) ) ) ; 
   
$point[2]['y'] = $y - ( $radius sindeg2rad90 $angle ) ) ) ;
   
$point[4]['x'] = $x + ( $radius sindeg2rad180 - ( $angle*) ) ) ) ;
   
$point[4]['y'] = $y + ( $radius cosdeg2rad180 - ( $angle*) ) ) ) ;
   
$point[6]['x'] = $x - ( $radius sindeg2rad180 - ( $angle*) ) ) ) ;                                                           
   
$point[6]['y'] = $y + ( $radius cosdeg2rad180 - ( $angle*) ) ) ) ;
   
$point[8]['x'] = $x - ( $radius cosdeg2rad90 $angle ) ) ) ;                                                                   
   
$point[8]['y'] = $y - ( $radius sindeg2rad90 $angle ) ) ) ;
    if( 
$shape == 'star' ) {
        if( 
$spiky == NULL $spiky 0.5 // degree of spikiness, default to 0.5
       
$indent $radius $spiky ;
       
$point[1]['x'] = $x + ( $indent cosdeg2rad90 $angle/) ) ) ;                                                             
       
$point[1]['y'] = $y - ( $indent sindeg2rad90 $angle/) ) ) ;                                                     
       
$point[3]['x'] = $x + ( $indent sindeg2rad180 $angle ) ) ) ;                                                             
       
$point[3]['y'] = $y - ( $indent cosdeg2rad180 $angle ) ) ) ;
       
$point[5]['x'] = $x ;                                                                                                             
       
$point[5]['y'] = $y + ( $indent sindeg2rad180 $angle ) ) ) ;
       
$point[7]['x'] = $x - ( $indent sindeg2rad180 $angle ) ) ) ;                                                             
       
$point[7]['y'] = $y - ( $indent cosdeg2rad180 $angle ) ) ) ;                                                             
       
$point[9]['x'] = $x - ( $indent cosdeg2rad90 $angle/) ) ) ;                                                             
       
$point[9]['y'] = $y - ( $indent sindeg2rad90 $angle/) ) ) ;
    }
   
ksort$point ) ;
   
$coords = array() ;  // new array                                                                                                                 
   
foreach( $point as $pKey=>$pVal ) {                                                                                                   
        if( 
is_array$pVal ) ) {                                                                                                         
            foreach( 
$pVal as $pSubKey=>$pSubVal ) {                                                                                     
                if( !empty( 
$pSubVal ) ) array_push$coords$pSubVal ) ;                                                               
            }                                                                                                                             
        }                                                                                                                                 
    }
    return 
$coords ;
}
$values _makeFiveSidedStar10010050'star' ) ;
?>
2006-09-11 19:48:08
http://php5.kiev.ua/manual/ru/function.imagepolygon.html
Function to draw a n-sided regular polygon

<?php
$img 
imagecreatetruecolor(1360,768);

function 
regularPolygon($img,$x,$y,$radius,$sides,$color)
{
   
$points = array();
    for(
$a 0;$a <= 360$a += 360/$sides)
    {
       
$points[] = $x $radius cos(deg2rad($a));
       
$points[] = $y $radius sin(deg2rad($a));
    }
    return 
imagepolygon($img,$points,$sides,$color);
}

regularPolygon($img,1360/2,768/2,300,8,0xffffff);//Test draw

header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
2012-05-21 13:39:47
http://php5.kiev.ua/manual/ru/function.imagepolygon.html
Here's a function to draw a n-sided star:

<?php
function drawStar($img,$x,$y,$radius,$sides,$color,$spikness=0.5)
{
   
$point =array();
   
$t 0;
    for(
$a 0;$a <= 360;$a += 360/($sides*2))
    {
       
$t++;
        if(
$t == 0)
        {
           
$point[] = $x + ($radius $spikness) * cos(deg2rad($a));
           
$point[] = $y + ($radius $spikness) * sin(deg2rad($a));
        }else{
           
$point[] = $x $radius cos(deg2rad($a));
           
$point[] = $y $radius sin(deg2rad($a));
        }
    }
    return 
imagepolygon($img,$point,$sides*2,$color);
}
?>
2012-05-23 14:21:34
http://php5.kiev.ua/manual/ru/function.imagepolygon.html

    Поддержать сайт на родительском проекте КГБ