From c8319fd10f454b93c6aa47ace48de625a01019eb Mon Sep 17 00:00:00 2001 From: Xkonti Date: Mon, 2 Oct 2023 13:23:12 -0400 Subject: [PATCH 1/7] Add contribution guide and code of conduct --- .gitignore | 2 + CODE_OF_CONDUCT.md | 122 ++++++++++++++++++++++++++++++++++++++ CONTRIBUTING.md | 145 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 269 insertions(+) create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md diff --git a/.gitignore b/.gitignore index 2e7699b..095f3a4 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,8 @@ !go.mod !README.md +!CODE_OF_CONDUCT.md +!CONTRIBUTING.md !LICENSE # Configuration files diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..e842d32 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,122 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +xkonti@xkonti.tech. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..99fa428 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,145 @@ + +# Contributing to govec + +First off, thanks for taking the time to contribute! ❤️ + +All types of contributions are encouraged and valued. See the [Table of Contents](#table-of-contents) for different ways to help and details about how this project handles them. Please make sure to read the relevant section before making your contribution. It will make it a lot easier for us maintainers and smooth out the experience for all involved. The community looks forward to your contributions. 🎉 + +> And if you like the project, but just don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about: +> - Star the project +> - Tweet about it +> - Refer this project in your project's readme +> - Mention the project at local meetups and tell your friends/colleagues + + +## Table of Contents + +- [Code of Conduct](#code-of-conduct) +- [I Have a Question](#i-have-a-question) +- [I Want To Contribute](#i-want-to-contribute) +- [Reporting Bugs](#reporting-bugs) +- [Suggesting Enhancements](#suggesting-enhancements) +- [Your First Code Contribution](#your-first-code-contribution) +- [Improving The Documentation](#improving-the-documentation) +- [Styleguides](#styleguides) +- [Commit Messages](#commit-messages) +- [Join The Project Team](#join-the-project-team) + + +## Code of Conduct + +This project and everyone participating in it is governed by the +[govec Code of Conduct](https://github.com/Xkonti/govec/blob/main/CODE_OF_CONDUCT.md). +By participating, you are expected to uphold this code. Please report unacceptable behavior +to . + + +## I Have a Question + +> If you want to ask a question, we assume that you have read the available [Documentation](https://github.com/Xkonti/govec/blob/main/README.md). + +Before you ask a question, it is best to search for existing [Issues](https://github.com/Xkonti/govec/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first. + +If you then still feel the need to ask a question and need clarification, we recommend the following: + +- Open an [Issue](https://github.com/Xkonti/govec/issues/new). +- Provide as much context as you can about what you're running into. +- Provide project and platform versions (nodejs, npm, etc), depending on what seems relevant. + +We will then take care of the issue as soon as possible. + +## I Want To Contribute + +> ### Legal Notice +> When contributing to this project, you must agree that you have authored 100% of the content, that you have the necessary rights to the content and that the content you contribute may be provided under the project license. + +### Reporting Bugs + + +#### Before Submitting a Bug Report + +A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible. + +- Make sure that you are using the latest version. +- Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions (Make sure that you have read the [documentation](https://github.com/Xkonti/govec/blob/main/README.md). If you are looking for support, you might want to check [this section](#i-have-a-question)). +- To see if other users have experienced (and potentially already solved) the same issue you are having, check if there is not already a bug report existing for your bug or error in the [bug tracker](https://github.com/Xkonti/govec/issues?q=label%3Abug). +- Also make sure to search the internet (including Stack Overflow) to see if users outside the GitHub community have discussed the issue. +- Collect information about the bug: +- Stack trace (Traceback) +- OS, Platform and Version (Windows, Linux, macOS, x86, ARM) +- Version of the interpreter, compiler, SDK, runtime environment, package manager, depending on what seems relevant. +- Possibly your input and the output +- Can you reliably reproduce the issue? And can you also reproduce it with older versions? + + +#### How Do I Submit a Good Bug Report? + +> You must never report security related issues, vulnerabilities or bugs including sensitive information to the issue tracker, or elsewhere in public. Instead sensitive bugs must be sent by email to xkonti@xkonti.tech + +We use GitHub issues to track bugs and errors. If you run into an issue with the project: + +- Open an [Issue](https://github.com/Xkonti/govec/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.) +- Explain the behavior you would expect and the actual behavior. +- Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case. +- Provide the information you collected in the previous section. + +Once it's filed: + +- The project team will label the issue accordingly. +- A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will ask you for those steps and mark the issue as `needs-repro`. Bugs with the `needs-repro` tag will not be addressed until they are reproduced. +- If the team is able to reproduce the issue, it will be marked `needs-fix`, as well as possibly other tags (such as `critical`), and the issue will be left to be [implemented by someone](#your-first-code-contribution). + + + + +### Suggesting Enhancements + +This section guides you through submitting an enhancement suggestion for govec, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions. + + +#### Before Submitting an Enhancement + +- Make sure that you are using the latest version. +- Read the [documentation](https://github.com/Xkonti/govec/blob/main/README.md) carefully and find out if the functionality is already covered, maybe by an individual configuration. +- Perform a [search](https://github.com/Xkonti/govec/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one. +- Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library. + + +#### How Do I Submit a Good Enhancement Suggestion? + +Enhancement suggestions are tracked as [GitHub issues](https://github.com/Xkonti/govec/issues). + +- Use a **clear and descriptive title** for the issue to identify the suggestion. +- Provide a **step-by-step description of the suggested enhancement** in as many details as possible. +- **Describe the current behavior** and **explain which behavior you expected to see instead** and why. At this point you can also tell which alternatives do not work for you. +- You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to. You can use [this tool](https://www.cockos.com/licecap/) to record GIFs on macOS and Windows, and [this tool](https://github.com/colinkeenan/silentcast) or [this tool](https://github.com/GNOME/byzanz) on Linux. +- **Explain why this enhancement would be useful** to most govec users. You may also want to point out the other projects that solved it better and which could serve as inspiration. + +### Your First Code Contribution + +Make sure you have Go 1.21 or higher installed. See the [installation guide](https://golang.org/doc/install) for details. + +1. Fork the project. +2. Clone the forked project to your local machine. +3. Create a new branch for your feature or bug fix. +4. Make changes to the code base. +5. Run `go test` to make sure all tests pass. +6. Commit your changes. +7. Push your branch to your forked repository. +8. Open a pull request in this repository. Make sure to include a description of your changes and a reference to the issue that you're fixing. +9. Wait for the pull request to be reviewed and merged. + +### Improving The Documentation + +Improvement to the documentation are always welcome. Common documentation related actions: +- If you see a **typo** or some other small issue, please feel free to either create an issue or open a pull request with a fix. +- Feel free to submit PRs that add **useful comments** to source code. Especially descriptions of functions, attributes, return values and why a function does something in a certain way. +- If you feel that some documentation is **missing** or **wrong**, please create an issue to discuss it further. +- **Tests** can be considered as a form of documentation. Adding/improving tests is also a great way to contribute to the project. + +### Focus Areas + +This library focuses on performance, therefore **performance** has higher priority than code readability. If you want to contribute to this project, please keep this in mind. + +## Attribution +This guide is based on the **contributing-gen**. [Make your own](https://github.com/bttger/contributing-gen)! \ No newline at end of file From b47c7df1f07b259e8bc14d657eaf07da459831fd Mon Sep 17 00:00:00 2001 From: Xkonti Date: Mon, 2 Oct 2023 13:23:33 -0400 Subject: [PATCH 2/7] Add conversion methods for `int` --- vec2f.go | 4 ++++ vec2i.go | 4 ++++ vec3f.go | 4 ++++ vec3i.go | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/vec2f.go b/vec2f.go index dc9b76d..af83cbd 100644 --- a/vec2f.go +++ b/vec2f.go @@ -30,3 +30,7 @@ func (v V2F[T]) ToV2I16() V2I[int16] { func (v V2F[T]) ToV2I8() V2I[int8] { return V2I[int8]{X: int8(v.X), Y: int8(v.Y)} } + +func (v V2F[T]) ToV2I() V2I[int] { + return V2I[int]{X: int(v.X), Y: int(v.Y)} +} diff --git a/vec2i.go b/vec2i.go index 2bf049c..594373a 100644 --- a/vec2i.go +++ b/vec2i.go @@ -30,3 +30,7 @@ func (v V2I[T]) ToV2I16() V2I[int16] { func (v V2I[T]) ToV2I8() V2I[int8] { return V2I[int8]{X: int8(v.X), Y: int8(v.Y)} } + +func (v V2I[T]) ToV2I() V2I[int] { + return V2I[int]{X: int(v.X), Y: int(v.Y)} +} diff --git a/vec3f.go b/vec3f.go index b175ae8..d260a5a 100644 --- a/vec3f.go +++ b/vec3f.go @@ -31,3 +31,7 @@ func (v V3F[T]) ToV3I16() V3I[int16] { func (v V3F[T]) ToV3I8() V3I[int8] { return V3I[int8]{X: int8(v.X), Y: int8(v.Y), Z: int8(v.Z)} } + +func (v V3F[T]) ToV3I() V3I[int] { + return V3I[int]{X: int(v.X), Y: int(v.Y), Z: int(v.Z)} +} diff --git a/vec3i.go b/vec3i.go index 2a4c1e2..74f4d56 100644 --- a/vec3i.go +++ b/vec3i.go @@ -31,3 +31,7 @@ func (v V3I[T]) ToV3I16() V3I[int16] { func (v V3I[T]) ToV3I8() V3I[int8] { return V3I[int8]{X: int8(v.X), Y: int8(v.Y), Z: int8(v.Z)} } + +func (v V3I[T]) ToV3I() V3I[int] { + return V3I[int]{X: int(v.X), Y: int(v.Y), Z: int(v.Z)} +} From 18dc10d996ede6256fdde2c9ac182a3dd4b94864 Mon Sep 17 00:00:00 2001 From: Xkonti Date: Mon, 2 Oct 2023 13:23:47 -0400 Subject: [PATCH 3/7] Add `Swizzle...` operations --- swizzle.go | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 swizzle.go diff --git a/swizzle.go b/swizzle.go new file mode 100644 index 0000000..8cb88a1 --- /dev/null +++ b/swizzle.go @@ -0,0 +1,129 @@ +package govec + +// V2F + +// SwizzleYZ returns a new V2F with the X and Y components swapped. +func (v V2F[T]) SwizzleYZ() V2F[T] { + return V2F[T]{X: v.Y, Y: v.X} +} + +// SwizzleInPlaceYZ swaps the X and Y components of the V2F in place. +func (v V2F[T]) SwizzleInPlaceYZ() { + v.X, v.Y = v.Y, v.X +} + +// V3F + +// SwizzleXZY returns a new V3F with components swapped: X->X Z->Y Y->Z +func (v V3F[T]) SwizzleXZY() V3F[T] { + return V3F[T]{X: v.X, Y: v.Z, Z: v.Y} +} + +// SwizzleInPlaceXZY swaps components in place: X->X Z->Y Y->Z +func (v V3F[T]) SwizzleInPlaceXZY() { + v.Y, v.Z = v.Z, v.Y +} + +// SwizzleYXZ returns a new V3F with components swapped: Y->X X->Y Z->Z +func (v V3F[T]) SwizzleYXZ() V3F[T] { + return V3F[T]{X: v.Y, Y: v.X, Z: v.Z} +} + +// SwizzleInPlaceYXZ swaps components in place: Y->X X->Y Z->Z +func (v V3F[T]) SwizzleInPlaceYXZ() { + v.X, v.Y = v.Y, v.X +} + +// SwizzleYZX returns a new V3F with components swapped: Y->X Z->Y X->Z +func (v V3F[T]) SwizzleYZX() V3F[T] { + return V3F[T]{X: v.Y, Y: v.Z, Z: v.X} +} + +// SwizzleInPlaceYZX swaps components in place: Y->X Z->Y X->Z +func (v V3F[T]) SwizzleInPlaceYZX() { + v.X, v.Y, v.Z = v.Y, v.Z, v.X +} + +// SwizzleZXY returns a new V3F with components swapped: Z->X X->Y Y->Z +func (v V3F[T]) SwizzleZXY() V3F[T] { + return V3F[T]{X: v.Z, Y: v.X, Z: v.Y} +} + +// SwizzleInPlaceZXY swaps components in place: Z->X X->Y Y->Z +func (v V3F[T]) SwizzleInPlaceZXY() { + v.X, v.Y, v.Z = v.Z, v.X, v.Y +} + +// SwizzleZYX returns a new V3F with components swapped: Z->X Y->Y X->Z +func (v V3F[T]) SwizzleZYX() V3F[T] { + return V3F[T]{X: v.Z, Y: v.Y, Z: v.X} +} + +// SwizzleInPlaceZYX swaps components in place: Z->X Y->Y X->Z +func (v V3F[T]) SwizzleInPlaceZYX() { + v.X, v.Z = v.Z, v.X +} + +// V2I + +// SwizzleYZ returns a new V2I with the X and Y components swapped. +func (v V2I[T]) SwizzleYZ() V2I[T] { + return V2I[T]{X: v.Y, Y: v.X} +} + +// SwizzleInPlaceYZ swaps the X and Y components of the V2I in place. +func (v V2I[T]) SwizzleInPlaceYZ() { + v.X, v.Y = v.Y, v.X +} + +// V3I + +// SwizzleXZY returns a new V3I with components swapped: X->X Z->Y Y->Z +func (v V3I[T]) SwizzleXZY() V3I[T] { + return V3I[T]{X: v.X, Y: v.Z, Z: v.Y} +} + +// SwizzleInPlaceXZY swaps components in place: X->X Z->Y Y->Z +func (v V3I[T]) SwizzleInPlaceXZY() { + v.Y, v.Z = v.Z, v.Y +} + +// SwizzleYXZ returns a new V3I with components swapped: Y->X X->Y Z->Z +func (v V3I[T]) SwizzleYXZ() V3I[T] { + return V3I[T]{X: v.Y, Y: v.X, Z: v.Z} +} + +// SwizzleInPlaceYXZ swaps components in place: Y->X X->Y Z->Z +func (v V3I[T]) SwizzleInPlaceYXZ() { + v.X, v.Y = v.Y, v.X +} + +// SwizzleYZX returns a new V3I with components swapped: Y->X Z->Y X->Z +func (v V3I[T]) SwizzleYZX() V3I[T] { + return V3I[T]{X: v.Y, Y: v.Z, Z: v.X} +} + +// SwizzleInPlaceYZX swaps components in place: Y->X Z->Y X->Z +func (v V3I[T]) SwizzleInPlaceYZX() { + v.X, v.Y, v.Z = v.Y, v.Z, v.X +} + +// SwizzleZXY returns a new V3I with components swapped: Z->X X->Y Y->Z +func (v V3I[T]) SwizzleZXY() V3I[T] { + return V3I[T]{X: v.Z, Y: v.X, Z: v.Y} +} + +// SwizzleInPlaceZXY swaps components in place: Z->X X->Y Y->Z +func (v V3I[T]) SwizzleInPlaceZXY() { + v.X, v.Y, v.Z = v.Z, v.X, v.Y +} + +// SwizzleZYX returns a new V3I with components swapped: Z->X Y->Y X->Z +func (v V3I[T]) SwizzleZYX() V3I[T] { + return V3I[T]{X: v.Z, Y: v.Y, Z: v.X} +} + +// SwizzleInPlaceZYX swaps components in place: Z->X Y->Y X->Z +func (v V3I[T]) SwizzleInPlaceZYX() { + v.X, v.Z = v.Z, v.X +} From 57a8c5649e36abb62e7cb92e5ffc2d43114e3358 Mon Sep 17 00:00:00 2001 From: Xkonti Date: Mon, 2 Oct 2023 13:47:09 -0400 Subject: [PATCH 4/7] Add `Add` operation descriptions --- add.go | 16 ++++++++++++++++ addScalar.go | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/add.go b/add.go index f7d2cac..664a94a 100644 --- a/add.go +++ b/add.go @@ -2,19 +2,23 @@ package govec // V2F +// Add returns a new V2F[T] with the sum of v and v2. func (v V2F[T]) Add(v2 V2F[T]) V2F[T] { return V2F[T]{X: v.X + v2.X, Y: v.Y + v2.Y} } +// AddInPlace modifies v by adding v2 to it. func (v *V2F[T]) AddInPlace(v2 V2F[T]) { v.X += v2.X v.Y += v2.Y } +// AddComp returns a new V2F[T] with the sum of v and the components x and y. func (v V2F[T]) AddComp(x T, y T) V2F[T] { return V2F[T]{X: v.X + x, Y: v.Y + y} } +// AddCompInPlace modifies v by adding the components x and y to it. func (v *V2F[T]) AddCompInPlace(x T, y T) { v.X += x v.Y += y @@ -22,20 +26,24 @@ func (v *V2F[T]) AddCompInPlace(x T, y T) { // V3F +// Add returns a new V3F[T] with the sum of v and v2. func (v V3F[T]) Add(v2 V3F[T]) V3F[T] { return V3F[T]{X: v.X + v2.X, Y: v.Y + v2.Y, Z: v.Z + v2.Z} } +// AddInPlace modifies v by adding v2 to it. func (v *V3F[T]) AddInPlace(v2 V3F[T]) { v.X += v2.X v.Y += v2.Y v.Z += v2.Z } +// AddComp returns a new V3F[T] with the sum of v and the components x, y and z. func (v V3F[T]) AddComp(x T, y T, z T) V3F[T] { return V3F[T]{X: v.X + x, Y: v.Y + y, Z: v.Z + z} } +// AddCompInPlace modifies v by adding the components x, y and z to it. func (v *V3F[T]) AddCompInPlace(x T, y T, z T) { v.X += x v.Y += y @@ -44,19 +52,23 @@ func (v *V3F[T]) AddCompInPlace(x T, y T, z T) { // V2I +// Add returns a new V2I[T] with the sum of v and v2. func (v V2I[T]) Add(v2 V2I[T]) V2I[T] { return V2I[T]{X: v.X + v2.X, Y: v.Y + v2.Y} } +// AddInPlace modifies v by adding v2 to it. func (v *V2I[T]) AddInPlace(v2 V2I[T]) { v.X += v2.X v.Y += v2.Y } +// AddComp returns a new V2I[T] with the sum of v and the components x and y. func (v V2I[T]) AddComp(x T, y T) V2I[T] { return V2I[T]{X: v.X + x, Y: v.Y + y} } +// AddCompInPlace modifies v by adding the components x and y to it. func (v *V2I[T]) AddCompInPlace(x T, y T) { v.X += x v.Y += y @@ -64,20 +76,24 @@ func (v *V2I[T]) AddCompInPlace(x T, y T) { // V3I +// Add returns a new V3I[T] with the sum of v and v2. func (v V3I[T]) Add(v2 V3I[T]) V3I[T] { return V3I[T]{X: v.X + v2.X, Y: v.Y + v2.Y, Z: v.Z + v2.Z} } +// AddInPlace modifies v by adding v2 to it. func (v *V3I[T]) AddInPlace(v2 V3I[T]) { v.X += v2.X v.Y += v2.Y v.Z += v2.Z } +// AddComp returns a new V3I[T] with the sum of v and the components x, y and z. func (v V3I[T]) AddComp(x T, y T, z T) V3I[T] { return V3I[T]{X: v.X + x, Y: v.Y + y, Z: v.Z + z} } +// AddCompInPlace modifies v by adding the components x, y and z to it. func (v *V3I[T]) AddCompInPlace(x T, y T, z T) { v.X += x v.Y += y diff --git a/addScalar.go b/addScalar.go index fa8f870..40b30d9 100644 --- a/addScalar.go +++ b/addScalar.go @@ -2,10 +2,12 @@ package govec // V2F +// AddScalar returns a new V2F[T] with the scalar value added to each component. func (v V2F[T]) AddScalar(scalar T) V2F[T] { return V2F[T]{X: v.X + scalar, Y: v.Y + scalar} } +// AddScalarInPlace modifies v by adding the scalar value to each component. func (v *V2F[T]) AddScalarInPlace(scalar T) { v.X += scalar v.Y += scalar @@ -13,10 +15,12 @@ func (v *V2F[T]) AddScalarInPlace(scalar T) { // V3F +// AddScalar returns a new V3F[T] with the scalar value added to each component. func (v V3F[T]) AddScalar(scalar T) V3F[T] { return V3F[T]{X: v.X + scalar, Y: v.Y + scalar, Z: v.Z + scalar} } +// AddScalarInPlace modifies v by adding the scalar value to each component. func (v *V3F[T]) AddScalarInPlace(scalar T) { v.X += scalar v.Y += scalar @@ -25,10 +29,12 @@ func (v *V3F[T]) AddScalarInPlace(scalar T) { // V2I +// AddScalar returns a new V2I[T] with the scalar value added to each component. func (v V2I[T]) AddScalar(scalar T) V2I[T] { return V2I[T]{X: v.X + scalar, Y: v.Y + scalar} } +// AddScalarInPlace modifies v by adding the scalar value to each component. func (v *V2I[T]) AddScalarInPlace(scalar T) { v.X += scalar v.Y += scalar @@ -36,10 +42,12 @@ func (v *V2I[T]) AddScalarInPlace(scalar T) { // V3I +// AddScalar returns a new V3I[T] with the scalar value added to each component. func (v V3I[T]) AddScalar(scalar T) V3I[T] { return V3I[T]{X: v.X + scalar, Y: v.Y + scalar, Z: v.Z + scalar} } +// AddScalarInPlace modifies v by adding the scalar value to each component. func (v *V3I[T]) AddScalarInPlace(scalar T) { v.X += scalar v.Y += scalar From 65ebb2a9a18d373da837228765c6d6903f491994 Mon Sep 17 00:00:00 2001 From: Xkonti Date: Mon, 2 Oct 2023 13:47:15 -0400 Subject: [PATCH 5/7] Fix file name --- splt.go => split.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename splt.go => split.go (100%) diff --git a/splt.go b/split.go similarity index 100% rename from splt.go rename to split.go From eb7eba282f0e57fed7832e4743cc4f09ed583c69 Mon Sep 17 00:00:00 2001 From: Xkonti Date: Mon, 2 Oct 2023 13:47:52 -0400 Subject: [PATCH 6/7] Improve README.md Add usage section Add links to files per operation --- README.md | 224 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 172 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index bec266d..8336049 100644 --- a/README.md +++ b/README.md @@ -11,60 +11,180 @@ The library supports both 2D and 3D vectors in two formats: go get github.com/xkonti/govec ``` +## Usage + +### Vector types + +The library provides the following vector types: +- `V2F[T]` - 2D floating point vector, where `T` is the type of the vector components (`float32` or `float64`) +- `V2I[T]` - 2D integer vector, where `T` is the type of the vector components (`int`, `int8`, `int16`, `int32` or `int64`) +- `V3F[T]` - 3D floating point vector, where `T` is the type of the vector components (`float32` or `float64`) +- `V3I[T]` - 3D integer vector, where `T` is the type of the vector components (`int`, `int8`, `int16`, `int32` or `int64`) + +### Creating new vectors + +Vectors can be created in various ways: +- Constructing vectors manually: + ```go + vector1 := govec.V2F{1.0, 2.0}; + vector2 := govec.V3F{1.0, 2.0, 3.0}; + vector3 := govec.V2I{1, 2}; + vector4 := govec.V3I{1, 2, 3}; + ``` +- Using the [`New`](new.go) function, which takes the vector components as arguments: + ```go + vector1 := govec.NewV2F(1.0, 2.0); + vector2 := govec.NewV3F(1.0, 2.0, 3.0); + vector3 := govec.NewV2I(1, 2); + vector4 := govec.NewV3I(1, 2, 3); + ``` +- Using the [`Fill`](new.go) function, which assigns the same value to all components of a vector: + ```go + vector1 := govec.FillV2F(1.0); + vector2 := govec.FillV3F(1.0); + vector3 := govec.FillV2I(1); + vector4 := govec.FillV3I(1); + ``` +- Using the [`Zero`](new.go) function, which creates a vector with all components set to 0: + ```go + vector1 := govec.ZeroV2F(); + vector2 := govec.ZeroV3F(); + vector3 := govec.ZeroV2I(); + vector4 := govec.ZeroV3I(); + ``` +- Using the [`One`](new.go) function, which creates a vector with all components set to 1: + ```go + vector1 := govec.OneV2F(); + vector2 := govec.OneV3F(); + vector3 := govec.OneV2I(); + vector4 := govec.OneV3I(); + ``` +- Using [`...FromArray`](array.go) +- Using [`...FromSlice`](slice.go) + +### Conversions + +Vectors can be converted to other types using the `To...` functions: +```go +vector1 := govec.V3F{1.0, 2.0, 3.0}; // Creates a 3D floating point vector (float64) +vector2 := vector1.ToV3I16(); // converts to 3D vector with int16 components +``` + +2D vectors have the following conversion functions: +- `ToV2F64`, `ToV2F32`, `ToV2I64`, `ToV2I32`, `ToV2I16`, `ToV2I8`, `ToV2I` + +3D vectors have the following conversion functions: +- `ToV3F64`, `ToV3F32`, `ToV3I64`, `ToV3I32`, `ToV3I16`, `ToV3I8`, `ToV3I` + +You can remove a component from a vector using the [`Discard`](discard.go) function: +```go +v1 := govec.V3F{1.0, 2.0, 3.0}; // Creates a 3D floating point vector (float64) +v2 := vector1.DiscardY(); // Returns a 2D vector with the Y component removed (v1.X -> v2.X, v1.Z -> v2.Y) +``` + +You can add a component to a vector using the [`Extend`](extend.go) function: +```go +v1 := govec.V2F{1.0, 2.0}; // Creates a 2D floating point vector (float64) +v2 := v1.Extend(3.0); // Returns a 3D vector with the Z component added. +``` + +You can insert a component to a vector using the [`Insert`](insert.go) function: +```go +v1 := govec.V2F{1.0, 2.0}; // Creates a 2D floating point vector (float64) +v2 := v1.InsertY(3.0); // Returns a 3D vector with the new component: newY -> Y, oldY -> Z. +``` + +You can swap components in a vector using the [`Swizzle`](swizzle.go) function: +```go +v1 := govec.V3F{1.0, 2.0, 3.0}; // Creates a 3D floating point vector (float64) +v2 := v1.SwizzleZYX(); // Returns a 3D vector with the components swapped (v1.Z -> v2.X, v1.Y -> v2.Y, v1.X -> v2.Z) +v3 := v1.SwizzleYZX(); // Returns a 3D vector with the components swapped (v1.Y -> v2.X, v1.Z -> v2.Y, v1.X -> v2.Z) +``` + +### Common variants + +Most vector operations are available in various forms: +- Default form - returns a new vector, usually takes other vector as an argument +- `InPlace` form - modifies the original vector, usually takes other vector as an argument +- `Comp` form - returns a new vector, usually takes raw vector components as arguments instead of another vector +- `CompInPlace` form - modifies the original vector, usually takes raw vector components as arguments instead of another vector +- `Scalar` form - returns a new vector, takes a scalar value as an argument +- `ScalarInPlace` form - modifies the original vector, takes a scalar value as an argument + +For example, the [`Add`](add.go) operation is available as: +- [`Add`](add.go) - takes another vector as an argument and returns a new vector +- [`AddInPlace`](add.go) - takes another vector as an argument and modifies the original vector +- [`AddComp`](add.go) - takes raw vector components (x, y, z) as arguments and returns a new vector +- [`AddCompInPlace`](add.go) - takes raw vector components (x, y, z) as arguments and modifies the original vector +- [`AddScalar`](addScalar.go) - takes a scalar value as an argument and returns a new vector +- [`AddScalarInPlace`](addScalar.go) - takes a scalar value as an argument and modifies the original vector + +For some operations, the `Comp`, `InPlace` or `Scalar` variants are not available as it doesn't make sense to use them. + +### Return type exceptions + +Usually the return type of the operation is the same as the type of the vector on which the operation is performed. However, +in some cases the return type is different. Examples: +- `Len` operation always returns `float64` even if the vector is an integer vector. This is the only way to return an accurate + length of the vector. +- `Norm` operations always returns a `float64` vectors even if the original vector is an integer vector. Normalization + returns values between 0 and 1, which is not possible to represent in an integer vector. + ## Available Operations -| Operation | Float | Integer | Description | -|-----------------|-------|---------|----------------------------------------------------------------------------------------------------| -| **Creation** | | | | -| `...FromArray` | ✔️ | ✔️ | Initializes a vector from an array of components. | -| `...FromSlice` | ✔️ | ✔️ | Initializes a vector from a slice of components. | -| `Fill` | ✔️ | ✔️ | Creates a vector where all components are set to a specified value. | -| `New` | ✔️ | ✔️ | Creates a new vector using the provided components. | -| `One` | ✔️ | ✔️ | Generates a vector with all components set to 1. | -| `Zero` | ✔️ | ✔️ | Generates a vector with all components set to 0. | -| **Conversions** | | | | -| `Apply` | ✔️ | ✔️ | Applies a given function to each component in the vector. | -| `ApplyToArray` | ✔️ | ✔️ | Modifies an array by assigning values from the vector's corresponding components. | -| `ApplyToSlice` | ✔️ | ✔️ | Modifies a slice by assigning values from the vector's corresponding components. | -| `Discard` | ✔️ | ✔️ | Removes a component from the vector at a specified index. | -| `Extend` | ✔️ | ✔️ | Extends the dimensionality of a vector by adding additional components. | -| `Insert` | ✔️ | ✔️ | Inserts a value into the vector at a specified index. | -| `Split` | ✔️ | ✔️ | Splits a vector into its individual components. | -| `ToArray` | ✔️ | ✔️ | Converts the vector to a new array. | -| `ToSlice` | ✔️ | ✔️ | Converts the vector to a new slice. | -| `To...` | ✔️ | ✔️ | Transforms the vector into another type. | -| **Mathematics** | | | | -| `Abs` | ✔️ | ✔️ | Computes the absolute value of each component in the vector. | -| `Add` | ✔️ | ✔️ | Performs vector addition. | -| `AddScalar` | ✔️ | ✔️ | Adds a scalar value to each component of the vector. | -| `Ceil` | ✔️ | ✔️ | Rounds each component of the vector up to the nearest integer. | -| `ClampComp` | ✔️ | ✔️ | Clamps the components of this vector to the given range. | -| `ClampLen` | ✔️ | ✔️ | Scales the length of the vector so that it fits within provided range. | -| `Cross` | ✔️ | ✔️ | Computes the cross product of two vectors. (Not applicable for 2D vectors.) | -| `Div` | ✔️ | ✔️ | Performs vector division. | -| `DivScalar` | ✔️ | ✔️ | Divides each component of the vector by a scalar. | -| `Dot` | ✔️ | ✔️ | Computes the dot product of two vectors. | -| `Floor` | ✔️ | ✔️ | Rounds each component of the vector down to the nearest integer. | -| `Inv` | ✔️ | ✔️ | Computes the multiplicative inverse of each component in the vector. | -| `Len` | ✔️ | ✔️ | Calculates the length of the vector. Returns `float64` for integer vectors. | -| `LenSqr` | ✔️ | ✔️ | Calculates the squared length of the vector. Returns `float64` for integer vectors. | -| `Max` | ✔️ | ✔️ | Returns the maximum component values from two vectors. | -| `Min` | ✔️ | ✔️ | Returns the minimum component values from two vectors. | -| `Mod` | ✔️ | ✔️ | Computes the modulus of each component in the vector against another vector components. | -| `ModScalar` | ✔️ | ✔️ | Computes the modulus of each component in the vector against a single scalar. | -| `Mul` | ✔️ | ✔️ | Performs vector multiplication. | -| `MulScalar` | ✔️ | ✔️ | Multiplies each component of the vector by a scalar. | -| `Neg` | ✔️ | ✔️ | Negates each component of the vector. | -| `Norm` | ✔️ | ✔️ | Normalizes the vector. Returns a `float64` vector for integer vectors. | -| `Pow` | ✔️ | ✔️ | Raises each component of the vector to the power of the corresponding component in another vector. | -| `Pow2` | ✔️ | ✔️ | Squares each component of the vector. | -| `Pow3` | ✔️ | ✔️ | Cubes each component of the vector. | -| `PowN` | ✔️ | ✔️ | Raises each component of the vector to a specified integer power. | -| `PowNFloat` | ✔️ | ✔️ | Raises each component of the vector to a specified floating-point power. | -| `Round` | ✔️ | ✔️ | Rounds each component of the vector to the nearest integer. | -| `Sqrt` | ✔️ | ✔️ | Computes the square root of each component in the vector. | -| `Sub` | ✔️ | ✔️ | Performs vector subtraction. | -| `SubScalar` | ✔️ | ✔️ | Subtracts a scalar from each component of the vector. | +| Operation | Float | Integer | Description | +|-----------------------------|-------|---------|-------------------------------------------------------------------------------------------------------------| +| **Creation** | | | | +| [`...FromArray`](array.go) | ✔️ | ✔️ | Initializes a vector from an array of components. | +| [`...FromSlice`](slice.go) | ✔️ | ✔️ | Initializes a vector from a slice of components. | +| [`Fill`](new.go) | ✔️ | ✔️ | Creates a vector where all components are set to a specified value. | +| [`New`](new.go) | ✔️ | ✔️ | Creates a new vector using the provided components. | +| [`One`](new.go) | ✔️ | ✔️ | Generates a vector with all components set to 1. | +| [`Zero`](new.go) | ✔️ | ✔️ | Generates a vector with all components set to 0. | +| **Conversions** | | | | +| [`Apply`](apply.go) | ✔️ | ✔️ | Applies a given function to each component in the vector. | +| [`ApplyToArray`](array.go) | ✔️ | ✔️ | Modifies an array by assigning values from the vector's corresponding components. | +| [`ApplyToSlice`](slice.go) | ✔️ | ✔️ | Modifies a slice by assigning values from the vector's corresponding components. | +| [`Discard`](discard.go) | ✔️ | ✔️ | Removes a component from the vector at a specified index. | +| [`Extend`](extend.go) | ✔️ | ✔️ | Extends the dimensionality of a vector by adding additional components. | +| [`Insert`](insert.go) | ✔️ | ✔️ | Inserts a value into the vector at a specified index. | +| [`Split`](split.go) | ✔️ | ✔️ | Splits a vector into its individual components. | +| [`Swizzle...`](swizzle.go) | ✔️ | ✔️ | Swaps components of a vector using a selected pattern. | +| [`ToArray`](array.go) | ✔️ | ✔️ | Converts the vector to a new array. | +| [`ToSlice`](slice.go) | ✔️ | ✔️ | Converts the vector to a new slice. | +| `To...` | ✔️ | ✔️ | Transforms the vector into another type. [V2F](vec2f.go), [V3F](vec3f.go), [V2I](vec2i.go), [V3I](vec3i.go) | +| **Mathematics** | | | | +| [`Abs`](abs.go) | ✔️ | ✔️ | Computes the absolute value of each component in the vector. | +| [`Add`](add.go) | ✔️ | ✔️ | Performs vector addition. | +| [`AddScalar`](addScalar.go) | ✔️ | ✔️ | Adds a scalar value to each component of the vector. | +| [`Ceil`](ceil.go) | ✔️ | ✔️ | Rounds each component of the vector up to the nearest integer. | +| [`ClampComp`](clampComp.go) | ✔️ | ✔️ | Clamps the components of this vector to the given range. | +| [`ClampLen`](clampLen.go) | ✔️ | ✔️ | Scales the length of the vector so that it fits within provided range. | +| [`Cross`](cross.go) | ✔️ | ✔️ | Computes the cross product of two vectors. (Not applicable for 2D vectors.) | +| [`Div`](div.go) | ✔️ | ✔️ | Performs vector division. | +| [`DivScalar`](dicScalar.go) | ✔️ | ✔️ | Divides each component of the vector by a scalar. | +| [`Dot`](dot.go) | ✔️ | ✔️ | Computes the dot product of two vectors. | +| [`Floor`](floor.go) | ✔️ | ✔️ | Rounds each component of the vector down to the nearest integer. | +| [`Inv`](inv.go) | ✔️ | ✔️ | Computes the multiplicative inverse of each component in the vector. | +| [`Len`](len.go) | ✔️ | ✔️ | Calculates the length of the vector. Returns `float64` for integer vectors. | +| [`LenSqrt`](lenSqrt.go) | ✔️ | ✔️ | Calculates the squared length of the vector. Returns `float64` for integer vectors. | +| [`Max`](max.go) | ✔️ | ✔️ | Returns the maximum component values from two vectors. | +| [`Min`](min.go) | ✔️ | ✔️ | Returns the minimum component values from two vectors. | +| [`Mod`](mod.go) | ✔️ | ✔️ | Computes the modulus of each component in the vector against another vector components. | +| [`ModScalar`](modScalar.go) | ✔️ | ✔️ | Computes the modulus of each component in the vector against a single scalar. | +| [`Mul`](mul.go) | ✔️ | ✔️ | Performs vector multiplication. | +| [`MulScalar`](mulScalar.go) | ✔️ | ✔️ | Multiplies each component of the vector by a scalar. | +| [`Neg`](neg.go) | ✔️ | ✔️ | Negates each component of the vector. | +| [`Norm`](norm.go) | ✔️ | ✔️ | Normalizes the vector. Returns a `float64` vector for integer vectors. | +| [`Pow`](pow.go) | ✔️ | ✔️ | Raises each component of the vector to the power of the corresponding component in another vector. | +| [`Pow2`](pow2.go) | ✔️ | ✔️ | Squares each component of the vector. | +| [`Pow3`](pow3.go) | ✔️ | ✔️ | Cubes each component of the vector. | +| [`PowN`](powN.go) | ✔️ | ✔️ | Raises each component of the vector to a specified integer power. | +| [`PowNFloat`](powNFloat.go) | ✔️ | ✔️ | Raises each component of the vector to a specified floating-point power. | +| [`Round`](round.go) | ✔️ | ✔️ | Rounds each component of the vector to the nearest integer. | +| [`Sqrt`](sqrt.go) | ✔️ | ✔️ | Computes the square root of each component in the vector. | +| [`Sub`](sub.go) | ✔️ | ✔️ | Performs vector subtraction. | +| [`SubScalar`](subScalar.go) | ✔️ | ✔️ | Subtracts a scalar from each component of the vector. | ## Not implemented yet From d31b641084f1c9f9d136bc771bbd3244fb5a20af Mon Sep 17 00:00:00 2001 From: Xkonti Date: Mon, 2 Oct 2023 13:58:51 -0400 Subject: [PATCH 7/7] Redo intro --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8336049..52a65d8 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # GoVec -A library providing vector operations for Go. +A library providing vector operations for Go. It focuses on performance so that it can be used in games and other +demanding applications. -The library supports both 2D and 3D vectors in two formats: -- Floating Point Vectors: `V2F[T]` and `V3F[T]` -- Integer Vectors: `V2I[T]` and `V3I[T]` +[![Go Reference](https://pkg.go.dev/badge/github.com/xkonti/govec.svg)](https://pkg.go.dev/github.com/xkonti/govec) +[![Release](https://img.shields.io/github/v/release/xkonti/govec)](https://img.shields.io/github/v/release/xkonti/govec) +[![License](https://img.shields.io/github/license/xkonti/govec)](https://img.shields.io/github/license/xkonti/govec) +[![Go Report Card](https://goreportcard.com/badge/github.com/xkonti/govec)](https://goreportcard.com/report/github.com/xkonti/govec) ## Installation