Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facing and Axis to enum #91

Merged
merged 35 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
69e3791
Facing and Axis to enum
Dhaiven Nov 19, 2023
7886af0
Modif toString function
Dhaiven Nov 19, 2023
7f6b638
Remove useless doc
Dhaiven Nov 20, 2023
c7e15ad
remove outside functionality of this pr
Dhaiven Nov 20, 2023
a45f1f0
Ditch numeric values
Dhaiven Nov 20, 2023
ac69813
Remove ditch values
Dhaiven Nov 21, 2023
d5ad9ac
Fix incorrect opposite side
Dhaiven Nov 21, 2023
426223d
Merge branch 'enum' of https://github.com/Dhaiven/Math into enum
Dhaiven Nov 21, 2023
d5e44f7
Modifications
Dhaiven Nov 21, 2023
19d771c
Follow order of cases
Dhaiven Nov 21, 2023
bca40e8
Fix bug in rotate function
Dhaiven Nov 27, 2023
5d66326
Modification
Dhaiven Nov 27, 2023
0e132c9
Merge branch 'stable' into enum
dktapps Feb 28, 2024
1c1a45e
Updates
Dhaiven May 12, 2024
89f49e0
Merge branch 'enum' of https://github.com/Dhaiven/Math into enum
Dhaiven May 12, 2024
54d40bb
Fix bugs
Dhaiven May 12, 2024
7857d74
Fix cs
Dhaiven May 12, 2024
2395e80
Fix cs
Dhaiven May 12, 2024
64d0112
Replace static functions by object functions
Dhaiven Dec 2, 2024
4fe606a
Add facing to key for Generator
Dhaiven Dec 2, 2024
9016d06
Fix tests
Dhaiven Dec 2, 2024
4d1f0e4
Prefix phpstan return
Dhaiven Dec 2, 2024
0fdc693
Update documentation
Dhaiven Dec 2, 2024
c3a71f9
Remove @var to specific content of variable
Dhaiven Dec 2, 2024
8ccea24
Update AxisAlignedBB.php
dktapps Dec 2, 2024
9f734d4
smh
dktapps Dec 2, 2024
c1ccf1e
formatting tidy
dktapps Dec 2, 2024
c9ebd9f
Remove unrealated changes
Dhaiven Dec 2, 2024
adf4a5a
Cs
Dhaiven Dec 2, 2024
f76eca3
Remove unrelated documentation
Dhaiven Dec 3, 2024
e212ce7
Don't repeat error or condition
Dhaiven Dec 3, 2024
e3fe09a
Update Facing.php
dktapps Dec 3, 2024
6865929
Update Vector3.php
dktapps Dec 3, 2024
f25c608
github web editor try not to suck challenge IMPOSSIBLE
dktapps Dec 3, 2024
49e2fef
Update Facing.php
dktapps Dec 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions src/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,8 @@

namespace pocketmine\math;

final class Axis{
private function __construct(){
//NOOP
}

public const Y = 0;
public const Z = 1;
public const X = 2;

/**
* Returns a human-readable string representation of the given axis.
*/
public static function toString(int $axis) : string{
return match($axis){
Axis::Y => "y",
Axis::Z => "z",
Axis::X => "x",
default => throw new \InvalidArgumentException("Invalid axis $axis")
};
}
enum Axis{
case Y;
case Z;
case X;
}
62 changes: 24 additions & 38 deletions src/AxisAlignedBB.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,18 @@ public function offsetCopy(float $x, float $y, float $z) : AxisAlignedBB{
/**
* Offsets this AxisAlignedBB in the given direction by the specified distance.
*
* @param int $face one of the Facing::* constants
*
* @return $this
*/
public function offsetTowards(int $face, float $distance) : AxisAlignedBB{
[$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$face] ?? throw new \InvalidArgumentException("Invalid Facing $face");
public function offsetTowards(Facing $face, float $distance) : AxisAlignedBB{
[$offsetX, $offsetY, $offsetZ] = $face->offset();

return $this->offset($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance);
}

/**
* Returns an offset clone of this AxisAlignedBB.
*/
public function offsetTowardsCopy(int $face, float $distance) : AxisAlignedBB{
public function offsetTowardsCopy(Facing $face, float $distance) : AxisAlignedBB{
return (clone $this)->offsetTowards($face, $distance);
}

Expand Down Expand Up @@ -182,17 +180,15 @@ public function contractedCopy(float $x, float $y, float $z) : AxisAlignedBB{
* @param float $distance Negative values pull the face in, positive values push out.
*
* @return $this
* @throws \InvalidArgumentException
*/
public function extend(int $face, float $distance) : AxisAlignedBB{
public function extend(Facing $face, float $distance) : AxisAlignedBB{
match($face){
Facing::DOWN => $this->minY -= $distance,
Facing::UP => $this->maxY += $distance,
Facing::NORTH => $this->minZ -= $distance,
Facing::SOUTH => $this->maxZ += $distance,
Facing::WEST => $this->minX -= $distance,
Facing::EAST => $this->maxX += $distance,
default => throw new \InvalidArgumentException("Invalid face $face"),
};

return $this;
Expand All @@ -201,10 +197,8 @@ public function extend(int $face, float $distance) : AxisAlignedBB{
/**
* Returns an extended clone of this bounding box.
* @see AxisAlignedBB::extend()
*
* @throws \InvalidArgumentException
*/
public function extendedCopy(int $face, float $distance) : AxisAlignedBB{
public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{
return (clone $this)->extend($face, $distance);
}

Expand All @@ -215,32 +209,27 @@ public function extendedCopy(int $face, float $distance) : AxisAlignedBB{
* @param float $distance Positive values pull the face in, negative values push out.
*
* @return $this
* @throws \InvalidArgumentException
*/
public function trim(int $face, float $distance) : AxisAlignedBB{
public function trim(Facing $face, float $distance) : AxisAlignedBB{
return $this->extend($face, -$distance);
}

/**
* Returns a trimmed clone of this bounding box.
* @see AxisAlignedBB::trim()
*
* @throws \InvalidArgumentException
*/
public function trimmedCopy(int $face, float $distance) : AxisAlignedBB{
public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{
return $this->extendedCopy($face, -$distance);
}

/**
* Increases the dimension of the AABB along the given axis.
*
* @param int $axis one of the Axis::* constants
* @param float $distance Negative values reduce width, positive values increase width.
*
* @return $this
* @throws \InvalidArgumentException
*/
public function stretch(int $axis, float $distance) : AxisAlignedBB{
public function stretch(Axis $axis, float $distance) : AxisAlignedBB{
if($axis === Axis::Y){
$this->minY -= $distance;
$this->maxY += $distance;
Expand All @@ -250,19 +239,16 @@ public function stretch(int $axis, float $distance) : AxisAlignedBB{
}elseif($axis === Axis::X){
$this->minX -= $distance;
$this->maxX += $distance;
}else{
throw new \InvalidArgumentException("Invalid axis $axis");
}

return $this;
}

/**
* Returns a stretched copy of this bounding box.
* @see AxisAlignedBB::stretch()
*
* @throws \InvalidArgumentException
*/
public function stretchedCopy(int $axis, float $distance) : AxisAlignedBB{
public function stretchedCopy(Axis $axis, float $distance) : AxisAlignedBB{
return (clone $this)->stretch($axis, $distance);
}

Expand All @@ -271,19 +257,16 @@ public function stretchedCopy(int $axis, float $distance) : AxisAlignedBB{
* @see AxisAlignedBB::stretch()
*
* @return $this
* @throws \InvalidArgumentException
*/
public function squash(int $axis, float $distance) : AxisAlignedBB{
public function squash(Axis $axis, float $distance) : AxisAlignedBB{
return $this->stretch($axis, -$distance);
}

/**
* Returns a squashed copy of this bounding box.
* @see AxisAlignedBB::squash()
*
* @throws \InvalidArgumentException
*/
public function squashedCopy(int $axis, float $distance) : AxisAlignedBB{
public function squashedCopy(Axis $axis, float $distance) : AxisAlignedBB{
return $this->stretchedCopy($axis, -$distance);
}

Expand Down Expand Up @@ -465,20 +448,23 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu

$vector = null;
$distance = PHP_INT_MAX;
$face = -1;
/** @var Facing $face */
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
$face = null;

foreach([
Facing::WEST => $v1,
Facing::EAST => $v2,
Facing::DOWN => $v3,
Facing::UP => $v4,
Facing::NORTH => $v5,
Facing::SOUTH => $v6
] as $f => $v){
[Facing::WEST, $v1],
[Facing::EAST, $v2],
[Facing::DOWN, $v3],
[Facing::UP, $v4],
[Facing::NORTH, $v5],
[Facing::SOUTH, $v6]
] as $value){
$v = $value[1];
if($v !== null and ($d = $pos1->distanceSquared($v)) < $distance){
$vector = $v;
$distance = $d;
$face = $f;
/** @var Facing $face */
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
$face = $value[0];
}
}

Expand Down
Loading