Skip to content

Commit e7a2125

Browse files
authored
Merge pull request #92 from AkmalFairuz/stable
Using $x*$x is 2x faster than $x**2
2 parents 22ebf43 + 8a2272b commit e7a2125

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

src/Math.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function solveQuadratic(float $a, float $b, float $c) : array{
5858
if($a === 0.0){
5959
throw new \InvalidArgumentException("Coefficient a cannot be 0!");
6060
}
61-
$discriminant = $b ** 2 - 4 * $a * $c;
61+
$discriminant = $b * $b - 4 * $a * $c;
6262
if($discriminant > 0){ //2 real roots
6363
$sqrtDiscriminant = sqrt($discriminant);
6464
return [

src/Vector2.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public function distance(Vector2 $pos) : float{
9696
}
9797

9898
public function distanceSquared(Vector2 $pos) : float{
99-
return (($this->x - $pos->x) ** 2) + (($this->y - $pos->y) ** 2);
99+
$dx = $this->x - $pos->x;
100+
$dy = $this->y - $pos->y;
101+
return ($dx * $dx) + ($dy * $dy);
100102
}
101103

102104
public function length() : float{

src/Vector3.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ public function distance(Vector3 $pos) : float{
216216
}
217217

218218
public function distanceSquared(Vector3 $pos) : float{
219-
return (($this->x - $pos->x) ** 2) + (($this->y - $pos->y) ** 2) + (($this->z - $pos->z) ** 2);
219+
$dx = $this->x - $pos->x;
220+
$dy = $this->y - $pos->y;
221+
$dz = $this->z - $pos->z;
222+
return ($dx * $dx) + ($dy * $dy) + ($dz * $dz);
220223
}
221224

222225
public function maxPlainDistance(Vector3|Vector2|float $x, float $z = 0) : float{

0 commit comments

Comments
 (0)