From 3397bf162df2b34a2857a6e111978fb1823b4372 Mon Sep 17 00:00:00 2001 From: "Kamshory, MT" Date: Wed, 24 Sep 2025 06:45:12 +0700 Subject: [PATCH] Move Mathematic Classes --- src/ComplexNumber/ComplexNumber.php | 170 --------- src/Exceptions/MatrixCalculationException.php | 51 --- src/Geometry/Area.php | 218 ----------- src/Geometry/Circle.php | 81 ----- src/Geometry/LatBounds.php | 143 -------- src/Geometry/LatLng.php | 112 ------ src/Geometry/LatLngBounds.php | 246 ------------- src/Geometry/Line.php | 58 --- src/Geometry/LngBounds.php | 180 --------- src/Geometry/Map.php | 61 ---- src/Geometry/NodeAttribute.php | 53 --- src/Geometry/Point.php | 79 ---- src/Geometry/Polygon.php | 119 ------ src/Geometry/Rectangle.php | 88 ----- src/Geometry/SphericalGeometry.php | 344 ------------------ src/Geometry/Triangle.php | 86 ----- src/Matrix/Matrix.php | 139 ------- 17 files changed, 2228 deletions(-) delete mode 100644 src/ComplexNumber/ComplexNumber.php delete mode 100644 src/Exceptions/MatrixCalculationException.php delete mode 100644 src/Geometry/Area.php delete mode 100644 src/Geometry/Circle.php delete mode 100644 src/Geometry/LatBounds.php delete mode 100644 src/Geometry/LatLng.php delete mode 100644 src/Geometry/LatLngBounds.php delete mode 100644 src/Geometry/Line.php delete mode 100644 src/Geometry/LngBounds.php delete mode 100644 src/Geometry/Map.php delete mode 100644 src/Geometry/NodeAttribute.php delete mode 100644 src/Geometry/Point.php delete mode 100644 src/Geometry/Polygon.php delete mode 100644 src/Geometry/Rectangle.php delete mode 100644 src/Geometry/SphericalGeometry.php delete mode 100644 src/Geometry/Triangle.php delete mode 100644 src/Matrix/Matrix.php diff --git a/src/ComplexNumber/ComplexNumber.php b/src/ComplexNumber/ComplexNumber.php deleted file mode 100644 index 8f5d6dfd..00000000 --- a/src/ComplexNumber/ComplexNumber.php +++ /dev/null @@ -1,170 +0,0 @@ -real = $real; - $this->imaginary = $imaginary; - } - - /** - * Add another complex number to this one. - * - * @param self $self The complex number to add. - * @return self The sum of the two complex numbers. - */ - public function add($self) { - return new self( - $this->real + $self->getReal(), - $this->imaginary + $self->getImaginary() - ); - } - - /** - * Subtract another complex number from this one. - * - * @param self $self The complex number to subtract. - * @return self The result of the subtraction. - */ - public function subtract($self) { - return new self( - $this->real - $self->getReal(), - $this->imaginary - $self->getImaginary() - ); - } - - /** - * Multiply this complex number by another. - * - * @param self $self The complex number to multiply. - * @return self The product of the two complex numbers. - */ - public function multiply($self) { - $real = $this->real * $self->getReal() - - $this->imaginary * $self->getImaginary(); - - $imaginary = $this->real * $self->getImaginary() - + $this->imaginary * $self->getReal(); - - return new self($real, $imaginary); - } - - /** - * Divide this complex number by another. - * - * @param self $self The complex number to divide by. - * @return self The result of the division. - */ - public function divide($self) { - $denominator = $self->getReal() ** 2 - + $self->getImaginary() ** 2; - - $real = ($this->real * $self->getReal() - + $this->imaginary * $self->getImaginary()) - / $denominator; - - $imaginary = ($this->imaginary * $self->getReal() - - $this->real * $self->getImaginary()) - / $denominator; - - return new self($real, $imaginary); - } - - /** - * Get the magnitude of the complex number. - * - * @return float The magnitude of the complex number. - */ - public function magnitude() { - return sqrt($this->real ** 2 + $this->imaginary ** 2); - } - - /** - * Get the conjugate of the complex number. - * - * @return self The conjugate of the complex number. - */ - public function conjugate() { - return new self($this->real, -$this->imaginary); - } - - /** - * String representation of the complex number. - * - * @return string The complex number as a string. - */ - public function __toString() { - return "({$this->real}, {$this->imaginary}i)"; - } - - /** - * Get the real part of the complex number. - * - * @return float The real part. - */ - public function getReal() { - return $this->real; - } - - /** - * Set the real part of the complex number. - * - * @param float $real The real part. - * @return self Returns the current instance for method chaining. - */ - public function setReal($real) { - $this->real = $real; - - return $this; - } - - /** - * Get the imaginary part of the complex number. - * - * @return float The imaginary part. - */ - public function getImaginary() { - return $this->imaginary; - } - - /** - * Set the imaginary part of the complex number. - * - * @param float $imaginary The imaginary part. - * @return self Returns the current instance for method chaining. - */ - public function setImaginary($imaginary) { - $this->imaginary = $imaginary; - - return $this; - } -} diff --git a/src/Exceptions/MatrixCalculationException.php b/src/Exceptions/MatrixCalculationException.php deleted file mode 100644 index 445329a7..00000000 --- a/src/Exceptions/MatrixCalculationException.php +++ /dev/null @@ -1,51 +0,0 @@ -previous = $previous; - } - - /** - * Get the previous exception. - * - * @return Throwable|null The previous exception - */ - public function getPreviousException() - { - return $this->previous; - } -} diff --git a/src/Geometry/Area.php b/src/Geometry/Area.php deleted file mode 100644 index c1dbafc0..00000000 --- a/src/Geometry/Area.php +++ /dev/null @@ -1,218 +0,0 @@ -href = $href; - $this->attributes = is_array($attributes) ? $attributes : array(); - $this->zoom = $zoom; - - if ($object instanceof Rectangle) { - $this->shape = "rect"; - $this->coords = $this->coordsFromRectangle($object); - } elseif ($object instanceof Triangle) { - $this->shape = "poly"; - $this->coords = $this->coordsFromTriangle($object); - } elseif ($object instanceof Polygon) { - $this->shape = "poly"; - $this->coords = $this->coordsFromPolygon($object); - } elseif ($object instanceof Circle) { - $this->shape = "circle"; - $this->coords = $this->coordsFromCircle($object); - } - } - - /** - * Get coordinates from a rectangle. - * - * @param Rectangle $object Rectangle object - * @return float[] Coordinates of the rectangle - */ - public function coordsFromRectangle($object) - { - return array( - $object->a->x, - $object->a->y, - $object->b->x, - $object->b->y - ); - } - - /** - * Get coordinates from a triangle. - * - * @param Triangle $object Triangle object - * @return float[] Coordinates of the triangle - */ - public function coordsFromTriangle($object) - { - return array( - $object->a->x, - $object->a->y, - $object->b->x, - $object->b->y, - $object->c->x, - $object->c->y - ); - } - - /** - * Get coordinates from a polygon. - * - * @param Polygon $object Polygon object - * @return float[] Coordinates of the polygon - */ - public function coordsFromPolygon($object) - { - $points = $object->getPoints(); - $coords = array(); - foreach ($points as $point) { - $coords[] = $point->x; - $coords[] = $point->y; - } - return $coords; - } - - /** - * Get coordinates from a circle. - * - * @param Circle $object Circle object - * @return float[] Coordinates of the circle - */ - public function coordsFromCircle($object) - { - return array($object->x, $object->y, $object->r); - } - - /** - * Get coordinates with optional zoom factor. - * - * @param float $zoom Zoom factor (default: 1) - * @return float[] Adjusted coordinates - */ - public function getCoords($zoom = 1) - { - if ($zoom <= 0) { - $zoom = abs($zoom); - } - return array_map(function($value) use ($zoom) { - return $value * $zoom; - }, $this->coords); - } - - /** - * Generate HTML representation of the area. - * - * @return string HTML string for the area - */ - public function getHTML() - { - $attrs = array(); - $attrs[] = 'shape="' . $this->shape . '"'; - $attrs[] = 'coords="' . implode(", ", $this->getCoords($this->zoom)) . '"'; - - if (isset($this->href)) { - $attrs[] = 'href="' . $this->href . '"'; - } - - if (isset($this->attributes) && is_array($this->attributes)) { - foreach ($this->attributes as $key => $value) { - $attrs[] = $key . '="' . $value . '"'; - } - } - - return ''; - } - - /** - * Convert the area object to a string. - * - * @return string HTML representation of the area - */ - public function __toString() - { - return $this->getHTML(); - } - - /** - * Get the current zoom factor. - * - * @return float Zoom factor - */ - public function getZoom() - { - return $this->zoom; - } - - /** - * Set the zoom factor. - * - * @param float $zoom Zoom factor - * @return self Returns the current instance for method chaining. - */ - public function setZoom($zoom) - { - $this->zoom = $zoom; - return $this; - } -} diff --git a/src/Geometry/Circle.php b/src/Geometry/Circle.php deleted file mode 100644 index d9a0c3ad..00000000 --- a/src/Geometry/Circle.php +++ /dev/null @@ -1,81 +0,0 @@ -x = $x; - $this->y = $y; - $this->r = $r; - $this->center = new Point($x, $y); - } - - /** - * Get the circumference of the circle. - * - * @return float Circumference of the circle - */ - public function getCircumference() - { - return 2 * pi() * $this->r; - } - - /** - * Get the area of the circle. - * - * @return float Area of the circle - */ - public function getArea() - { - return pi() * $this->r * $this->r; - } -} diff --git a/src/Geometry/LatBounds.php b/src/Geometry/LatBounds.php deleted file mode 100644 index de14e484..00000000 --- a/src/Geometry/LatBounds.php +++ /dev/null @@ -1,143 +0,0 @@ -_swLat = $swLat; - $this->_neLat = $neLat; - } - - /** - * Get the southwestern latitude. - * - * @return float The southwestern latitude. - */ - public function getSw() - { - return $this->_swLat; - } - - /** - * Get the northeastern latitude. - * - * @return float The northeastern latitude. - */ - public function getNe() - { - return $this->_neLat; - } - - /** - * Calculate the midpoint latitude between the southwestern and northeastern latitudes. - * - * @return float The midpoint latitude. - */ - public function getMidpoint() - { - return ($this->_swLat + $this->_neLat) / 2; - } - - /** - * Check if the latitude bounds are empty (i.e., invalid). - * - * @return bool true if the bounds are empty, false otherwise. - */ - public function isEmpty() - { - return $this->_swLat > $this->_neLat; - } - - /** - * Determine if this LatBounds intersects with another LatBounds. - * - * @param LatBounds $latBounds The other LatBounds to check for intersection. - * @return bool true if there is an intersection, false otherwise. - */ - public function intersects($latBounds) - { - return $this->_swLat <= $latBounds->getSw() - ? $latBounds->getSw() <= $this->_neLat && $latBounds->getSw() <= $latBounds->getNe() - : $this->_swLat <= $latBounds->getNe() && $this->_swLat <= $this->_neLat; - } - - /** - * Check if this LatBounds is equal to another LatBounds within a certain margin of error. - * - * @param LatBounds $latBounds The other LatBounds to compare. - * @return bool true if they are equal, false otherwise. - */ - public function equals($latBounds) - { - return $this->isEmpty() - ? $latBounds->isEmpty() - : abs($latBounds->getSw() - $this->_swLat) - + abs($this->_neLat - $latBounds->getNe()) - <= SphericalGeometry::EQUALS_MARGIN_ERROR; - } - - /** - * Check if a given latitude is contained within the bounds. - * - * @param float $lat The latitude to check. - * @return bool true if the latitude is contained, false otherwise. - */ - public function contains($lat) - { - return $lat >= $this->_swLat && $lat <= $this->_neLat; - } - - /** - * Extend the bounds to include a new latitude. - * - * If the bounds are empty, the latitude becomes both the southwestern and northeastern bounds. - * If the latitude is less than the southwestern bound, it updates the southwestern bound. - * If the latitude is greater than the northeastern bound, it updates the northeastern bound. - * - * @param float $lat The latitude to extend the bounds with. - */ - public function extend($lat) - { - if ($this->isEmpty()) - { - $this->_neLat = $this->_swLat = $lat; - } - else if ($lat < $this->_swLat) - { - $this->_swLat = $lat; - } - else if ($lat > $this->_neLat) - { - $this->_neLat = $lat; - } - } -} diff --git a/src/Geometry/LatLng.php b/src/Geometry/LatLng.php deleted file mode 100644 index 86e4e5d5..00000000 --- a/src/Geometry/LatLng.php +++ /dev/null @@ -1,112 +0,0 @@ - Invalid float numbers: ('. $lat .', '. $lng .')', E_USER_ERROR); - } - - if ($noWrap !== true) { - $lat = SphericalGeometry::clampLatitude($lat); - $lng = SphericalGeometry::wrapLongitude($lng); - } - - $this->_lat = $lat; - $this->_lng = $lng; - } - - /** - * Get the latitude value. - * - * @return float The latitude. - */ - public function getLat() - { - return $this->_lat; - } - - /** - * Get the longitude value. - * - * @return float The longitude. - */ - public function getLng() - { - return $this->_lng; - } - - /** - * Check if this LatLng is equal to another LatLng object within a certain margin of error. - * - * @param LatLng $latLng The LatLng object to compare. - * @return bool true if they are equal, false otherwise. - */ - public function equals($latLng) - { - if (!is_object($latLng) || !($latLng instanceof self)) { - return false; - } - - return abs($this->_lat - $latLng->getLat()) <= SphericalGeometry::EQUALS_MARGIN_ERROR - && abs($this->_lng - $latLng->getLng()) <= SphericalGeometry::EQUALS_MARGIN_ERROR; - } - - /** - * Convert the LatLng object to a string representation. - * - * @return string The string representation of the LatLng in the format "(lat, lng)". - */ - public function toString() - { - return '('. $this->_lat .', '. $this->_lng .')'; - } - - /** - * Convert the LatLng object to a URL-friendly string value. - * - * @param int $precision The number of decimal places to round to (default: 6). - * @return string The latitude and longitude values as a string. - */ - public function toUrlValue($precision = 6) - { - $precision = (int) $precision; - return round($this->_lat, $precision) .','. round($this->_lng, $precision); - } -} diff --git a/src/Geometry/LatLngBounds.php b/src/Geometry/LatLngBounds.php deleted file mode 100644 index e785109c..00000000 --- a/src/Geometry/LatLngBounds.php +++ /dev/null @@ -1,246 +0,0 @@ - Invalid LatLng object.', E_USER_ERROR); - } - - if ($latLngSw && !$tatLngNe) - { - $tatLngNe = $latLngSw; - } - - if ($latLngSw) - { - $sw = SphericalGeometry::clampLatitude($latLngSw->getLat()); - $ne = SphericalGeometry::clampLatitude($tatLngNe->getLat()); - $this->_latBounds = new LatBounds($sw, $ne); - - $sw = $latLngSw->getLng(); - $ne = $tatLngNe->getLng(); - - if ($ne - $sw >= 360) - { - $this->_lngBounds = new LngBounds(-180, 180); - } - else - { - $sw = SphericalGeometry::wrapLongitude($latLngSw->getLng()); - $ne = SphericalGeometry::wrapLongitude($tatLngNe->getLng()); - $this->_lngBounds = new LngBounds($sw, $ne); - } - } - else - { - $this->_latBounds = new LatBounds(1, -1); - $this->_lngBounds = new LngBounds(180, -180); - } - } - - /** - * Get the latitude bounds. - * - * @return LatBounds The latitude bounds of the bounding box. - */ - public function getLatBounds() - { - return $this->_latBounds; - } - - /** - * Get the longitude bounds. - * - * @return LngBounds The longitude bounds of the bounding box. - */ - public function getLngBounds() - { - return $this->_lngBounds; - } - - /** - * Get the center point of the bounding box. - * - * @return LatLng The center point as a LatLng object. - */ - public function getCenter() - { - return new LatLng($this->_latBounds->getMidpoint(), $this->_lngBounds->getMidpoint()); - } - - /** - * Check if the bounding box is empty. - * - * @return bool true if the bounding box is empty, false otherwise. - */ - public function isEmpty() - { - return $this->_latBounds->isEmpty() || $this->_lngBounds->isEmpty(); - } - - /** - * Get the southwestern corner of the bounding box. - * - * @return LatLng The southwestern corner as a LatLng object. - */ - public function getSouthWest() - { - return new LatLng($this->_latBounds->getSw(), $this->_lngBounds->getSw(), true); - } - - /** - * Get the northeastern corner of the bounding box. - * - * @return LatLng The northeastern corner as a LatLng object. - */ - public function getNorthEast() - { - return new LatLng($this->_latBounds->getNe(), $this->_lngBounds->getNe(), true); - } - - /** - * Get the span of the bounding box as a LatLng object. - * - * @return LatLng The span defined by the latitude and longitude differences. - */ - public function toSpan() - { - if ($this->_latBounds->isEmpty()) { - $lat = 0; - } else { - $lat = $this->_latBounds->getNe() - $this->_latBounds->getSw(); - } - - if ($this->_lngBounds->isEmpty()) { - $lng = 0; - } else { - if ($this->_lngBounds->getSw() > $this->_lngBounds->getNe()) { - $lng = 360 - ($this->_lngBounds->getSw() - $this->_lngBounds->getNe()); - } else { - $lng = $this->_lngBounds->getNe() - $this->_lngBounds->getSw(); - } - } - - return new LatLng($lat, $lng, true); - } - - - /** - * Convert the bounding box to a string representation. - * - * @return string The string representation of the bounding box. - */ - public function toString() - { - return '('. $this->getSouthWest()->toString() .', '. $this->getNorthEast()->toString() .')'; - } - - /** - * Convert the bounding box to a URL-friendly string value. - * - * @param int $precision The number of decimal places to round to. - * @return string The southwest and northeast corner values as a string. - */ - public function toUrlValue($precision = 6) - { - return $this->getSouthWest()->toUrlValue($precision) .','. - $this->getNorthEast()->toUrlValue($precision); - } - - /** - * Check if this LatLngBounds is equal to another LatLngBounds object. - * - * @param LatLngBounds $latLngBounds The LatLngBounds object to compare. - * @return bool true if they are equal, false otherwise. - */ - public function equals($latLngBounds) - { - return !$latLngBounds - ? false - : $this->_latBounds->equals($latLngBounds->getLatBounds()) - && $this->_lngBounds->equals($latLngBounds->getLngBounds()); - } - - /** - * Check if this LatLngBounds intersects with another LatLngBounds. - * - * @param LatLngBounds $latLngBounds The LatLngBounds to check for intersection. - * @return bool true if they intersect, false otherwise. - */ - public function intersects($latLngBounds) - { - return $this->_latBounds->intersects($latLngBounds->getLatBounds()) - && $this->_lngBounds->intersects($latLngBounds->getLngBounds()); - } - - /** - * Extend this bounding box to include another LatLngBounds. - * - * @param LatLngBounds $latLngBounds The LatLngBounds to extend with. - * @return $this The current instance for method chaining. - */ - public function union($latLngBounds) - { - $this->extend($latLngBounds->getSouthWest()); - $this->extend($latLngBounds->getNorthEast()); - return $this; - } - - /** - * Check if this LatLngBounds contains a specific LatLng point. - * - * @param LatLng $latLng The LatLng point to check for containment. - * @return bool true if the point is contained, false otherwise. - */ - public function contains($latLng) - { - return $this->_latBounds->contains($latLng->getLat()) - && $this->_lngBounds->contains($latLng->getLng()); - } - - /** - * Extend the bounding box to include a new LatLng point. - * - * @param LatLng $latLng The LatLng point to extend with. - * @return $this The current instance for method chaining. - */ - public function extend($latLng) - { - $this->_latBounds->extend($latLng->getLat()); - $this->_lngBounds->extend($latLng->getLng()); - return $this; - } -} diff --git a/src/Geometry/Line.php b/src/Geometry/Line.php deleted file mode 100644 index 952cb605..00000000 --- a/src/Geometry/Line.php +++ /dev/null @@ -1,58 +0,0 @@ -a = $a; - $this->b = $b; - } - - /** - * Method to calculate the length of the line. - * - * This method calculates the Euclidean distance between Point A and Point B. - * - * @return float The length of the line between Point A and Point B. - */ - public function getLength() { - return $this->a->distanceFrom($this->b); - } -} diff --git a/src/Geometry/LngBounds.php b/src/Geometry/LngBounds.php deleted file mode 100644 index 2ccffd06..00000000 --- a/src/Geometry/LngBounds.php +++ /dev/null @@ -1,180 +0,0 @@ -_swLng = $swLng; - $this->_neLng = $neLng; - } - - /** - * Get the southwestern longitude. - * - * @return float The southwestern longitude value. - */ - public function getSw() - { - return $this->_swLng; - } - - /** - * Get the northeastern longitude. - * - * @return float The northeastern longitude value. - */ - public function getNe() - { - return $this->_neLng; - } - - /** - * Get the midpoint of the longitude bounds. - * - * @return float The midpoint longitude value. - */ - public function getMidpoint() - { - $midPoint = ($this->_swLng + $this->_neLng) / 2; - - if ($this->_swLng > $this->_neLng) - { - $midPoint = SphericalGeometry::wrapLongitude($midPoint + 180); - } - - return $midPoint; - } - - /** - * Check if the bounds are empty. - * - * @return bool true if the bounds are empty, false otherwise. - */ - public function isEmpty() - { - return $this->_swLng - $this->_neLng == 360; - } - - /** - * Check if this LngBounds intersects with another LngBounds. - * - * @param LngBounds $lngBounds The LngBounds to check for intersection. - * @return bool true if they intersect, false otherwise. - */ - public function intersects($lngBounds) // NOSONAR - { - if ($this->isEmpty() || $lngBounds->isEmpty()) - { - return false; - } - else if ($this->_swLng > $this->_neLng) - { - return $lngBounds->getSw() > $lngBounds->getNe() - || $lngBounds->getSw() <= $this->_neLng - || $lngBounds->getNe() >= $this->_swLng; - } - else if ($lngBounds->getSw() > $lngBounds->getNe()) - { - return $lngBounds->getSw() <= $this->_neLng || $lngBounds->getNe() >= $this->_swLng; - } - else - { - return $lngBounds->getSw() <= $this->_neLng && $lngBounds->getNe() >= $this->_swLng; - } - } - - /** - * Check if this LngBounds is equal to another LngBounds. - * - * @param LngBounds $lngBounds The LngBounds object to compare. - * @return bool true if they are equal, false otherwise. - */ - public function equals($lngBounds) - { - return $this->isEmpty() - ? $lngBounds->isEmpty() - : fmod(abs($lngBounds->getSw() - $this->_swLng), 360) - + fmod(abs($lngBounds->getNe() - $this->_neLng), 360) - <= SphericalGeometry::EQUALS_MARGIN_ERROR; - } - - /** - * Check if a given longitude is contained within the bounds. - * - * @param float $lng The longitude to check. - * @return bool true if the longitude is contained, false otherwise. - */ - public function contains($lng) - { - $lng = $lng == -180 ? 180 : $lng; - - return $this->_swLng > $this->_neLng - ? ($lng >= $this->_swLng || $lng <= $this->_neLng) && !$this->isEmpty() - : $lng >= $this->_swLng && $lng <= $this->_neLng; - } - - /** - * Extend the bounds to include a new longitude. - * - * @param float $lng The longitude to extend the bounds with. - * @return void - */ - public function extend($lng) - { - if ($this->contains($lng)) - { - return; - } - - if ($this->isEmpty()) - { - $this->_swLng = $this->_neLng = $lng; - } - else - { - $swLng = $this->_swLng - $lng; - $swLng = $swLng >= 0 ? $swLng : $this->_swLng + 180 - ($lng - 180); - $neLng = $lng - $this->_neLng; - $neLng = $neLng >= 0 ? $neLng : $lng + 180 - ($this->_neLng - 180); - - if ($swLng < $neLng) - { - $this->_swLng = $lng; - } - else - { - $this->_neLng = $lng; - } - } - } -} diff --git a/src/Geometry/Map.php b/src/Geometry/Map.php deleted file mode 100644 index c84a0ada..00000000 --- a/src/Geometry/Map.php +++ /dev/null @@ -1,61 +0,0 @@ -areas = $areas; - } - } - - /** - * Add an area to the map. - * - * This method appends a new Area object to the map's collection of areas. - * - * @param Area $area Area to add - * @return self Returns the current instance for method chaining. - */ - public function addArea($area) - { - $this->areas[] = $area; - return $this; - } - - /** - * Get all areas in the map. - * - * This method returns an array of Area objects contained in the map. - * - * @return Area[] An array of Area objects - */ - public function getAreas() - { - return $this->areas; - } -} diff --git a/src/Geometry/NodeAttribute.php b/src/Geometry/NodeAttribute.php deleted file mode 100644 index eac6b323..00000000 --- a/src/Geometry/NodeAttribute.php +++ /dev/null @@ -1,53 +0,0 @@ -values = $values; - } - - /** - * Convert the node attributes to a string representation. - * - * This method returns the attributes in a format suitable for inclusion in HTML tags, - * where each attribute is represented as key="value". - * - * @return string A string representation of the node attributes. - */ - public function __toString() - { - $attributes = array(); - if (isset($this->values) && is_array($this->values)) { - foreach ($this->values as $key => $value) { - $attributes[] = $key . '="' . htmlspecialchars($value) . '"'; - } - } - return implode(' ', $attributes); - } -} diff --git a/src/Geometry/Point.php b/src/Geometry/Point.php deleted file mode 100644 index 35afe31e..00000000 --- a/src/Geometry/Point.php +++ /dev/null @@ -1,79 +0,0 @@ -x = $x; - $this->y = $y; - } - - /** - * Calculate the distance between this Point and another Point. - * - * This method computes the Euclidean distance between the current Point - * and another Point provided as an argument. - * - * @param Point $p Another Point. - * @return float The distance between the two Points. - * @throws InvalidArgumentException If the argument is not of type Point. - */ - public function distanceFrom($p) - { - // Ensure $p is an instance of Point - if (!$p instanceof Point) { - throw new InvalidArgumentException('Argument must be of type Point.'); - } - - $dx = $this->x - $p->x; - $dy = $this->y - $p->y; - return sqrt($dx * $dx + $dy * $dy); - } - - /** - * Calculate the distance between this Point and another Point. - * - * This method serves as an alias to distanceFrom for better readability. - * - * @param Point $p Another Point. - * @return float The distance between the two Points. - */ - public function distanceTo($p) - { - return $this->distanceFrom($p); - } -} diff --git a/src/Geometry/Polygon.php b/src/Geometry/Polygon.php deleted file mode 100644 index 2ac37c0f..00000000 --- a/src/Geometry/Polygon.php +++ /dev/null @@ -1,119 +0,0 @@ -points = $points; - } - - /** - * Add a point to the polygon. - * - * @param Point $point Point to add. - * @return self Returns the current instance for method chaining. - */ - public function addPoint($point) - { - $this->points[] = $point; - return $this; - } - - /** - * Clear all points from the polygon. - * - * This method removes all points currently defined for the polygon. - * - * @return self Returns the current instance for method chaining. - */ - public function clearPolygon() - { - $this->points = array(); - return $this; - } - - /** - * Calculate the area of the polygon using the Shoelace formula. - * - * @return float The area of the polygon. - * @throws InvalidPolygonException If the polygon has fewer than 3 points. - */ - public function getArea() - { - $cnt = count($this->points); - if ($cnt < 3) { - throw new InvalidPolygonException("Invalid polygon. A polygon must have at least 3 points. $cnt given."); - } - - $sum = 0; - for ($i = 0; $i < $cnt; $i++) { - $p1 = $this->points[$i]; - $p2 = $this->points[($i + 1) % $cnt]; // Wrap around to the first point - $sum += ($p1->x * $p2->y) - ($p2->x * $p1->y); - } - - return abs($sum) / 2; - } - - /** - * Calculate the circumference of the polygon. - * - * This method computes the total length of the polygon's edges. - * - * @return float The circumference of the polygon. - * @throws InvalidPolygonException If the polygon has fewer than 2 points. - */ - public function getCircumference() - { - $cnt = count($this->points); - if ($cnt < 2) { - throw new InvalidPolygonException("Invalid polygon. A polygon must have at least 2 points. $cnt given."); - } - - $sum = 0; - for ($i = 0; $i < $cnt; $i++) { - $p1 = $this->points[$i]; - $p2 = $this->points[($i + 1) % $cnt]; // Wrap around to the first point - $l = new Line($p1, $p2); - $sum += $l->getLength(); - } - - return $sum; - } - - /** - * Get the points of the polygon. - * - * @return Point[] An array of Point objects that define the polygon. - */ - public function getPoints() - { - return $this->points; - } -} diff --git a/src/Geometry/Rectangle.php b/src/Geometry/Rectangle.php deleted file mode 100644 index 5cb7cc2c..00000000 --- a/src/Geometry/Rectangle.php +++ /dev/null @@ -1,88 +0,0 @@ -polygon = new Polygon(); - - // Assign the points - $this->a = $a; - $this->b = $b; - - // Define the rectangle's corners - $point1 = new Point($this->a->x, $this->a->y); - $point2 = new Point($this->b->x, $this->a->y); - $point3 = new Point($this->b->x, $this->b->y); - $point4 = new Point($this->a->x, $this->b->y); - - // Add the points to the polygon - $this->polygon - ->addPoint($point1) - ->addPoint($point2) - ->addPoint($point3) - ->addPoint($point4); - } - - /** - * Calculate the area of the rectangle. - * - * @return float The area of the rectangle. - * @throws InvalidPolygonException If the polygon is invalid. - */ - public function getArea() { - return $this->polygon->getArea(); - } - - /** - * Get the circumference (perimeter) of the rectangle. - * - * @return float The circumference of the rectangle. - * @throws InvalidPolygonException If the polygon is invalid. - */ - public function getCircumference() { - return $this->polygon->getCircumference(); - } -} diff --git a/src/Geometry/SphericalGeometry.php b/src/Geometry/SphericalGeometry.php deleted file mode 100644 index 00cae752..00000000 --- a/src/Geometry/SphericalGeometry.php +++ /dev/null @@ -1,344 +0,0 @@ -getLat()))); - - // SW point - $swLat = $latLng->getLat() - $latDegreesDistance; - $swLng = $latLng->getLng() - $lngDegreesDistance; - $sw = new LatLng($swLat, $swLng); - - // NE point - $neLat = $latLng->getLat() + $latDegreesDistance; - $neLng = $latLng->getLng() + $lngDegreesDistance; - $ne = new LatLng($neLat, $neLng); - - return new LatLngBounds($sw, $ne); - } - - /** - * Computes the heading from one LatLng to another. - * - * @param LatLng $fromLatLng The starting LatLng. - * @param LatLng $toLatLng The destination LatLng. - * @return float The heading in degrees from the starting point to the destination. - */ - public static function computeHeading($fromLatLng, $toLatLng) - { - $fromLat = deg2rad($fromLatLng->getLat()); - $toLat = deg2rad($toLatLng->getLat()); - $lng = deg2rad($toLatLng->getLng()) - deg2rad($fromLatLng->getLng()); - - $wrap = self::wrapLongitude(rad2deg(atan2(sin($lng) * cos($toLat), cos($fromLat) - * sin($toLat) - sin($fromLat) * cos($toLat) * cos($lng)))); - return ($wrap + 360) % 360; - } - - /** - * Computes a new LatLng based on a starting point, distance, and heading. - * - * @param LatLng $fromLatLng The starting LatLng. - * @param float $distance The distance to move, in meters. - * @param float $heading The direction to move, in degrees. - * @return LatLng The new LatLng after the offset. - */ - public static function computeOffset($fromLatLng, $distance, $heading) - { - $distance /= self::EARTH_RADIUS; - $heading = deg2rad($heading); - $fromLat = deg2rad($fromLatLng->getLat()); - $cosDistance = cos($distance); - $sinDistance = sin($distance); - $sinFromLat = sin($fromLat); - $cosFromLat = cos($fromLat); - $sc = $cosDistance * $sinFromLat + $sinDistance * $cosFromLat * cos($heading); - - $lat = rad2deg(asin($sc)); - $lng = rad2deg(deg2rad($fromLatLng->getLng()) + atan2($sinDistance * $cosFromLat - * sin($heading), $cosDistance - $sinFromLat * $sc)); - - return new LatLng($lat, $lng); - } - - /** - * Determines whether a point is contained within a polygon. - * - * @param object $point A point object with x() and y() methods for longitude and latitude. - * @param object $polygon A polygon object with an exteriorRing() method. - * @return bool true if the point is inside the polygon, false otherwise. - */ - public static function containsLocation($point, $polygon) { - $vertices_x = array(); // x-coordinates of the vertices of the polygon - $vertices_y = array(); // y-coordinates of the vertices of the polygon - $longitude_x = $point->x(); // x-coordinate of the point to test - $latitude_y = $point->y(); // y-coordinate of the point to test - - $polygon_array = $polygon->exteriorRing()->getComponents(); - foreach ($polygon_array as $value) { - array_push($vertices_x, $value->x()); - array_push($vertices_y, $value->y()); - } - // number vertices - zero-based array - $points_polygon = count($vertices_x) - 1; - $i = $j = $c = 0; - for ($i = 0, $j = $points_polygon ; $i < $points_polygon; $j = $i++) { - if ((($vertices_y[$i] > $latitude_y) != ($vertices_y[$j] > $latitude_y)) && - ($longitude_x < ($vertices_x[$j] - $vertices_x[$i]) * ($latitude_y - $vertices_y[$i]) / ($vertices_y[$j] - $vertices_y[$i]) + $vertices_x[$i])) { - $c = !$c; - } - } - return $c; - } - - /** - * Interpolates between two LatLng points based on a fraction. - * - * @param LatLng $fromLatLng The starting LatLng. - * @param LatLng $toLatLng The ending LatLng. - * @param float $fraction A fraction between 0 and 1. - * @return LatLng The interpolated LatLng. - */ - public static function interpolate($fromLatLng, $toLatLng, $fraction) - { - $radFromLat = deg2rad($fromLatLng->getLat()); - $radFromLng = deg2rad($fromLatLng->getLng()); - $radToLat = deg2rad($toLatLng->getLat()); - $radToLng = deg2rad($toLatLng->getLng()); - $cosFromLat = cos($radFromLat); - $cosToLat = cos($radToLat); - $radDist = self::_computeDistanceInRadiansBetween($fromLatLng, $toLatLng); - $sinRadDist = sin($radDist); - - if ($sinRadDist < 1.0E-6) { - return new LatLng($fromLatLng->getLat(), $fromLatLng->getLng()); - } - - $a = sin((1 - $fraction) * $radDist) / $sinRadDist; - $b = sin($fraction * $radDist) / $sinRadDist; - $c = $a * $cosFromLat * cos($radFromLng) + $b * $cosToLat * cos($radToLng); - $d = $a * $cosFromLat * sin($radFromLng) + $b * $cosToLat * sin($radToLng); - - $lat = rad2deg(atan2($a * sin($radFromLat) + $b * sin($radToLat), sqrt(pow($c,2) + pow($d,2)))); - $lng = rad2deg(atan2($d, $c)); - - return new LatLng($lat, $lng); - } - - /** - * Computes the distance between two LatLng points. - * - * @param LatLng $latLng1 The first LatLng point. - * @param LatLng $latLng2 The second LatLng point. - * @return float The distance in yards. - */ - public static function computeDistanceBetween($latLng1, $latLng2) - { - return self::_computeDistanceInRadiansBetween($latLng1, $latLng2) * self::EARTH_RADIUS * 1.09361; - } - - /** - * Computes the total length of a series of LatLng points. - * - * @param LatLng[] $latLngsArray An array of LatLng points. - * @return float The total length in yards. - */ - public static function computeLength($latLngsArray) - { - $length = 0; - - for ($i = 0, $l = count($latLngsArray) - 1; $i < $l; ++$i) { - $length += self::computeDistanceBetween($latLngsArray[$i], $latLngsArray[$i + 1]); - } - - return $length; - } - - /** - * Computes the area of a polygon defined by a series of LatLng points. - * - * @param LatLng[] $latLngsArray An array of LatLng points defining the polygon. - * @return float The area in square meters. - */ - public static function computeArea($latLngsArray) - { - return abs(self::computeSignedArea($latLngsArray, false)); - } - - /** - * Computes the signed area of a polygon defined by a series of LatLng points. - * - * @param LatLng[] $latLngsArray An array of LatLng points defining the polygon. - * @param bool $signed Whether to return a signed area. - * @return float The signed area in square meters. - */ - public static function computeSignedArea($latLngsArray, $signed = true) - { - if (empty($latLngsArray) || count($latLngsArray) < 3) - { - return 0; - } - - $e = 0; - $r2 = pow(self::EARTH_RADIUS, 2); - - for ($i = 1, $l = count($latLngsArray) - 1; $i < $l; ++$i) { - $e += self::_computeSphericalExcess($latLngsArray[0], $latLngsArray[$i], $latLngsArray[$i + 1], $signed); - } - - return $e * $r2; - } - - /** - * Clamps a latitude value to be within valid bounds. - * - * @param float $lat The latitude to clamp. - * @return float The clamped latitude value. - */ - public static function clampLatitude($lat) - { - return min(max($lat, -90), 90); - } - - /** - * Wraps a longitude value to be within valid bounds. - * - * @param float $lng The longitude to wrap. - * @return float The wrapped longitude value. - */ - public static function wrapLongitude($lng) - { - return fmod((fmod(($lng - -180), 360) + 360), 360) + -180; - } - - /** - * Computes the great circle distance (in radians) between two points. - * Uses the Haversine formula. - * - * @param LatLng $latLng1 The first LatLng point. - * @param LatLng $latLng2 The second LatLng point. - * @return float The distance in radians. - */ - protected static function _computeDistanceInRadiansBetween($latLng1, $latLng2) - { - $p1RadLat = deg2rad($latLng1->getLat()); - $p1RadLng = deg2rad($latLng1->getLng()); - $p2RadLat = deg2rad($latLng2->getLat()); - $p2RadLng = deg2rad($latLng2->getLng()); - return 2 * asin(sqrt(pow(sin(($p1RadLat - $p2RadLat) / 2), 2) + cos($p1RadLat) - * cos($p2RadLat) * pow(sin(($p1RadLng - $p2RadLng) / 2), 2))); - } - - /** - * Computes the spherical excess using L'Huilier's Theorem. - * - * @param LatLng $latLng1 The first vertex of the triangle. - * @param LatLng $latLng2 The second vertex of the triangle. - * @param LatLng $latLng3 The third vertex of the triangle. - * @param bool $signed Whether to return a signed value. - * @return float The spherical excess. - */ - protected static function _computeSphericalExcess($latLng1, $latLng2, $latLng3, $signed) - { - $latLngsArray = array($latLng1, $latLng2, $latLng3, $latLng1); - $distances = array(); - $sumOfDistances = 0; - - for ($i = 0; $i < 3; ++$i) { - $distances[$i] = self::_computeDistanceInRadiansBetween($latLngsArray[$i], $latLngsArray[$i + 1]); - $sumOfDistances += $distances[$i]; - } - - $semiPerimeter = $sumOfDistances / 2; - $tan = tan($semiPerimeter / 2); - - for ($i = 0; $i < 3; ++$i) { - $tan *= tan(($semiPerimeter - $distances[$i]) / 2); - } - - $sphericalExcess = 4 * atan(sqrt(abs($tan))); - - if (!$signed) { - return $sphericalExcess; - } - - // Negative or positive sign? - array_pop($latLngsArray); - - $v = array(); - - for ($i = 0; $i < 3; ++$i) { - $latLng = $latLngsArray[$i]; - $lat = deg2rad($latLng->getLat()); - $lng = deg2rad($latLng->getLng()); - - $v[$i] = array(); - $v[$i][0] = cos($lat) * cos($lng); - $v[$i][1] = cos($lat) * sin($lng); - $v[$i][2] = sin($lat); - } - - $sign = ($v[0][0] * $v[1][1] * $v[2][2] - + $v[1][0] * $v[2][1] * $v[0][2] - + $v[2][0] * $v[0][1] * $v[1][2] - - $v[0][0] * $v[2][1] * $v[1][2] - - $v[1][0] * $v[0][1] * $v[2][2] - - $v[2][0] * $v[1][1] * $v[0][2]) > 0 ? 1 : -1; - - return $sphericalExcess * $sign; - } -} diff --git a/src/Geometry/Triangle.php b/src/Geometry/Triangle.php deleted file mode 100644 index bf9c689b..00000000 --- a/src/Geometry/Triangle.php +++ /dev/null @@ -1,86 +0,0 @@ -a = $a; - $this->b = $b; - $this->c = $c; - - // Calculate the lengths of the sides of the triangle - $this->sa = $b->distanceFrom($c); - $this->sb = $c->distanceFrom($a); - $this->sc = $a->distanceFrom($b); - } - - /** - * Calculate the area of the triangle using Heron's formula. - * - * @return float The area of the triangle. - */ - public function getArea() { - $z = ($this->sa + $this->sb + $this->sc) / 2; - return sqrt($z * ($z - $this->sa) * ($z - $this->sb) * ($z - $this->sc)); - } -} diff --git a/src/Matrix/Matrix.php b/src/Matrix/Matrix.php deleted file mode 100644 index 21bc53a2..00000000 --- a/src/Matrix/Matrix.php +++ /dev/null @@ -1,139 +0,0 @@ -validateSameDimensions($a, $b); - - $result = []; - foreach ($a as $i => $row) { - foreach ($row as $j => $value) { - $result[$i][$j] = $value + $b[$i][$j]; - } - } - return $result; - } - - /** - * Subtracts matrix B from matrix A element-wise. - * - * @param array $a First matrix. - * @param array $b Second matrix. - * @return array Resulting matrix after subtraction. - * @throws MatrixCalculationException If matrix dimensions do not match. - */ - public function subtract($a, $b) - { - $this->validateSameDimensions($a, $b); - - $result = []; - foreach ($a as $i => $row) { - foreach ($row as $j => $value) { - $result[$i][$j] = $value - $b[$i][$j]; - } - } - return $result; - } - - /** - * Multiplies two matrices using standard matrix multiplication. - * - * @param array $a First matrix. - * @param array $b Second matrix. - * @return array Resulting matrix after multiplication. - * @throws MatrixCalculationException If matrix dimensions are not compatible for multiplication. - */ - public function multiply($a, $b) - { - $rowsA = count($a); - $colsA = count($a[0]); - $rowsB = count($b); - $colsB = count($b[0]); - - if ($colsA !== $rowsB) { - throw new MatrixCalculationException("Matrix dimensions are not compatible for multiplication."); - } - - $result = []; - for ($i = 0; $i < $rowsA; $i++) { - for ($j = 0; $j < $colsB; $j++) { - $sum = 0; - for ($k = 0; $k < $colsA; $k++) { - $sum += $a[$i][$k] * $b[$k][$j]; - } - $result[$i][$j] = $sum; - } - } - - return $result; - } - - /** - * Divides matrix A by matrix B element-wise. - * - * @param array $a First matrix. - * @param array $b Second matrix. - * @return array Resulting matrix after element-wise division. - * @throws MatrixCalculationException If dimensions do not match or division by zero occurs. - */ - public function divide($a, $b) - { - $this->validateSameDimensions($a, $b); - - $result = []; - foreach ($a as $i => $row) { - foreach ($row as $j => $value) { - if ($b[$i][$j] == 0) { - throw new MatrixCalculationException("Division by zero at position [$i][$j]."); - } - $result[$i][$j] = $value / $b[$i][$j]; - } - } - return $result; - } - - /** - * Validates that two matrices have the same dimensions. - * - * @param array $a First matrix. - * @param array $b Second matrix. - * @throws MatrixCalculationException If dimensions do not match. - */ - private function validateSameDimensions(array $a, array $b): void - { - if (count($a) !== count($b)) { - throw new MatrixCalculationException("Matrices must have the same number of rows."); - } - - foreach ($a as $i => $row) { - if (!isset($b[$i]) || count($row) !== count($b[$i])) { - throw new MatrixCalculationException("Matrices must have the same number of columns in each row (row $i)."); - } - } - } -}