Ejemplo n.º 1
0
 /**
  * Parameter array:
  * 
  * 'connect': bool [optional] Specifies whether the start point should be
  * connected  to the endpoint (closed polygon) or not (connected line)
  * 
  * 'fill': mixed [optional] The fill color
  * 
  * 'line': mixed [optional] The line color
  * @param array $params Parameter array
  */
 function polygon($params)
 {
     include_once 'Image/Canvas/Tool.php';
     $connectEnds = isset($params['connect']) ? $params['connect'] : false;
     $fillColor = isset($params['fill']) ? $params['fill'] : false;
     $lineColor = isset($params['line']) ? $params['line'] : false;
     if (!$connectEnds) {
         $fillColor = 'transparent';
     }
     $style = $this->_getLineStyle($lineColor) . $this->_getFillStyle($fillColor);
     $lastPoint = false;
     foreach ($this->_polygon as $point) {
         if ($lastPoint && isset($lastPoint['P1X']) && isset($lastPoint['P1Y']) && isset($lastPoint['P2X']) && isset($lastPoint['P2Y'])) {
             $dx = abs($point['X'] - $lastPoint['X']);
             $dy = abs($point['Y'] - $lastPoint['Y']);
             $d = sqrt($dx * $dx + $dy * $dy);
             if ($d > 0) {
                 $interval = 1 / $d;
                 for ($t = 0; $t <= 1; $t = $t + $interval) {
                     $x = Image_Canvas_Tool::bezier($t, $lastPoint['X'], $lastPoint['P1X'], $lastPoint['P2X'], $point['X']);
                     $y = Image_Canvas_Tool::bezier($t, $lastPoint['Y'], $lastPoint['P1Y'], $lastPoint['P2Y'], $point['Y']);
                     if (!isset($low['X'])) {
                         $low['X'] = $x;
                     } else {
                         $low['X'] = min($x, $low['X']);
                     }
                     if (!isset($high['X'])) {
                         $high['X'] = $x;
                     } else {
                         $high['X'] = max($x, $high['X']);
                     }
                     if (!isset($low['Y'])) {
                         $low['Y'] = $y;
                     } else {
                         $low['Y'] = min($y, $low['Y']);
                     }
                     if (!isset($high['Y'])) {
                         $high['Y'] = $y;
                     } else {
                         $high['Y'] = max($y, $high['Y']);
                     }
                     $polygon[] = $x;
                     $polygon[] = $y;
                 }
                 if ($t - $interval < 1) {
                     $x = Image_Canvas_Tool::bezier(1, $lastPoint['X'], $lastPoint['P1X'], $lastPoint['P2X'], $point['X']);
                     $y = Image_Canvas_Tool::bezier(1, $lastPoint['Y'], $lastPoint['P1Y'], $lastPoint['P2Y'], $point['Y']);
                     $polygon[] = $x;
                     $polygon[] = $y;
                 }
             }
         } else {
             if (!isset($low['X'])) {
                 $low['X'] = $point['X'];
             } else {
                 $low['X'] = min($point['X'], $low['X']);
             }
             if (!isset($high['X'])) {
                 $high['X'] = $point['X'];
             } else {
                 $high['X'] = max($point['X'], $high['X']);
             }
             if (!isset($low['Y'])) {
                 $low['Y'] = $point['Y'];
             } else {
                 $low['Y'] = min($point['Y'], $low['Y']);
             }
             if (!isset($high['Y'])) {
                 $high['Y'] = $point['Y'];
             } else {
                 $high['Y'] = max($point['Y'], $high['Y']);
             }
             $polygon[] = $point['X'];
             $polygon[] = $point['Y'];
         }
         $lastPoint = $point;
     }
     if (isset($polygon) && is_array($polygon)) {
         if ($connectEnds) {
             if (($fill = $this->_getFillStyle($fillColor, $low['X'], $low['Y'], $high['X'], $high['Y'])) !== false) {
                 ImageFilledPolygon($this->_canvas, $polygon, count($polygon) / 2, $fill);
             }
             if ($this->_antialias === 'driver') {
                 $pfirst = $p0 = false;
                 reset($polygon);
                 while (list(, $x) = each($polygon)) {
                     list(, $y) = each($polygon);
                     if ($p0 !== false) {
                         $this->_antialiasedLine($p0['X'], $p0['Y'], $x, $y, $lineColor);
                     }
                     if ($pfirst === false) {
                         $pfirst = array('X' => $x, 'Y' => $y);
                     }
                     $p0 = array('X' => $x, 'Y' => $y);
                 }
                 $this->_antialiasedLine($p0['X'], $p0['Y'], $pfirst['X'], $pfirst['Y'], $lineColor);
             } elseif (($line = $this->_getLineStyle($lineColor)) !== false) {
                 ImagePolygon($this->_canvas, $polygon, count($polygon) / 2, $line);
             }
         } else {
             $prev_point = false;
             if ($this->_antialias === 'driver') {
                 reset($polygon);
                 while (list(, $x) = each($polygon)) {
                     list(, $y) = each($polygon);
                     if ($prev_point) {
                         $this->_antialiasedLine($prev_point['X'], $prev_point['Y'], $x, $y, $lineColor);
                     }
                     $prev_point = array('X' => $x, 'Y' => $y);
                 }
             } elseif (($line = $this->_getLineStyle($lineColor)) !== false) {
                 reset($polygon);
                 while (list(, $x) = each($polygon)) {
                     list(, $y) = each($polygon);
                     if ($prev_point) {
                         ImageLine($this->_canvas, $prev_point['X'], $prev_point['Y'], $x, $y, $line);
                     }
                     $prev_point = array('X' => $x, 'Y' => $y);
                 }
             }
         }
     }
     parent::polygon($params);
 }