| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2020 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 calculating a best-fit line |
| 26 |
*/ |
| 27 |
class BestFitLine { |
| 28 |
|
| 29 |
protected $graph; |
| 30 |
protected $points; |
| 31 |
protected $line; |
| 32 |
protected $projection; |
| 33 |
|
| 34 |
public function __construct(&$graph, $points) |
| 35 |
{ |
| 36 |
$this->graph =& $graph; |
| 37 |
$this->line = new PathData; |
| 38 |
$this->projection = new PathData; |
| 39 |
$this->points = $points; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Calculates the line and projection |
| 44 |
*/ |
| 45 |
public function calculate(BoundingBox $area, $limit_start, $limit_end, |
| 46 |
$project_start, $project_end) |
| 47 |
{ |
| 48 |
// can't draw a line through fewer than 2 points |
| 49 |
if(count($this->points) < 2) |
| 50 |
return false; |
| 51 |
|
| 52 |
$sum_x = $sum_y = $sum_x2 = $sum_xy = 0; |
| 53 |
foreach($this->points as $p) { |
| 54 |
$sum_x += $p->x; |
| 55 |
$sum_y += $p->y; |
| 56 |
$sum_x2 += pow($p->x, 2); |
| 57 |
$sum_xy += $p->x * $p->y; |
| 58 |
} |
| 59 |
|
| 60 |
$mean_x = $sum_x / count($this->points); |
| 61 |
$mean_y = $sum_y / count($this->points); |
| 62 |
|
| 63 |
if($sum_x2 == $sum_x * $mean_x) { |
| 64 |
// vertical line |
| 65 |
$slope = null; |
| 66 |
$y_int = $mean_x; |
| 67 |
} else { |
| 68 |
$slope = ($sum_xy - $sum_x * $mean_y) / ($sum_x2 - $sum_x * $mean_x); |
| 69 |
$y_int = $mean_y - $slope * $mean_x; |
| 70 |
} |
| 71 |
$this->buildPaths($slope, $y_int, $area, $limit_start, $limit_end, |
| 72 |
$project_start, $project_end); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Builds the line and projection paths. |
| 77 |
* For vertical lines, $slope = null and $y_int = $x |
| 78 |
*/ |
| 79 |
protected function buildPaths($slope, $y_int, $area, $limit_start, $limit_end, |
| 80 |
$project_start, $project_end) |
| 81 |
{ |
| 82 |
// initialize min and max points of line |
| 83 |
$x_min = $limit_start === null ? 0 : max($limit_start, 0); |
| 84 |
$x_max = $limit_end === null ? $area->width() : min($limit_end, $area->width()); |
| 85 |
$y_min = 0; |
| 86 |
$y_max = $area->height(); |
| 87 |
$line = new PathData; |
| 88 |
$projection = new PathData; |
| 89 |
|
| 90 |
if($slope === null) { |
| 91 |
// line is vertical! |
| 92 |
$coords = [ |
| 93 |
'x1' => $y_int, 'x2' => $y_int, |
| 94 |
'y1' => $y_min, 'y2' => $y_max |
| 95 |
]; |
| 96 |
} else { |
| 97 |
$coords = $this->boxLine($x_min, $x_max, $y_min, $y_max, $slope, $y_int); |
| 98 |
|
| 99 |
if($project_end) { |
| 100 |
$pcoords = $this->boxLine($coords['x2'], $area->width(), $y_min, $y_max, |
| 101 |
$slope, $y_int); |
| 102 |
if($pcoords !== null) { |
| 103 |
$x1 = $pcoords['x1'] + $area->x1; |
| 104 |
$x2 = $pcoords['x2'] + $area->x1; |
| 105 |
$y1 = $area->y2 - $pcoords['y1']; |
| 106 |
$y2 = $area->y2 - $pcoords['y2']; |
| 107 |
$projection->add('M', $x1, $y1, 'L', $x2, $y2); |
| 108 |
} |
| 109 |
} |
| 110 |
if($project_start) { |
| 111 |
$pcoords = $this->boxLine(0, $coords['x1'], $y_min, $y_max, |
| 112 |
$slope, $y_int); |
| 113 |
if($pcoords !== null) { |
| 114 |
$x1 = $pcoords['x1'] + $area->x1; |
| 115 |
$x2 = $pcoords['x2'] + $area->x1; |
| 116 |
$y1 = $area->y2 - $pcoords['y1']; |
| 117 |
$y2 = $area->y2 - $pcoords['y2']; |
| 118 |
$projection->add('M', $x1, $y1, 'L', $x2, $y2); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
$x1 = $coords['x1'] + $area->x1; |
| 123 |
$x2 = $coords['x2'] + $area->x1; |
| 124 |
$y1 = $area->y2 - $coords['y1']; |
| 125 |
$y2 = $area->y2 - $coords['y2']; |
| 126 |
$line->add('M', $x1, $y1, 'L', $x2, $y2); |
| 127 |
|
| 128 |
$this->projection = $projection; |
| 129 |
$this->line = $line; |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns the best-fit line as PathData |
| 135 |
*/ |
| 136 |
public function getLine() |
| 137 |
{ |
| 138 |
return $this->line; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns the projection line(s) as PathData |
| 143 |
*/ |
| 144 |
public function getProjection() |
| 145 |
{ |
| 146 |
return $this->projection; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Returns the coordinates of a line that passes through a box |
| 151 |
*/ |
| 152 |
public static function boxLine($x_min, $x_max, $y_min, $y_max, $slope, $y_int) |
| 153 |
{ |
| 154 |
$x1 = $x_min; |
| 155 |
$y1 = $slope * $x1 + $y_int; |
| 156 |
$x2 = $x_max; |
| 157 |
$y2 = $slope * $x2 + $y_int; |
| 158 |
|
| 159 |
if($slope != 0) { |
| 160 |
if($y1 < 0) { |
| 161 |
$x1 = -$y_int / $slope; |
| 162 |
$y1 = $y_min; |
| 163 |
} elseif($y1 > $y_max) { |
| 164 |
$x1 = ($y_max - $y_int) / $slope; |
| 165 |
$y1 = $y_max; |
| 166 |
} |
| 167 |
|
| 168 |
if($y2 < 0) { |
| 169 |
$x2 = - $y_int / $slope; |
| 170 |
$y2 = $y_min; |
| 171 |
} elseif($y2 > $y_max) { |
| 172 |
$x2 = ($y_max - $y_int) / $slope; |
| 173 |
$y2 = $y_max; |
| 174 |
} |
| 175 |
} |
| 176 |
if($x1 == $x2 && $y1 == $y2) |
| 177 |
return null; |
| 178 |
return compact('x1','y1','x2','y2'); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|