public function testEquality() { $p1 = new Point(10, 10); $p2 = new Point(10, 10); $p3 = new Point(100, 100); $this->assertTrue($p1->equals($p2)); $this->assertTrue($p2->equals($p1)); $this->assertFalse($p1->equals($p3)); $this->assertFalse($p3->equals($p1)); $this->assertFalse($p3->equals(1337)); $this->assertTrue($p3->equals($p3)); }
/** * Whether the given $point is inside the coordinates. * * @param Point $point A Point object. * @return boolean */ public function inside(Point $point) { if ($this->getShape() === Shape::DEF) { return true; } elseif ($this->getShape() === Shape::RECT) { return $point->getX() >= $this[0] && $point->getX() <= $this[2] && $point->getY() >= $this[1] && $point->getY() <= $this[3]; } elseif ($this->getShape() === Shape::CIRCLE) { return pow($point->getX() - $this[0], 2) + pow($point->getY() - $this[1], 2) < pow($this[2], 2); } else { // we consider it is a polygon. // - Transform coordinates in vertices. // -- Use of the "point in polygon" algorithm. $vertices = array(); for ($i = 0; $i < count($this); $i++) { $vertex = array(); $vertex[] = $this[$i]; //x $i++; $vertex[] = $this[$i]; //y $vertices[] = $vertex; } $intersects = 0; for ($i = 1; $i < count($vertices); $i++) { $vertex1 = $vertices[$i - 1]; $vertex2 = $vertices[$i]; if ($vertex1[1] === $vertex2[1] && $vertex1[1] === $point->getY() && $point->getX() > min($vertex1[0], $vertex2[0]) && $point->getX() < max($vertex1[0], $vertex2[0])) { // we are on a boundary. return true; } if ($point->getY() > min($vertex1[1], $vertex2[1]) && $point->getY() <= max($vertex1[1], $vertex2[1]) && $point->getX() <= max($vertex1[0], $vertex2[0]) && $vertex1[1] !== $vertex2[1]) { $xinters = ($point->getY() - $vertex1[1]) * ($vertex2[0] - $vertex1[0]) / ($vertex2[1] - $vertex1[1]) + $vertex1[0]; if ($xinters === $point->getX()) { // Again, we are on a boundary. return true; } if ($vertex1[0] === $vertex2[0] || $point->getX() <= $xinters) { // We have a single intersection. $intersects++; } } } // If we passed through an odd number of edges, we are in the polygon! return $intersects % 2 !== 0; } }
/** * Write a Point in the current binary stream. * * @param Point $point A Point object. * @throws QtiBinaryStreamAccessException */ public function writePoint(Point $point) { try { $this->writeShort($point->getX()); $this->writeShort($point->getY()); } catch (BinaryStreamAccessException $e) { $msg = "An error occured while writing a point."; throw new QtiBinaryStreamAccessException($msg, $this, QtiBinaryStreamAccessException::POINT, $e); } }
/** * Marshall a QTI point datatype into its PCI JSON Representation. * * @param \qtism\common\datatypes\Point $point * @return array */ protected function marshallPoint(Point $point) { return array('base' => array('point' => array($point->getX(), $point->getY()))); }