| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2019 Graham Breach |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
/** |
| 19 |
* For more information, please contact <graham@goat1000.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Goat1000\SVGGraph; |
| 23 |
|
| 24 |
/** |
| 25 |
* Class for details of each pie slice |
| 26 |
*/ |
| 27 |
class SliceInfo { |
| 28 |
public $start_angle; |
| 29 |
public $end_angle; |
| 30 |
public $radius_x; |
| 31 |
public $radius_y; |
| 32 |
|
| 33 |
public function __construct($start, $end, $rx, $ry) |
| 34 |
{ |
| 35 |
$this->start_angle = $start; |
| 36 |
$this->end_angle = $end; |
| 37 |
$this->radius_x = $rx; |
| 38 |
$this->radius_y = $ry; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Calculates the middle angle of the slice |
| 43 |
*/ |
| 44 |
public function midAngle() |
| 45 |
{ |
| 46 |
return $this->start_angle + ($this->end_angle - $this->start_angle) / 2; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns the slice angle in degrees |
| 51 |
*/ |
| 52 |
public function degrees() |
| 53 |
{ |
| 54 |
return rad2deg($this->end_angle - $this->start_angle); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the bounding box for the slice, radius from 0,0 |
| 59 |
* @return array($x1, $y1, $x2, $y2) |
| 60 |
*/ |
| 61 |
public function boundingBox($reverse) |
| 62 |
{ |
| 63 |
$x1 = $y1 = $x2 = $y2 = 0; |
| 64 |
$angle = fmod($this->end_angle - $this->start_angle, 2 * M_PI); |
| 65 |
$right_angle = M_PI * 0.5; |
| 66 |
|
| 67 |
$rx = $this->radius_x; |
| 68 |
$ry = $this->radius_y; |
| 69 |
$a1 = fmod($this->start_angle, 2 * M_PI); |
| 70 |
$a2 = $a1 + $angle; |
| 71 |
$start_sector = floor($a1 / $right_angle); |
| 72 |
$end_sector = floor($a2 / $right_angle); |
| 73 |
|
| 74 |
switch($end_sector - $start_sector) { |
| 75 |
|
| 76 |
case 0: |
| 77 |
// slice all in one sector |
| 78 |
$x = max(abs(cos($a1)), abs(cos($a2))) * $rx; |
| 79 |
$y = max(abs(sin($a1)), abs(sin($a2))) * $ry; |
| 80 |
switch($start_sector) { |
| 81 |
case 0: |
| 82 |
$x2 = $x; |
| 83 |
$y2 = $y; |
| 84 |
break; |
| 85 |
case 1: |
| 86 |
$x1 = -$x; |
| 87 |
$y2 = $y; |
| 88 |
break; |
| 89 |
case 2: |
| 90 |
$x1 = -$x; |
| 91 |
$y1 = -$y; |
| 92 |
break; |
| 93 |
case 3: |
| 94 |
$x2 = $x; |
| 95 |
$y1 = -$y; |
| 96 |
break; |
| 97 |
} |
| 98 |
break; |
| 99 |
|
| 100 |
case 1: |
| 101 |
// slice across two sectors |
| 102 |
switch($start_sector) { |
| 103 |
case 0: |
| 104 |
$x1 = cos($a2) * $rx; |
| 105 |
$x2 = cos($a1) * $rx; |
| 106 |
$y2 = $ry; |
| 107 |
break; |
| 108 |
case 1: |
| 109 |
$x1 = -$rx; |
| 110 |
$y1 = sin($a2) * $ry; |
| 111 |
$y2 = sin($a1) * $ry; |
| 112 |
break; |
| 113 |
case 2: |
| 114 |
$x1 = cos($a1) * $rx; |
| 115 |
$x2 = cos($a2) * $rx; |
| 116 |
$y1 = -$ry; |
| 117 |
break; |
| 118 |
case 3: |
| 119 |
$x2 = $rx; |
| 120 |
$y1 = sin($a1) * $ry; |
| 121 |
$y2 = sin($a2) * $ry; |
| 122 |
break; |
| 123 |
} |
| 124 |
break; |
| 125 |
|
| 126 |
case 2: |
| 127 |
// slice across three sectors |
| 128 |
$x1 = -$rx; |
| 129 |
$y1 = -$ry; |
| 130 |
$x2 = $rx; |
| 131 |
$y2 = $ry; |
| 132 |
switch($start_sector) { |
| 133 |
case 0: |
| 134 |
$y1 = sin($a2) * $ry; |
| 135 |
$x2 = cos($a1) * $rx; |
| 136 |
break; |
| 137 |
case 1: |
| 138 |
$x2 = cos($a2) * $rx; |
| 139 |
$y2 = sin($a1) * $ry; |
| 140 |
break; |
| 141 |
case 2: |
| 142 |
$x1 = cos($a1) * $rx; |
| 143 |
$y2 = sin($a2) * $ry; |
| 144 |
break; |
| 145 |
case 3: |
| 146 |
$x1 = cos($a2) * $rx; |
| 147 |
$y1 = sin($a1) * $ry; |
| 148 |
break; |
| 149 |
} |
| 150 |
break; |
| 151 |
|
| 152 |
case 3: |
| 153 |
// slice across four sectors |
| 154 |
$x = max(abs(cos($a1)), abs(cos($a2))) * $rx; |
| 155 |
$y = max(abs(sin($a1)), abs(sin($a2))) * $ry; |
| 156 |
$x1 = -$rx; |
| 157 |
$y1 = -$ry; |
| 158 |
$x2 = $rx; |
| 159 |
$y2 = $ry; |
| 160 |
switch($start_sector) { |
| 161 |
case 0: $x2 = $x; break; |
| 162 |
case 1: $y2 = $y; break; |
| 163 |
case 2: $x1 = -$x; break; |
| 164 |
case 3: $y1 = -$y; break; |
| 165 |
} |
| 166 |
break; |
| 167 |
|
| 168 |
case 4: |
| 169 |
// slice is > 270 degrees and both ends in one sector |
| 170 |
$x1 = -$rx; |
| 171 |
$y1 = -$ry; |
| 172 |
$x2 = $rx; |
| 173 |
$y2 = $ry; |
| 174 |
break; |
| 175 |
} |
| 176 |
|
| 177 |
if($reverse) { |
| 178 |
// swap Y around origin |
| 179 |
$y = -$y1; |
| 180 |
$y1 = -$y2; |
| 181 |
$y2 = $y; |
| 182 |
} |
| 183 |
return [$x1, $y1, $x2, $y2]; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|