From bda75095a8c55e7f20108d87b1e755119f997186 Mon Sep 17 00:00:00 2001 From: Kenneth Yang <82800265+kjy5@users.noreply.github.com> Date: Thu, 21 Mar 2024 13:33:48 -0700 Subject: [PATCH] Overload vector4 math (#15) * Add math functions to Vector4 * Version bump --- .github/workflows/build.yml | 3 +++ src/vbl_aquarium/__about__.py | 2 +- src/vbl_aquarium/models/unity.py | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9308e1b..f05b601 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,9 +1,11 @@ name: Build on: + pull_request: push: branches: - main + merge_group: jobs: build: @@ -27,6 +29,7 @@ jobs: run: python src/vbl_aquarium/build.py - name: 📤 PR changes + if: github.ref == 'refs/heads/main' uses: peter-evans/create-pull-request@v6 with: token: ${{ secrets.PR_TOKEN }} diff --git a/src/vbl_aquarium/__about__.py b/src/vbl_aquarium/__about__.py index a73339b..00ec2dc 100644 --- a/src/vbl_aquarium/__about__.py +++ b/src/vbl_aquarium/__about__.py @@ -1 +1 @@ -__version__ = "0.0.8" +__version__ = "0.0.9" diff --git a/src/vbl_aquarium/models/unity.py b/src/vbl_aquarium/models/unity.py index e8d5cb7..d0ed4e1 100644 --- a/src/vbl_aquarium/models/unity.py +++ b/src/vbl_aquarium/models/unity.py @@ -27,3 +27,27 @@ class Vector4(BaseModel): y: float = 0.0 z: float = 0.0 w: float = 0.0 + + def __add__(self, other): + if isinstance(other, Vector4): + return Vector4(x=self.x + other.x, y=self.y + other.y, z=self.z + other.z, w=self.w + other.w) + error = f"unsupported operand type(s) for +: 'Vector4' and '{type(other)}'" + raise TypeError(error) + + def __sub__(self, other): + if isinstance(other, Vector4): + return Vector4(x=self.x - other.x, y=self.y - other.y, z=self.z - other.z, w=self.w - other.w) + error = f"unsupported operand type(s) for -: 'Vector4' and '{type(other)}'" + raise TypeError(error) + + def __mul__(self, other): + if isinstance(other, (int, float)): + return Vector4(x=self.x * other, y=self.y * other, z=self.z * other, w=self.w * other) + error = f"unsupported operand type(s) for *: 'Vector4' and '{type(other)}'" + raise TypeError(error) + + def __truediv__(self, other): + if isinstance(other, (int, float)): + return Vector4(x=self.x / other, y=self.y / other, z=self.z / other, w=self.w / other) + error = f"unsupported operand type(s) for /: 'Vector4' and '{type(other)}'" + raise TypeError(error)