forked from tatai/burndowngenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New class for controlling Axis Ticks
- Loading branch information
Showing
6 changed files
with
394 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/* | ||
* Yet another burndown online generator, http://www.burndowngenerator.com | ||
* Copyright (C) 2010 Francisco José Naranjo <fran.naranjo@gmail.com> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
class DrawAxisTicks { | ||
/** | ||
* | ||
* @var DrawLine | ||
*/ | ||
private $_draw_line = null; | ||
|
||
/** | ||
* | ||
* @param DrawLine $drawLine | ||
*/ | ||
public function __construct(DrawLine $drawLine) { | ||
$this->_draw_line = $drawLine; | ||
} | ||
|
||
public function draw($distanceBetweenTicks, $tickSize, Line $line) { | ||
$operations = new LineOperations(); | ||
|
||
$distance = $line->size() / $distanceBetweenTicks; | ||
$nTicks = (int)floor($distance); | ||
|
||
$xSplit = ($line->to()->x() - $line->from()->x()) / $distance; | ||
$ySplit = ($line->to()->y() - $line->from()->y()) / $distance; | ||
|
||
for($i = 0; $i < $nTicks + 1; $i++) { | ||
$at = new Point($line->from()->x() + $xSplit * $i, $line->from()->y() + $ySplit * $i); | ||
$perpendicular = $operations->perpendicular($line, $at, $tickSize); | ||
|
||
$this->_draw_line->draw($perpendicular); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/* | ||
* Yet another burndown online generator, http://www.burndowngenerator.com | ||
* Copyright (C) 2010 Francisco José Naranjo <fran.naranjo@gmail.com> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
class LineOperations { | ||
|
||
/** | ||
* | ||
* @param Line $line | ||
* @param Point $at | ||
* @param float $length | ||
* @return Line | ||
*/ | ||
public function perpendicular(Line $line, Point $at, $length) { | ||
if($line->from()->x() == $line->to()->x()) { | ||
return $this->_perpendicularFromVerticalLine($line, $at, $length); | ||
} | ||
else if($line->from()->y() == $line->to()->y()) { | ||
return $this->_perpendicularFromHorizontalLine($line, $at, $length); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param Line $line | ||
* @param Point $at | ||
* @param float $length | ||
* @return Line | ||
*/ | ||
private function _perpendicularFromHorizontalLine(Line $line, Point $at, $length) { | ||
$medTickSize = $length / 2; | ||
|
||
$medTickSize *= $this->_chooseSign($line, 'x'); | ||
|
||
$from = new Point($at->x(), $at->y() - $medTickSize); | ||
$to = new Point($at->x(), $at->y() + $medTickSize); | ||
|
||
return new Line($from, $to); | ||
} | ||
|
||
/** | ||
* | ||
* @param Line $line | ||
* @param Point $at | ||
* @param float $length | ||
* @return Line | ||
*/ | ||
private function _perpendicularFromVerticalLine(Line $line, Point $at, $length) { | ||
$medTickSize = $length / 2; | ||
|
||
$medTickSize *= $this->_chooseSign($line, 'y'); | ||
|
||
$from = new Point($at->x() - $medTickSize, $at->y()); | ||
$to = new Point($at->x() + $medTickSize, $at->y()); | ||
|
||
return new Line($from, $to); | ||
} | ||
|
||
private function _chooseSign(Line $line, $dimension) { | ||
if($line->to()->$dimension() - $line->from()->$dimension() < 0) { | ||
return -1; | ||
} | ||
|
||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
/* | ||
* Yet another burndown online generator, http://www.burndowngenerator.com | ||
* Copyright (C) 2010 Francisco José Naranjo <fran.naranjo@gmail.com> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
require_once (dirname(__FILE__) . '/../test_startup.php'); | ||
|
||
class DrawAxisTicksTest extends PHPUnit_Framework_TestCase { | ||
/** | ||
* | ||
* @var DrawLine | ||
*/ | ||
private $_draw_line = null; | ||
|
||
/** | ||
* | ||
* @var int | ||
*/ | ||
private $_tick_size = null; | ||
|
||
/** | ||
* | ||
* @var DrawAxisTicks | ||
*/ | ||
private $_draw_axis_ticks = null; | ||
|
||
/** | ||
* | ||
* @var Line | ||
*/ | ||
private $_line = null; | ||
|
||
/** | ||
* | ||
* @var float | ||
*/ | ||
private $_step = null; | ||
|
||
public function setUp() { | ||
$pdf = $this->getMock('MetricsPdf', array(), array( | ||
'a4', | ||
'landscape')); | ||
$styleChanger = new LineStyleChanger(); | ||
|
||
$this->_draw_line = $this->getMock('DrawLine', array(), array( | ||
$pdf, | ||
$styleChanger)); | ||
|
||
$this->_tick_size = 3; | ||
$this->_step = 2; | ||
|
||
$this->_line = new Line(new Point(2, 4), new Point(8.5, 4)); | ||
|
||
$this->_draw_axis_ticks = new DrawAxisTicks($this->_draw_line); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function createsTicksWithinLine() { | ||
$distanceBetweenTicks = 3; | ||
$lineSize = $this->_line->to()->x() - $this->_line->from()->x(); | ||
|
||
$ticks = (int)floor($lineSize / $distanceBetweenTicks); | ||
|
||
$this->_draw_line->expects($this->exactly($ticks))->method('draw'); | ||
|
||
$this->_draw_axis_ticks->draw($distanceBetweenTicks, $this->_tick_size, $this->_line); | ||
} | ||
|
||
/** | ||
* @TODO: impossible to make this work with PHPUnit 3.4.1 | ||
*/ | ||
public function twoTicksMeansOneTickAtStartPointAndOneAtTheEnd() { | ||
$ticks = 2; | ||
$startTick = new Line( | ||
new Point($this->_start_point->x(), $this->_start_point->y() - $this->_tick_size / 2), | ||
new Point($this->_start_point->x(), $this->_start_point->y() + $this->_tick_size / 2) | ||
); | ||
|
||
$endTick = new Line( | ||
new Point($this->_end_point->x(), $this->_end_point->y() - $this->_tick_size / 2), | ||
new Point($this->_end_point->x(), $this->_end_point->y() + $this->_tick_size / 2) | ||
); | ||
|
||
$this->_draw_line->expects($this->once())->method('draw')->with($startTick); | ||
$this->_draw_line->expects($this->once())->method('draw')->with($endTick); | ||
|
||
$this->_draw_axis_ticks->draw($ticks, $this->_tick_size, $this->_line); | ||
} | ||
} | ||
|
||
require_once (dirname(__FILE__) . '/../test_shutdown.php'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/* | ||
* Yet another burndown online generator, http://www.burndowngenerator.com | ||
* Copyright (C) 2010 Francisco José Naranjo <fran.naranjo@gmail.com> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
require_once (dirname(__FILE__) . '/../test_startup.php'); | ||
|
||
class LinePerpendicularFromHorizontalTest extends PHPUnit_Framework_TestCase { | ||
private $_from = null; | ||
private $_to = null; | ||
private $_at = null; | ||
|
||
/** | ||
* | ||
* @var LineOperations | ||
*/ | ||
private $_operations = null; | ||
|
||
public function setUp() { | ||
$this->_from = new Point(1, 2); | ||
$this->_to = new Point(4, 2); | ||
$this->_at = new Point(3, 2); | ||
|
||
$this->_operations = new LineOperations(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function createPerpendicularLineInPointFromHorizontal0Degrees() { | ||
$length = 2; | ||
|
||
$perpendicularFrom = new Point(3, 1); | ||
$perpendicularTo = new Point(3, 3); | ||
|
||
$line = new Line($this->_from, $this->_to); | ||
|
||
$perpendicular = $this->_operations->perpendicular($line, $this->_at, $length); | ||
$this->assertEquals($perpendicularFrom, $perpendicular->from()); | ||
$this->assertEquals($perpendicularTo, $perpendicular->to()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function createPerpendicularLineInPointFromHorizontal180Degrees() { | ||
$length = 2; | ||
|
||
$perpendicularFrom = new Point(3, 3); | ||
$perpendicularTo = new Point(3, 1); | ||
|
||
$line = new Line($this->_to, $this->_from); | ||
|
||
$perpendicular = $this->_operations->perpendicular($line, $this->_at, $length); | ||
$this->assertEquals($perpendicularFrom, $perpendicular->from()); | ||
$this->assertEquals($perpendicularTo, $perpendicular->to()); | ||
} | ||
} | ||
|
||
require_once (dirname(__FILE__) . '/../test_shutdown.php'); |
Oops, something went wrong.