Exemple #1
0
 /**
  * @param Point $point
  * @return bool
  */
 public function contains(Point $point)
 {
     $vertices = $this->points->getPoints();
     // Check if the point is inside the polygon or on the boundary
     $intersections = 0;
     for ($i = 1; $i < $this->points->getPoints()->count(); $i++) {
         $vertex1 = $vertices->offsetGet($i - 1);
         $vertex2 = $vertices->offsetGet($i);
         // Check if point is on an horizontal polygon boundary
         if ($vertex1->getY() == $vertex2->getY() && $vertex1->getY() == $point->getY() && $point->getX() > min($vertex1->getX(), $vertex2->getX()) && $point->getX() < max($vertex1->getX(), $vertex2->getX())) {
             return true;
         }
         if ($point->getY() > min($vertex1->getY(), $vertex2->getY()) && $point->getY() <= max($vertex1->getY(), $vertex2->getY()) && $point->getX() <= max($vertex1->getX(), $vertex2->getX()) and $vertex1->getY() != $vertex2->getY()) {
             $xinters = ($point->getY() - $vertex1->getY()) * ($vertex2->getX() - $vertex1->getX()) / ($vertex2->getY() - $vertex1->getY()) + $vertex1->getX();
             // Check if point is on the polygon boundary (other than horizontal)
             if ($xinters == $point->getX()) {
                 return true;
             }
             if ($vertex1->getX() == $vertex2->getX() || $point->getX() <= $xinters) {
                 $intersections++;
             }
         }
     }
     // If the number of edges we passed through is odd, then it's in the polygon.
     if ($intersections % 2 != 0) {
         return true;
     } else {
         return false;
     }
 }
Exemple #2
0
 /**
  * Return true if the given point is on the line
  *
  * @param Point $point
  * @return bool
  */
 public function hasPoint(Point $point)
 {
     $nv = $this->getNormalVector();
     if ($nv->getX() * $point->getX() + $nv->getY() * $point->getY() != $nv->getX() * $this->a->getX() + $nv->getY() * $this->a->getY()) {
         return false;
     }
     return $point->getX() >= $this->a->getX() && $point->getX() <= $this->b->getX() && $point->getY() >= $this->a->getY() && $point->getY() <= $this->b->getY();
 }
 public function __construct(Point $center, $edge_length)
 {
     $this->edge = $edge_length;
     $this->minX = $center->getX() - $this->edge / 2;
     $this->maxX = $center->getX() + $this->edge / 2;
     $this->minY = $center->getY() - $this->edge / 2;
     $this->maxY = $center->getY() + $this->edge / 2;
     $this->minZ = $center->getZ() - $this->edge / 2;
     $this->maxZ = $center->getZ() + $this->edge / 2;
 }
Exemple #4
0
 /**
  * @covers Imagine\Image\Point::getX
  * @covers Imagine\Image\Point::getY
  * @covers Imagine\Image\Point::in
  *
  * @dataProvider getCoordinates
  *
  * @param integer       $x
  * @param integer       $y
  * @param BoxInterface $box
  * @param Boolean       $expected
  */
 public function testShouldAssignXYCoordinates($x, $y, BoxInterface $box, $expected)
 {
     $coordinate = new Point($x, $y);
     $this->assertEquals($x, $coordinate->getX());
     $this->assertEquals($y, $coordinate->getY());
     $this->assertEquals($expected, $coordinate->in($box));
 }
Exemple #5
0
 /**
  * test using valid x, valid y
  **/
 public function testValidPoint()
 {
     $point = new Point($this->VALID_X, $this->VALID_Y);
     //use the mutators to make a valid test case
     $point->setX($this->VALID_X);
     $point->setY($this->VALID_Y);
     // assertSame() that getFoo() is the same as $VALID_FOO
     $this->assertSame($point->getX(), $this->VALID_X);
     $this->assertSame($point->getY(), $this->VALID_Y);
 }
Exemple #6
0
 /**
  * test grabbing a segment by SegmentStopElevation
  **/
 public function testGetValidSegmentByStopElevation()
 {
     //count the number of rows and save it for later
     $numRows = $this->getConnection()->getRowCount("segment");
     //create a new segment and insert it into the database
     $segment = new Segment(null, $this->VALID_SEGMENTSTART, $this->VALID_SEGMENTSTOP, $this->VALID_SEGMENTSTARTELEVATION, $this->VALID_SEGMENTSTOPELEVATION);
     $segment->insert($this->getPDO());
     //grab the data from mySQL and verify the fields
     $pdoSegments = Segment::getSegmentBySegmentStopElevation($this->getPDO(), $segment->getSegmentStopElevation());
     $this->assertSame($numRows + 1, $this->getConnection()->getRowCount("segment"));
     foreach ($pdoSegments as $pdoSegment) {
         $this->assertSame($pdoSegment->getSegmentStart()->getX(), $this->VALID_SEGMENTSTART->getX());
         $this->assertSame($pdoSegment->getSegmentStart()->getY(), $this->VALID_SEGMENTSTART->getY());
         $this->assertSame($pdoSegment->getSegmentStop()->getX(), $this->VALID_SEGMENTSTOP->getX());
         $this->assertSame($pdoSegment->getSegmentStop()->getY(), $this->VALID_SEGMENTSTOP->getY());
         $this->assertSame($pdoSegment->getSegmentStartElevation(), $this->VALID_SEGMENTSTARTELEVATION);
         $this->assertSame($pdoSegment->getSegmentStopElevation(), $this->VALID_SEGMENTSTOPELEVATION);
     }
 }
 public function distance(Point $p1, Point $p2)
 {
     $p1x = $p1->getX();
     $p2x = $p2->getX();
     $p1y = $p1->getY();
     $p2y = $p2->getY();
     $v1 = ($p1x - $p2x) * -1;
     $v2 = ($p1y - $p2y) * -1;
     $steps = $v1 + $v2;
     $distance = sqrt($v1 * $v1 + $v2 * $v2);
     return $distance;
 }
Exemple #8
0
 /**
  * gets segment by segmentStop
  *
  * @param PDO $pdo pointer to PDO connection
  * @param Point $segmentStop stop point to trail-search for
  * @return mixed segment found or null if not found
  * @throws PDOException when mySQL related errors occur
  * @throws RangeException when range is invalid
  * @throws Exception for other exception
  */
 public static function getSegmentBySegmentStop(PDO &$pdo, Point $segmentStop)
 {
     //create query template
     $query = "SELECT segmentId, ST_AsWKT(segmentStart) AS segmentStart, ST_AsWKT(segmentStop) AS segmentStop, segmentStartElevation, segmentStopElevation FROM segment WHERE segmentStop = POINT(:segmentStopX, :segmentStopY)";
     $statement = $pdo->prepare($query);
     //binds segmentStop to placeholder
     $parameters = array("segmentStopX" => $segmentStop->getX(), "segmentStopY" => $segmentStop->getY());
     $statement->execute($parameters);
     //build an array of segments
     $segments = new SplFixedArray($statement->rowCount());
     $statement->setFetchMode(PDO::FETCH_ASSOC);
     while (($row = $statement->fetch()) !== false) {
         try {
             $segmentStartJSON = Gisconverter::wktToGeojson($row["segmentStart"]);
             $segmentStopJSON = Gisconverter::wktToGeojson($row["segmentStop"]);
             $segmentStartGenericObject = json_decode($segmentStartJSON);
             $segmentStopGenericObject = json_decode($segmentStopJSON);
             $segmentStart = new Point($segmentStartGenericObject->coordinates[0], $segmentStartGenericObject->coordinates[1]);
             $segmentStop = new Point($segmentStopGenericObject->coordinates[0], $segmentStopGenericObject->coordinates[1]);
             $segment = new Segment($row["segmentId"], $segmentStart, $segmentStop, $row["segmentStartElevation"], $row["segmentStopElevation"]);
             $segments[$segments->key()] = $segment;
             $segments->next();
         } catch (Exception $e) {
             //if the row couldn't be converter, rethrow it
             throw new PDOException($e->getMessage(), 0, $e);
         }
     }
     return $segments;
 }
Exemple #9
0
 /**
  * @param Point $point
  *
  * @return bool
  */
 public function equals(Point $point)
 {
     return $point->getX() == $this->x && $point->getY() == $this->y;
 }
 private function drawGradient(Point $point1, Point $point2, Color $color, $decay)
 {
     /* Positive gradient */
     if ($decay > 0) {
         $YStep = ($point2->getY() - $point1->getY() - 2) / $decay;
         for ($i = 0; $i <= $decay; $i++) {
             $color = $color->addRGBIncrement(-1);
             $Yi1 = $point1->getY() + $i * $YStep;
             $Yi2 = ceil($Yi1 + $i * $YStep + $YStep);
             if ($Yi2 >= $Yi2) {
                 $Yi2 = $point2->getY() - 1;
             }
             $this->canvas->drawFilledRectangle(new Point($point1->getX(), $Yi1), new Point($point2->getX(), $Yi2), $color, ShadowProperties::NoShadow());
         }
     }
     /* Negative gradient */
     if ($decay < 0) {
         $YStep = ($point2->getY() - $point1->getY() - 2) / -$decay;
         $Yi1 = $point1->getY();
         $Yi2 = $point1->getY() + $YStep;
         for ($i = -$decay; $i >= 0; $i--) {
             $color = $color->addRGBIncrement(1);
             $this->canvas->drawFilledRectangle(new Point($point1->getX(), $Yi1), new Point($point2->getX(), $Yi2), $color, ShadowProperties::NoShadow());
             $Yi1 += $YStep;
             $Yi2 += $YStep;
             if ($Yi2 >= $Yi2) {
                 $Yi2 = $point2->getY() - 1;
             }
         }
     }
 }
 public function drawFilledCircle(Point $center, $height, Color $color, ShadowProperties $shadowProperties, $width = null)
 {
     if ($width == null) {
         $width = $height;
     }
     $C_Circle = $this->allocateColor($color);
     $Step = 360 / (2 * M_PI * max($width, $height));
     for ($i = 90; $i <= 270; $i = $i + $Step) {
         $X1 = cos($i * M_PI / 180) * $height + $center->getX();
         $Y1 = sin($i * M_PI / 180) * $width + $center->getY();
         $X2 = cos((180 - $i) * M_PI / 180) * $height + $center->getX();
         $Y2 = sin((180 - $i) * M_PI / 180) * $width + $center->getY();
         $this->drawAntialiasPixel(new Point($X1 - 1, $Y1 - 1), $color, $shadowProperties);
         $this->drawAntialiasPixel(new Point($X2 - 1, $Y2 - 1), $color, $shadowProperties);
         if ($Y1 - 1 > $center->getY() - max($width, $height)) {
             imageline($this->picture, $X1, $Y1 - 1, $X2 - 1, $Y2 - 1, $C_Circle);
         }
     }
 }
Exemple #12
0
 /**
  *
  * Вычитает из высоты и ширины размера координаты точки или
  * высоту и ширину другого размера и возвращает получившийся размер.
  *
  * @param Size $s
  * @param Size|Point $obj
  * @return Size
  * @throws IllegalArgumentException
  */
 public static function subtract(Size $s, $obj)
 {
     if ($obj instanceof Size) {
         return new Size($s->getWidth() - $obj->getWidth(), $s->getHeight() - $obj->getHeight());
     } else {
         if ($obj instanceof Point) {
             return new Size($s->getWidth() - $obj->getX(), $s->getHeigth() - $obj->getY());
         }
     }
     throw new IllegalArgumentException();
 }
Exemple #13
0
 public function circle(Point $a, $r)
 {
     $xc = $a->getX();
     $yc = $a->getY();
     $x = 0;
     $y = $r;
     $d = $r - 1;
     while ($y >= $x) {
         $this->moveCursorTo($xc + $x, $yc + $y);
         echo "@";
         $this->moveCursorTo($xc + $y, $yc + $x);
         echo "@";
         $this->moveCursorTo($xc - $x, $yc + $y);
         echo "@";
         $this->moveCursorTo($xc - $y, $yc + $x);
         echo "@";
         $this->moveCursorTo($xc + $x, $yc - $y);
         echo "@";
         $this->moveCursorTo($xc + $y, $yc - $x);
         echo "@";
         $this->moveCursorTo($xc - $x, $yc - $y);
         echo "@";
         $this->moveCursorTo($xc - $y, $yc - $x);
         echo "@";
         if ($d >= 2 * $x) {
             $d -= 2 * $x + 1;
             $x++;
         } else {
             if ($d < 2 * ($r - $y)) {
                 $d += 2 * $y - 1;
                 $y--;
             } else {
                 $d += 2 * ($y - $x - 1);
                 $y--;
                 $x++;
             }
         }
     }
 }
$rectangleArea = $firstSide * $secondSide;
echo 'Rectangle area: ' . $rectangleArea;
?>
                </div>
            </section>

            <section class="panel panel-info top-buffer">
                <header class="panel-heading">
                    <h2>Problem 4. Triangle Area</h2>
                </header>
                <div class="panel-body">
                    <?php 
$pointA = new Point(-5, 10);
$pointB = new Point(25, 30);
$pointC = new Point(60, 15);
$area = abs(($pointA->getX() * ($pointB->getY() - $pointC->getY()) + $pointB->getX() * ($pointC->getY() - $pointA->getY()) + $pointC->getX() * ($pointA->getY() - $pointB->getY())) / 2);
echo 'The area is: ' . $area;
?>
                </div>
            </section>

            <section class="panel panel-info top-buffer">
                <header class="panel-heading">
                    <h2>Problem 5. Boolean Variable</h2>
                </header>
                <div class="panel-body">
                    <?php 
$isFemale = false;
echo "I'm female? - " . getBooleanDisplayValue($isFemale);
?>
                </div>
Exemple #15
0
 /**
  * Сравнивает две точки
  *
  * @param Point
  * @return boolean
  */
 public function equals(Point $p)
 {
     return $this->getX() == $p->getX() && $this->getY() == $p->getY();
 }
Exemple #16
0
 public function getY()
 {
     return $this->location->getY();
 }
<?php

/*ПРАКТИЧЕСКОЕ ПРИМЕНЕИЕ ИНКАПСУЛЯЦИИ*/
//Инкапсуляция - это свойство ООП позволяющее защитить и объединить данные и код в объект и скрыть реализацию объекта от пользователя.
//Модификатор доступа public - позволяет обращаться к свойствам и методам отовсюду.
//Модификатор доступа private - позволяет обращаться к свойствам и методам только внутри текущего класса.
//Модификатор доступа protected - позволяет обращаться к свойствам и методам только текущего класса и класса, который наследует свойства и методы текущего класса
//Лучше всего скрывать поля (свойства), а доступ давать к ним через методы
require_once getenv("DOCUMENT_ROOT") . "/lib/config.php";
require_once "Point.php";
$point = new Point();
//echo $point->length(5, 4);
//echo $point->x;
$point->setX(10);
$point->setY(-6);
echo $point->getX($x) . "<br/>";
echo $point->getY($y) . "<br/>";
echo $point->lengthObject(new Point(4, 9));
Exemple #18
0
 /**
  * Return true if the given point is in the same location
  *
  * @param Point $point
  * @return bool
  */
 public function equalsTo(Point $point)
 {
     return $this->x == $point->getX() && $this->getY() == $point->getY();
 }
<?php

/*КЛОНИРОВАНИЕ ОБЪЕКТОВ*/
//В старых версиях PHP и да же в 4 версии копирование объектов заключалось в том, что создавалась переменная и к ней присваивался существующий объект ($point_2 = $point_1), сейчас с версии PHP5 это не создаст новый объект, а всего лишь создаст ссылку на существующий объект.
//Ключевое слово clone - клонирует объект, создает идентичную копию объекта ($point_3 = clone $point_1)
//Метод __clone() - позволяет изменить значения свойств и методов в классе при клонировании объектов, то есть сначала клонируются объекты при использовании ключевого слово clone, а потом внутри класса меняются значения свойств и методов при помощи метода __clone()
require_once getenv("DOCUMENT_ROOT") . "/lib/config.php";
require_once "Point.php";
$point_1 = new Point(5, 2);
$point_2 = $point_1;
//так делали в старых версиях PHP
echo $point_1->getX() . "<br/>";
echo $point_1->getY() . "<br/>";
echo $point_2->getX() . "<br/>";
echo $point_2->getY() . "<br/>";
$point_2->setX(15);
echo $point_1->getX() . "<br/>";
$point_1->setX(25);
echo $point_2->getX() . "<br/>";
echo "<br/>----------------------------------<br/>";
$point_3 = clone $point_1;
echo $point_3->getX() . "<br/>";
echo $point_3->getY() . "<br/>";
echo $point_1->getX() . "<br/>";
echo $point_1->getY() . "<br/>";
$point_3->setX(17);
echo $point_1->getX() . "<br/>";
echo $point_3->getX() . "<br/>";
echo "<br/>----------------------------------<br/>";
echo $point_1->getY() . "<br/>";
echo $point_3->getY() . "<br/>";
Exemple #20
0
 /**
  * @param Point $a
  * @param Point $b
  *
  * @return bool
  */
 public function betweenY(Point $a, Point $b)
 {
     return $a->getY() <= $this->getY() && $this->getY() < $b->getY();
 }
Exemple #21
0
 public function hasPoint(Point $searchPoint)
 {
     $foundPoint = $this->getPoint($searchPoint->getX(), $searchPoint->getY());
     return $foundPoint instanceof Point;
 }