Pop\Image\Svg::drawArc PHP Метод

drawArc() публичный Метод

Method to add an arc to the image.
public drawArc ( integer $x, integer $y, integer $start, integer $end, integer $w, integer $h = null ) : Svg
$x integer
$y integer
$start integer
$end integer
$w integer
$h integer
Результат Svg
    public function drawArc($x, $y, $start, $end, $w, $h = null)
    {
        if (null === $h) {
            $h = $w;
        }
        $sX = round($w * cos($start / 180 * pi()));
        $sY = round($h * sin($start / 180 * pi()));
        $eX = round($w * cos($end / 180 * pi()));
        $eY = round($h * sin($end / 180 * pi()));
        $centerPoint = array('x' => $x, 'y' => $y);
        $startPoint = array('x' => $x + $sX, 'y' => $y + $sY);
        $endPoint = array('x' => $x + $eX, 'y' => $y + $eY);
        $startQuad = $this->getQuadrant($startPoint, $centerPoint);
        $endQuad = $this->getQuadrant($endPoint, $centerPoint);
        $corner1 = array('x' => $this->width, 'y' => $this->height);
        $corner2 = array('x' => 0, 'y' => $this->height);
        $corner3 = array('x' => 0, 'y' => 0);
        $corner4 = array('x' => $this->width, 'y' => 0);
        $polyPoints = array($centerPoint, $startPoint);
        switch ($startQuad) {
            case 1:
                if ($endQuad == 1) {
                    $polyPoints[] = $corner1;
                    $polyPoints[] = array('x' => $endPoint['x'], 'y' => $this->height);
                } else {
                    if ($endQuad == 2) {
                        $polyPoints[] = $corner1;
                        $polyPoints[] = $corner2;
                    } else {
                        if ($endQuad == 3) {
                            $polyPoints[] = $corner1;
                            $polyPoints[] = $corner2;
                            $polyPoints[] = $corner3;
                        } else {
                            if ($endQuad == 4) {
                                $polyPoints[] = $corner1;
                                $polyPoints[] = $corner2;
                                $polyPoints[] = $corner3;
                                $polyPoints[] = $corner4;
                            }
                        }
                    }
                }
                break;
            case 2:
                if ($endQuad == 1) {
                    $polyPoints[] = $corner2;
                    $polyPoints[] = $corner3;
                    $polyPoints[] = $corner4;
                    $polyPoints[] = $corner1;
                } else {
                    if ($endQuad == 2) {
                        $polyPoints[] = $corner2;
                        $polyPoints[] = array('x' => 0, 'y' => $endPoint['y']);
                    } else {
                        if ($endQuad == 3) {
                            $polyPoints[] = $corner2;
                            $polyPoints[] = $corner3;
                        } else {
                            if ($endQuad == 4) {
                                $polyPoints[] = $corner2;
                                $polyPoints[] = $corner3;
                                $polyPoints[] = $corner4;
                            }
                        }
                    }
                }
                break;
            case 3:
                if ($endQuad == 1) {
                    $polyPoints[] = $corner3;
                    $polyPoints[] = $corner4;
                    $polyPoints[] = $corner1;
                } else {
                    if ($endQuad == 2) {
                        $polyPoints[] = $corner3;
                        $polyPoints[] = $corner4;
                        $polyPoints[] = $corner1;
                        $polyPoints[] = $corner2;
                    } else {
                        if ($endQuad == 3) {
                            $polyPoints[] = $corner3;
                            $polyPoints[] = array('x' => $endPoint['x'], 'y' => 0);
                        } else {
                            if ($endQuad == 4) {
                                $polyPoints[] = $corner3;
                                $polyPoints[] = $corner4;
                            }
                        }
                    }
                }
                break;
            case 4:
                if ($endQuad == 1) {
                    $polyPoints[] = $corner4;
                    $polyPoints[] = $corner1;
                } else {
                    if ($endQuad == 2) {
                        $polyPoints[] = $corner4;
                        $polyPoints[] = $corner1;
                        $polyPoints[] = $corner2;
                    } else {
                        if ($endQuad == 3) {
                            $polyPoints[] = $corner4;
                            $polyPoints[] = $corner1;
                            $polyPoints[] = $corner2;
                            $polyPoints[] = $corner3;
                        } else {
                            if ($endQuad == 4) {
                                $polyPoints[] = $corner4;
                                $polyPoints[] = array('x' => $this->width, 'y' => $endPoint['y']);
                            }
                        }
                    }
                }
                break;
        }
        $polyPoints[] = $endPoint;
        $stamp = rand();
        $defs = $this->resource->addChild('defs');
        $clip = $defs->addChild('clipPath');
        $clip->addAttribute('id', 'polyClip' . $stamp);
        $formattedPoints = array();
        foreach ($polyPoints as $point) {
            $formattedPoints[] = $point['x'] . ',' . $point['y'];
        }
        $poly = $clip->addChild('polygon');
        $poly->addAttribute('points', implode(' ', $formattedPoints));
        $ellipse = $this->resource->addChild('ellipse');
        $ellipse->addAttribute('style', 'clip-path: url(#polyClip' . $stamp . ');');
        $ellipse->addAttribute('cx', $x . $this->units);
        $ellipse->addAttribute('cy', $y . $this->units);
        $ellipse->addAttribute('rx', $w . $this->units);
        $ellipse->addAttribute('ry', (null === $h ? $w : $h) . $this->units);
        $ellipse = $this->setStyles($ellipse);
        return $this;
    }

Usage Example

Пример #1
0
 public function testAddArc()
 {
     $s = new Svg('graph.svg', '640px', '480px');
     $s->setStrokeColor(new Rgb(0, 0, 0))->drawArc(320, 240, 0, 120, 100);
     $s->setBackgroundColor(new Rgb(255, 0, 0));
     $s->drawArc(320, 240, 0, 120, 100, 100);
     $s->setFillColor(new Rgb(255, 0, 0));
     $s->drawArc(320, 240, 0, 120, 100, 100);
     $this->assertEquals(640, $s->getWidth());
 }