initial commit
This commit is contained in:
commit
e8c3f9862a
193 files changed
+29795
No files matched your search
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Game;
|
||||
|
||||
final class Vector2
|
||||
{
|
||||
public function __construct(
|
||||
public float $x,
|
||||
public float $y,
|
||||
) {}
|
||||
|
||||
public static function zero(): self
|
||||
{
|
||||
return new self(0.0, 0.0);
|
||||
}
|
||||
|
||||
public function mulS(float $scalar): self
|
||||
{
|
||||
return new self($this->x * $scalar, $this->y * $scalar);
|
||||
}
|
||||
|
||||
public function mulV(self $v): self
|
||||
{
|
||||
return new self($this->x * $v->x, $this->y * $v->y);
|
||||
}
|
||||
|
||||
public function divS(float $scalar): self
|
||||
{
|
||||
return new self($this->x / $scalar, $this->y / $scalar);
|
||||
}
|
||||
|
||||
public function addS(float $scalar): self
|
||||
{
|
||||
return new self($this->x + $scalar, $this->y + $scalar);
|
||||
}
|
||||
|
||||
public function addV(self $v): self
|
||||
{
|
||||
return new self($this->x + $v->x, $this->y + $v->y);
|
||||
}
|
||||
|
||||
public function subS(float $scalar): self
|
||||
{
|
||||
return new self($this->x - $scalar, $this->y - $scalar);
|
||||
}
|
||||
|
||||
public function subV(self $v): self
|
||||
{
|
||||
return new self($this->x - $v->x, $this->y - $v->y);
|
||||
}
|
||||
|
||||
public function dot(self $v): float
|
||||
{
|
||||
return $this->x * $v->x + $this->y * $v->y;
|
||||
}
|
||||
|
||||
public function abs(): self
|
||||
{
|
||||
return new self(abs($this->x), abs($this->y));
|
||||
}
|
||||
|
||||
public function length(): float
|
||||
{
|
||||
return sqrt($this->dot($this));
|
||||
}
|
||||
|
||||
public function lengthSquared(): float
|
||||
{
|
||||
return $this->dot($this);
|
||||
}
|
||||
|
||||
public function normalize(): self
|
||||
{
|
||||
$length = $this->length();
|
||||
if ($length < 1.0e-9) {
|
||||
return new self(0.0, 0.0);
|
||||
}
|
||||
|
||||
return new self($this->x / $length, $this->y / $length);
|
||||
}
|
||||
|
||||
public function rotate(float $angle): self
|
||||
{
|
||||
$cos = cos($angle);
|
||||
$sin = sin($angle);
|
||||
|
||||
return new self($this->x * $cos - $this->y * $sin, $this->x * $sin + $this->y * $cos);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user