1 | <?php |
---|
2 | |
---|
3 | namespace Cerdic\Geometrize\Shape; |
---|
4 | |
---|
5 | use \Cerdic\Geometrize\Exporter\SvgExporter; |
---|
6 | |
---|
7 | class Ellipse implements Shape { |
---|
8 | |
---|
9 | /** |
---|
10 | * @var int |
---|
11 | */ |
---|
12 | protected $x; |
---|
13 | |
---|
14 | /** |
---|
15 | * @var int |
---|
16 | */ |
---|
17 | protected $y; |
---|
18 | |
---|
19 | /** |
---|
20 | * @var int |
---|
21 | */ |
---|
22 | protected $rx; |
---|
23 | |
---|
24 | /** |
---|
25 | * @var int |
---|
26 | */ |
---|
27 | protected $ry; |
---|
28 | |
---|
29 | /** |
---|
30 | * @var int |
---|
31 | */ |
---|
32 | protected $xBound; |
---|
33 | |
---|
34 | /** |
---|
35 | * @var int |
---|
36 | */ |
---|
37 | protected $yBound; |
---|
38 | |
---|
39 | /** |
---|
40 | * @var int |
---|
41 | */ |
---|
42 | public $color; |
---|
43 | |
---|
44 | /** |
---|
45 | * Rasterized lines |
---|
46 | * @var null|array |
---|
47 | */ |
---|
48 | protected $lines = null; |
---|
49 | |
---|
50 | public function __construct($xBound, $yBound, $sizeFactor=1.0){ |
---|
51 | $this->x = mt_rand(0, $xBound-1); |
---|
52 | $this->y = mt_rand(0, $yBound-1); |
---|
53 | $this->rx = intval(mt_rand(1, $xBound >> 2) * $sizeFactor); |
---|
54 | $this->ry = intval(mt_rand(1, $yBound >> 2) * $sizeFactor); |
---|
55 | $this->xBound = $xBound; |
---|
56 | $this->yBound = $yBound; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Rasterize the shape |
---|
61 | * @return array |
---|
62 | * @throws \Exception |
---|
63 | */ |
---|
64 | public function rasterize(){ |
---|
65 | if (!$this->lines){ |
---|
66 | $this->lines = []; |
---|
67 | |
---|
68 | $aspect = $this->rx/$this->ry; |
---|
69 | $w = $this->xBound; |
---|
70 | $h = $this->yBound; |
---|
71 | |
---|
72 | for ($dy = 0; $dy<$this->ry; $dy++) { |
---|
73 | $y1 = $this->y-$dy; |
---|
74 | $y2 = $this->y+$dy; |
---|
75 | |
---|
76 | $s = null; |
---|
77 | if ($y1>=0 or $y2<$h){ |
---|
78 | $s = intval(sqrt($this->ry*$this->ry-$dy*$dy)*$aspect); |
---|
79 | $x1 = max($this->x-$s, 0); |
---|
80 | $x2 = min($this->x+$s, $w-1); |
---|
81 | } |
---|
82 | |
---|
83 | if ($y1>=0){ |
---|
84 | $this->lines[] = ['y' => $y1, 'x1'=>$x1, 'x2'=>$x2]; |
---|
85 | } |
---|
86 | if ($y2<$h){ |
---|
87 | $this->lines[] = ['y' => $y2, 'x1'=>$x1, 'x2'=>$x2]; |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | return $this->lines; |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Mutate the shape |
---|
96 | * @throws \Exception |
---|
97 | */ |
---|
98 | public function mutate(){ |
---|
99 | $r = mt_rand(0, 2); |
---|
100 | switch ($r) { |
---|
101 | case 0: |
---|
102 | $this->x += mt_rand(-16, +16); |
---|
103 | $this->x = max(min($this->x, $this->xBound-1),0); |
---|
104 | $this->y += mt_rand(-16, +16); |
---|
105 | $this->y = max(min($this->y, $this->yBound-1),0); |
---|
106 | break; |
---|
107 | |
---|
108 | case 1: |
---|
109 | $this->rx += mt_rand(-16, +16); |
---|
110 | $this->rx = max(min($this->rx, $this->xBound-1),1); |
---|
111 | break; |
---|
112 | case 2: |
---|
113 | $this->ry += mt_rand(-16, +16); |
---|
114 | $this->ry = max(min($this->ry, $this->yBound-1),1); |
---|
115 | break; |
---|
116 | } |
---|
117 | |
---|
118 | // force to rasterize the new shape |
---|
119 | $this->lines = null; |
---|
120 | |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Get an approximative size ratio of the shape vs the bounds |
---|
125 | * @return float|int |
---|
126 | */ |
---|
127 | public function getSizeFactor(){ |
---|
128 | return $this->rx / $this->xBound + $this->ry / $this->yBound; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * @param int $xBound |
---|
133 | * @param int $yBound |
---|
134 | */ |
---|
135 | public function rescale($xBound, $yBound){ |
---|
136 | $xScale = ($xBound-1) / ($this->xBound-1); |
---|
137 | $yScale = ($yBound-1) / ($this->yBound-1); |
---|
138 | $this->xBound = $xBound; |
---|
139 | $this->yBound = $yBound; |
---|
140 | $this->x = intval(round($this->x*$xScale)); |
---|
141 | $this->y = intval(round($this->y*$yScale)); |
---|
142 | $this->rx = intval(round($this->rx*$xScale)); |
---|
143 | $this->ry = intval(round($this->ry*$yScale)); |
---|
144 | |
---|
145 | // need to rasterize again |
---|
146 | $this->lines = null; |
---|
147 | } |
---|
148 | |
---|
149 | public function getType(){ |
---|
150 | return ShapeTypes::T_ELLIPSE; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * @return array |
---|
155 | */ |
---|
156 | public function getRawShapeData(){ |
---|
157 | return [ |
---|
158 | $this->x, |
---|
159 | $this->y, |
---|
160 | $this->rx, |
---|
161 | $this->ry |
---|
162 | ]; |
---|
163 | } |
---|
164 | |
---|
165 | public function getSvgShapeData(){ |
---|
166 | return "<ellipse cx=\"" . $this->x . "\" cy=\"" . $this->y . "\" rx=\"" . $this->rx . "\" ry=\"" . $this->ry . "\" " . SvgExporter::$SVG_STYLE_HOOK . " />"; |
---|
167 | } |
---|
168 | |
---|
169 | } |
---|