| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2022 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 |
class Bar3DCylinder extends Bar3D { |
| 25 |
|
| 26 |
protected $ellipse; |
| 27 |
protected $arc_path; |
| 28 |
protected $cyl_offset_x; |
| 29 |
protected $cyl_offset_y; |
| 30 |
protected $shade_gradient_id; |
| 31 |
|
| 32 |
public function __construct(&$graph) |
| 33 |
{ |
| 34 |
parent::__construct($graph); |
| 35 |
$gradient = $graph->getOption('depth_shade_gradient'); |
| 36 |
if(is_array($gradient)) |
| 37 |
$this->shade_gradient_id = $graph->defs->addGradient($gradient); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Calculates the a and b radii of the ellipse filling the parallelogram |
| 42 |
*/ |
| 43 |
protected function findEllipse() |
| 44 |
{ |
| 45 |
$alpha = deg2rad($this->angle / 2); |
| 46 |
$x = $this->depth * cos($alpha) / 2; |
| 47 |
$y = $this->depth * sin($alpha) / 2; |
| 48 |
$dydx = -$y / $x; |
| 49 |
|
| 50 |
$bsq = pow($y, 2) - $x * $y * $dydx; |
| 51 |
$asq = pow($x, 2) / (1 - $y / ($y - $x * $dydx)); |
| 52 |
|
| 53 |
$a = sqrt($asq); |
| 54 |
$b = sqrt($bsq); |
| 55 |
|
| 56 |
// now find the vertical |
| 57 |
$alpha2 = deg2rad(- $this->angle / 2 - 90); |
| 58 |
$dydx2 = tan($alpha2); |
| 59 |
$ysq = $bsq / (pow($dydx2, 2) * ($asq / $bsq) + 1); |
| 60 |
$xsq = $asq - $asq * $ysq / $bsq; |
| 61 |
|
| 62 |
$x1 = sqrt($xsq); |
| 63 |
$y1 = -sqrt($ysq); |
| 64 |
$this->ellipse = compact('a', 'b', 'x1', 'y1'); |
| 65 |
|
| 66 |
// create the arc path |
| 67 |
$r = -$this->angle / 2; |
| 68 |
$rr = deg2rad($r); |
| 69 |
$x1a = -($x1 * cos($rr) + $y1 * sin($rr)); |
| 70 |
$y1a = -($x1 * sin($rr) - $y1 * cos($rr)); |
| 71 |
$x2 = -2 * $x1a; |
| 72 |
$y2 = -2 * $y1a; |
| 73 |
$this->cyl_offset_x = $x1a; |
| 74 |
$this->cyl_offset_y = $y1a; |
| 75 |
$this->arc_path = new PathData('a', $a, $b, $r, 1, 0, $x2, $y2); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Sets the depth of the bar |
| 80 |
*/ |
| 81 |
public function setDepth($d) |
| 82 |
{ |
| 83 |
parent::setDepth($d); |
| 84 |
$this->findEllipse(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Draws bar front |
| 89 |
*/ |
| 90 |
protected function front($x, $y, $w, $h) |
| 91 |
{ |
| 92 |
$x += $this->cyl_offset_x; |
| 93 |
$y += $this->cyl_offset_y; |
| 94 |
$path = new PathData('M', $x, $y, 'v', $h); |
| 95 |
$path->add($this->arc_path); |
| 96 |
$path->add('v', -$h); |
| 97 |
$f = ['d' => $path, 'stroke' => 'none']; |
| 98 |
$front = $this->graph->element('path', $f); |
| 99 |
|
| 100 |
if($this->shade_gradient_id) { |
| 101 |
$f['fill'] = 'url(#' . $this->shade_gradient_id . ')'; |
| 102 |
$front .= $this->graph->element('path', $f); |
| 103 |
} |
| 104 |
|
| 105 |
$f = ['d' => $path, 'fill' => 'none', 'stroke-linejoin' => 'bevel']; |
| 106 |
$front .= $this->graph->element('path', $f); |
| 107 |
return $front; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Draws bar top |
| 112 |
*/ |
| 113 |
protected function top($x, $y, $w, $h) |
| 114 |
{ |
| 115 |
$r = -$this->angle / 2; |
| 116 |
$xform = new Transform; |
| 117 |
$xform->translate($x, $y); |
| 118 |
$xform->rotate($r); |
| 119 |
$t = [ |
| 120 |
'cx' => 0, 'cy' => 0, |
| 121 |
'rx' => $this->ellipse['a'], 'ry' => $this->ellipse['b'], |
| 122 |
'transform' => $xform, |
| 123 |
'fill' => $this->solid_colour, |
| 124 |
]; |
| 125 |
if($this->overlay_top) |
| 126 |
$t['stroke'] = 'none'; |
| 127 |
$top = $this->graph->element('ellipse', $t); |
| 128 |
|
| 129 |
if($this->overlay_top) { |
| 130 |
$t['fill-opacity'] = $this->overlay_top; |
| 131 |
$t['fill'] = $this->overlay_top_colour; |
| 132 |
unset($t['stroke']); |
| 133 |
$top .= $this->graph->element('ellipse', $t); |
| 134 |
} |
| 135 |
return $top; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Draws bar side |
| 140 |
*/ |
| 141 |
protected function side($x, $y, $w, $h) |
| 142 |
{ |
| 143 |
return ''; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Draws bar edge path |
| 148 |
*/ |
| 149 |
protected function edge($x, $y, $w, $h) |
| 150 |
{ |
| 151 |
// cylinder edge is drawn with front |
| 152 |
return ''; |
| 153 |
} |
| 154 |
} |
| 155 |
|