/** * 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); }
/** * Calculates a Bezier control point, this function must be called for BOTH * X and Y coordinates (will it work for 3D coordinates!?) * * @param double $p1 1st point * @param double $p2 Point to * @param double $factor Mirror factor, 0 returns P2, 1 returns a pure * mirror, i.e. P1 on the exact other side of P2 * @return double P1 mirrored in P2 by Factor * @static */ function controlPoint($p1, $p2, $factor, $smoothFactor = 0.75) { $sa = Image_Canvas_Tool::mirror($p1, $p2, $smoothFactor); $sb = Image_Canvas_Tool::mid($p2, $sa); $m = Image_Canvas_Tool::mid($p2, $factor); $pC = Image_Canvas_Tool::mid($sb, $m); return $pC; }
/** * Draw a line * * Parameter array: * 'x0': int X start point * 'y0': int Y start point * 'x1': int X end point * 'y1': int Y end point * 'end0': string [optional] The end type of end0 (the start) * 'end1': string [optional] The end type of end1 (the end) * 'size0': int [optional] The size of end0 * 'size1': int [optional] The size of end1 * 'color': mixed [optional] The line color * @param array $params Parameter array */ function line($params) { $x0 = $this->_getX($params['x0']); $y0 = $this->_getY($params['y0']); $x1 = $this->_getX($params['x1']); $y1 = $this->_getY($params['y1']); if (isset($params['end0'])) { $angle = Image_Canvas_Tool::getAngle($x1, $y1, $x0, $y0); $this->drawEnd(array('end' => $params['end0'], 'x' => $params['x0'], 'y' => $params['y0'], 'angle' => $angle, 'color' => isset($params['color0']) ? $params['color0'] : false, 'size' => $params['size0'])); } if (isset($params['end1'])) { $angle = Image_Canvas_Tool::getAngle($x0, $y0, $x1, $y1); //print "<pre>"; var_dump($params, $angle); print "</pre>"; $this->drawEnd(array('end' => $params['end1'], 'x' => $params['x1'], 'y' => $params['y1'], 'angle' => $angle, 'color' => isset($params['color1']) ? $params['color1'] : false, 'size' => $params['size1'])); } $this->_reset(); }