Horde_Image::arcPoints PHP Method

arcPoints() public static method

Returns point coordinates at the limits of an arc.
public static arcPoints ( integer $r, integer $start, integer $end ) : array
$r integer The radius of the arc.
$start integer The starting angle.
$end integer The ending angle.
return array The start point (x1,y1), end point (x2,y2), and anchor point (x3,y3).
    public static function arcPoints($r, $start, $end)
    {
        // Start point.
        $pts['x1'] = $r * cos(deg2rad($start));
        $pts['y1'] = $r * sin(deg2rad($start));
        // End point.
        $pts['x2'] = $r * cos(deg2rad($end));
        $pts['y2'] = $r * sin(deg2rad($end));
        // Anchor point.
        $pts['x3'] = $pts['y3'] = 0;
        // Shift to positive.
        if ($pts['x1'] < 0) {
            $pts['x2'] += abs($pts['x1']);
            $pts['x3'] += abs($pts['x1']);
            $pts['x1'] = 0;
        }
        if ($pts['x2'] < 0) {
            $pts['x1'] += abs($pts['x2']);
            $pts['x3'] += abs($pts['x2']);
            $pts['x2'] = 0;
        }
        if ($pts['x3'] < 0) {
            $pts['x1'] += abs($pts['x3']);
            $pts['x2'] += abs($pts['x3']);
            $pts['x3'] = 0;
        }
        if ($pts['y1'] < 0) {
            $pts['y2'] += abs($pts['y1']);
            $pts['y3'] += abs($pts['y1']);
            $pts['y1'] = 0;
        }
        if ($pts['y2'] < 0) {
            $pts['y1'] += abs($pts['y2']);
            $pts['y3'] += abs($pts['y2']);
            $pts['y2'] = 0;
        }
        if ($pts['y3'] < 0) {
            $pts['y1'] += abs($pts['y3']);
            $pts['y2'] += abs($pts['y3']);
            $pts['y3'] = 0;
        }
        return $pts;
    }

Usage Example

Example #1
0
 /**
  * Draws an arc.
  *
  * @param integer $x      The x coordinate of the centre.
  * @param integer $y      The y coordinate of the centre.
  * @param integer $r      The radius of the arc.
  * @param integer $start  The start angle of the arc.
  * @param integer $end    The end angle of the arc.
  * @param string $color   The line color of the arc.
  * @param string $fill    The fill color of the arc (defaults to none).
  */
 public function arc($x, $y, $r, $start, $end, $color = 'black', $fill = 'none')
 {
     $points = Horde_Image::arcPoints($r, $start, $end);
     $points['x1'] += $x;
     $points['x2'] += $x;
     $points['x3'] += $x;
     $points['y1'] += $y;
     $points['y2'] += $y;
     $points['y3'] += $y;
     try {
         $draw = new ImagickDraw();
         $draw->setStrokeColor(new ImagickPixel($color));
         $draw->setFillColor(new ImagickPixel($fill));
         $draw->arc($x - $r, $y - $r, $x + $r, $y + $r, $start, $end);
     } catch (ImagickDrawException $e) {
         throw new Horde_Image_Exception($e);
     } catch (ImagickPixelException $e) {
         throw new Horde_Image_Exception($e);
     }
     // If filled, draw the outline.
     if (!empty($fill)) {
         $mid = round(($start + $end) / 2);
         list($x1, $y1) = Horde_Image::circlePoint($start, $r * 2);
         list($x2, $y2) = Horde_Image::circlePoint($mid, $r * 2);
         list($x3, $y3) = Horde_Image::circlePoint($end, $r * 2);
         $verts = array(array('x' => $x + round($x3), 'y' => $y + round($y3)), array('x' => $x, 'y' => $y), array('x' => $x + round($x1), 'y' => $y + round($y1)));
         if ($mid > 90) {
             $verts1 = array(array('x' => $x + round($x2), 'y' => $y + round($y2)), array('x' => $x, 'y' => $y), array('x' => $x + round($x1), 'y' => $y + round($y1)));
             $verts2 = array(array('x' => $x + round($x3), 'y' => $y + round($y3)), array('x' => $x, 'y' => $y), array('x' => $x + round($x2), 'y' => $y + round($y2)));
             $this->polygon($verts1, $fill, $fill);
             $this->polygon($verts2, $fill, $fill);
         } else {
             $this->polygon($verts, $fill, $fill);
         }
         $this->polyline($verts, $color);
     }
     try {
         $this->_imagick->drawImage($draw);
     } catch (ImagickException $e) {
         throw new Horde_Image_Exception($e);
     }
     $draw->destroy();
 }
All Usage Examples Of Horde_Image::arcPoints