From 929ed80b2fc7954db7afe9b8f874c7b3ec6b5209 Mon Sep 17 00:00:00 2001 From: Kenneth Yang Date: Thu, 21 Mar 2024 13:29:42 -0700 Subject: [PATCH] Add math functions to Vector4 --- .github/workflows/build.yml | 3 +++ src/vbl_aquarium/models/unity.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) 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/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)