closedquestion-8.x-3.x-dev/src/Question/Mapping/CqHotspotCircle.php
src/Question/Mapping/CqHotspotCircle.php
<?php
namespace Drupal\closedquestion\Question\Mapping;
/**
* Class CqHotspotCircle.
*
* Implements a circle-shaped hotspot formed by a centre point and a radius.
*
* @package Drupal\closedquestion\Question\Mapping
*/
class CqHotspotCircle extends CqAbstractHotspot {
/**
* The x-coordinate of the center of the hotspot.
*
* @var int
*/
private $x = 0;
/**
* The y-coordinate of the center of the hotspot.
*
* @var int
*/
private $y = 0;
/**
* The radius of the hotspot.
*
* @var int
*/
private $radius = 0;
/**
* The square of the radius.
*
* Its stored, so we don't need to re-calculate this or the root
* when calculating a distance from the center.
*
* @var int
*/
private $radius2 = 0;
/**
* Creates a new CqHotspotCircle.
*
* @param string $identifier
* The identifier to use for this hotspot.
* @param int $x
* The x-coordinate of the center of the hotspot.
* @param int $y
* The y-coordinate of the center of the hotspot.
* @param int $radius
* The radius of the hotpot.
*/
public function __construct($identifier, $x, $y, $radius) {
$this->identifier = $identifier;
$this->x = $x;
$this->y = $y;
$this->radius = $radius;
$this->radius2 = $radius * $radius;
}
/**
* Implements CqHotspotInterface::doMatch()
*/
public function doMatch($location) {
$x = $location[0];
$y = $location[1];
$dist2 = pow($this->x - $x, 2) + pow($this->y - $y, 2);
return ($dist2 < $this->radius2);
}
/**
* Implements CqHotspotInterface::getMapHtml()
*/
public function getMapHtml() {
return 'shape="circle" coords="' . $this->x . ',' . $this->y . ',' . $this->radius . '"';
}
/**
* Implements CqHotspotInterface::getMapData()
*/
public function getMapData() {
return array('shape' => 'circle', 'coords' => $this->x . ',' . $this->y . ',' . $this->radius);
}
}
