Skip to content

Commit 9d2e027

Browse files
committed
fixed divide by zero error
1 parent 3535a47 commit 9d2e027

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/SpatialHashTable/SupportedGeometries/Point.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ public static function dist(Point &$A, Point &$B) {
100100
}
101101

102102
public static function distToLine(Point &$A, Point &$B, Point &$P) {
103-
return abs(self::cross($A, $B, $P))/self::dist($A, $B);
103+
$distAB = self::dist($A, $B);
104+
if ($distAB == 0) return self::dist($A, $P);
105+
return abs(self::cross($A, $B, $P))/$distAB;
104106
}
105107

106108
public static function distToSegment(Point &$A, Point &$B, Point &$P) {

test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
$b->addElement(new Edge(new Point(1.25, 1.5), new Point( 0.5, 0.75), 200));
5454
$b->addElement(new Edge(new Point(1.75, 1.75), new Point( 1.75, 1.25), 300));
5555
$b->addElement(new Point(0.5, 0.5, 400));
56+
$b->addElement(new Edge(new Point(0.5, 0.5), new Point(0.5, 0.5), 500));
5657

5758
// inverted tricky one
5859
$b->outputHashTable();

0 commit comments

Comments
 (0)