closedquestion-8.x-3.x-dev/src/Question/Mapping/CqHotspotPoly.php

src/Question/Mapping/CqHotspotPoly.php
<?php

namespace Drupal\closedquestion\Question\Mapping;

/**
 * Class CqHotspotPoly.
 *
 * A polygon shaped hotspot formed by a series of points.
 *
 * @package Drupal\closedquestion\Question\Mapping
 */
class CqHotspotPoly extends CqAbstractHotspot {

  /**
   * The array of x-coordinates of the points in the polygon.
   *
   * @var int[]
   */
  private $pointsX;

  /**
   * The array of y-coordinates of the points in the polygon.
   *
   * @var int[]
   */
  private $pointsY;

  /**
   * The coordinates of the polygon as a string.
   *
   * To be used in an html image-map.
   *
   * @var string
   */
  private $pointsString = '';

  /**
   * Creates a new CqHotspotPoly.
   *
   * @param string $identifier
   *   The identifier to use for this hotspot.
   * @param array $numbers
   *   An array of x and y coordinates in the form of x1,y1,x2,y2,x3,y3.
   */
  public function __construct($identifier, array $numbers) {
    $this->identifier = $identifier;
    $count = count($numbers);
    $this->pointsX = [];
    $this->pointsY = [];

    if (($count % 2) != 0) {
      \Drupal::messenger()->addMessage(t('HotSpotPoly: strange number count: @c', array('@c' => $count)));
      $count--;
    }
    $pointCount = $count / 2;
    $allPoints = [];
    for ($teller = 0; $teller < $pointCount; $teller++) {
      $x = $numbers[$teller * 2];
      $y = $numbers[$teller * 2 + 1];
      $this->pointsX[] = $x;
      $this->pointsY[] = $y;
      $allPoints[] = $x;
      $allPoints[] = $y;
    }
    $this->pointsString = implode(',', $allPoints);
  }

  /**
   * Implements CqHotspotInterface::doMatch()
   */
  public function doMatch($location) {
    $x = $location[0];
    $y = $location[1];
    $inside = FALSE;
    $npol = count($this->pointsX);
    $j = $npol - 1;
    for ($i = 0; $i < $npol; $j = $i++) {
      if ((($this->pointsY[$i] <= $y) && ($this->pointsY[$j] > $y)) ||
           (($this->pointsY[$j] <= $y) && ($this->pointsY[$i] > $y))) {
        // Possible intercept.
        $dx = $this->pointsX[$j] - $this->pointsX[$i];
        $dy = $this->pointsY[$j] - $this->pointsY[$i];
        $py = $this->pointsY[$j] - $y;
        $px = $dx * $py / $dy;
        $interceptX = $this->pointsX[$j] - $px;
        if ($x < $interceptX) {
          $inside = !$inside;
        }
      }
    }
    return $inside;
  }

  /**
   * Implements CqHotspotInterface::getMapHtml()
   */
  public function getMapHtml() {
    return 'shape="poly" coords="' . $this->pointsString . '"';
  }

  /**
   * Implements CqHotspotInterface::getMapData()
   */
  public function getMapData() {
    return array('shape' => 'poly', 'coords' => $this->pointsString);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc