{"id":21411790,"url":"https://github.com/elicdavis/vector","last_synced_at":"2025-07-14T02:31:59.234Z","repository":{"id":92410675,"uuid":"212321915","full_name":"EliCDavis/vector","owner":"EliCDavis","description":"Immutable Generic Vector Math Library","archived":false,"fork":false,"pushed_at":"2024-11-16T03:46:41.000Z","size":17843,"stargazers_count":4,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-16T04:22:59.125Z","etag":null,"topics":["3d","immutable","math","vector","vector-math","vector2d","vector3d","vector4d"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/EliCDavis/vector","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EliCDavis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-02T11:27:01.000Z","updated_at":"2024-11-16T03:30:28.000Z","dependencies_parsed_at":"2023-12-13T01:54:09.356Z","dependency_job_id":"0f2fd146-89b7-4fa2-b951-8707fa70fb83","html_url":"https://github.com/EliCDavis/vector","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EliCDavis%2Fvector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EliCDavis%2Fvector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EliCDavis%2Fvector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EliCDavis%2Fvector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EliCDavis","download_url":"https://codeload.github.com/EliCDavis/vector/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225941813,"owners_count":17549070,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["3d","immutable","math","vector","vector-math","vector2d","vector3d","vector4d"],"created_at":"2024-11-22T17:47:34.448Z","updated_at":"2025-07-14T02:31:59.204Z","avatar_url":"https://github.com/EliCDavis.png","language":"Go","readme":"# Vector\n\n![Coverage](https://img.shields.io/badge/Coverage-92.9%25-brightgreen)\n[![Go Report Card](https://goreportcard.com/badge/github.com/EliCDavis/vector)](https://goreportcard.com/report/github.com/EliCDavis/vector)\n[![GoDoc](https://godoc.org/github.com/EliCDavis/vector?status.svg)](http://godoc.org/github.com/EliCDavis/vector)\n\nCollection of **generic, immutable** vector math functions I've written overtime for different hobby projects.\n\n## API\n\n| Function      | Vector2 | Vector3 | Vector4 | Description                                                                                                                              |\n| ------------- | ------- | ------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------- |\n| Abs           | ✅       | ✅       | ✅       | Returns a vector with each component's absolute value                                                                                    |\n| Add           | ✅       | ✅       | ✅       | Component Wise Addition                                                                                                                  |\n| Angle         | ✅       | ✅       |         | Returns the angle between two vectors                                                                                                    |\n| ToArr         | ✅       | ✅       | ✅       | Returns a slice containing the vector component data                                                                                     |\n| ToFixedArr    | ✅       | ✅       | ✅       | Returns a array containing the vector component data                                                                                     |\n| Ceil          | ✅       | ✅       | ✅       | Ceils each vectors component to the nearest integer                                                                                      |\n| Clamp         | ✅       | ✅       | ✅       | Clamps each component between two values                                                                                                 |\n| ContainsNaN   | ✅       | ✅       | ✅       | Returns true if any component of the vector is NaN                                                                                       |\n| Cross         |         | ✅       |         | Returns the cross product between two vectors                                                                                            |\n| Dot           | ✅       | ✅       | ✅       | Returns the dot product between two vectors                                                                                              |\n| DivByVector   | ✅       | ✅       | ✅       | Component wise division                                                                                                                  |\n| Flip          | ✅       | ✅       | ✅       | Scales the vector by -1                                                                                                                  |\n| FlipX         | ✅       | ✅       | ✅       | Returns a vector with the X component multiplied by -1                                                                                   |\n| FlipY         | ✅       | ✅       | ✅       | Returns a vector with the Y component multiplied by -1                                                                                   |\n| FlipZ         |         | ✅       | ✅       | Returns a vector with the Z component multiplied by -1                                                                                   |\n| FlipW         |         |         | ✅       | Returns a vector with the W component multiplied by -1                                                                                   |\n| Floor         | ✅       | ✅       | ✅       | Floors each vectors component                                                                                                            |\n| Format        | ✅       | ✅       | ✅       | Build a string with vector data                                                                                                          |\n| Length        | ✅       | ✅       | ✅       | Returns the length of the vector                                                                                                         |\n| LengthSquared | ✅       | ✅       | ✅       | Returns the squared length of the vector                                                                                                 |\n| Max           | ✅       | ✅       | ✅       | Returns a new vector where each component is the largest value between the two vectors                                                   |\n| MaxX          | ✅       | ✅       | ✅       | Returns the largest X component between the two vectors                                                                                  |\n| MaxY          | ✅       | ✅       | ✅       | Returns the largest Y component between the two vectors                                                                                  |\n| MaxZ          |         | ✅       | ✅       | Returns the largest Z component between the two vectors                                                                                  |\n| MaxW          |         |         | ✅       | Returns the largest W component between the two vectors                                                                                  |\n| MaxComponent  | ✅       | ✅       | ✅       | Returns the vectors largest component                                                                                                    |\n| Midpoint      | ✅       | ✅       | ✅       | Finds the mid point between two vectors                                                                                                  |\n| Min           | ✅       | ✅       | ✅       | Returns a new vector where each component is the smallest value between the two vectors                                                  |\n| MinX          | ✅       | ✅       | ✅       | Returns the smallest X component between the two vectors                                                                                 |\n| MinY          | ✅       | ✅       | ✅       | Returns the smallest Y component between the two vectors                                                                                 |\n| MinZ          |         | ✅       | ✅       | Returns the smallest Z component between the two vectors                                                                                 |\n| MinW          |         |         | ✅       | Returns the smallest W component between the two vectors                                                                                 |\n| MinComponent  | ✅       | ✅       | ✅       | Returns the vectors smallest component                                                                                                   |\n| MultByVector  | ✅       | ✅       | ✅       | component wise multiplication, also known as Hadamard product                                                                            |\n| Normalized    | ✅       | ✅       | ✅       | Returns the normalized vector                                                                                                            |\n| NearZero      | ✅       | ✅       | ✅       | Returns true if all of the components are near 0                                                                                         |\n| Round         | ✅       | ✅       | ✅       | Rounds each vectors component to the nearest integer                                                                                     |\n| Scale         | ✅       | ✅       | ✅       | Scales the vector by some constant                                                                                                       |\n| Sqrt          | ✅       | ✅       | ✅       | Returns a vector with each component's square root                                                                                       |\n| Sub           | ✅       | ✅       | ✅       | Component Wise Subtraction                                                                                                               |\n| Values        | ✅       | ✅       | ✅       | Returns all components of the vector                                                                                                     |\n| X             | ✅       | ✅       | ✅       | Returns the x component of the vector                                                                                                    |\n| Y             | ✅       | ✅       | ✅       | Returns the y component of the vector                                                                                                    |\n| Z             |         | ✅       | ✅       | Returns the z component of the vector                                                                                                    |\n| W             |         |         | ✅       | Returns the w component of the vector                                                                                                    |\n| XY            |         | ✅       | ✅       | Equivalent to vector2.New[T](v.x, v.y)                                                                                                   |\n| YZ            |         | ✅       | ✅       | Equivalent to vector2.New[T](v.y, v.z)                                                                                                   |\n| XZ            |         | ✅       | ✅       | Equivalent to vector2.New[T](v.x, v.z)                                                                                                   |\n| YX            | ✅       | ✅       | ✅       | Equivalent to vector2.New[T](v.y, v.x)                                                                                                   |\n| ZX            |         | ✅       | ✅       | Equivalent to vector2.New[T](v.z, v.x)                                                                                                   |\n| ZY            |         | ✅       | ✅       | Equivalent to vector2.New[T](v.z, v.y)                                                                                                   |\n| Lerp          | ✅       | ✅       | ✅       | Interpolates between two vectors by t.                                                                                                   |\n| LerpClamped   | ✅       | ✅       | ✅       | Interpolates between two vectors by t. T is clamped 0 to 1                                                                               |\n| Log           | ✅       | ✅       | ✅       | Returns the natural logarithm for each component                                                                                         |\n| Log2          | ✅       | ✅       | ✅       | Returns the binary logarithm for each component                                                                                          |\n| Log10         | ✅       | ✅       | ✅       | Returns the decimal logarithm for each component                                                                                         |\n| Exp           | ✅       | ✅       | ✅       | Returns e**x, the base-e exponential for each component                                                                                  |\n| Exp2          | ✅       | ✅       | ✅       | Returns 2**x, the base-2 exponential for each component                                                                                  |\n| Expm1         | ✅       | ✅       | ✅       | Returns e**x - 1, the base-e exponential for each component minus 1. It is more accurate than Exp(x) - 1 when the component is near zero |\n| Write         | ✅       | ✅       | ✅       | Write vector component data as binary to io.Writer                                                                                       |\n\n\n## Example\n\nBelow is an example on how to implement the different sign distance field functions in a generic fashion to work for both `int8`, `int16`, `int32` `int`, `int64`, `float32`, and `float64`.\n\nThe code below produces this output:\n\n![out.gif](./out.gif)\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"image\"\n\t\"image/color\"\n\t\"image/png\"\n\t\"math\"\n\t\"os\"\n\n\t\"github.com/EliCDavis/vector\"\n\t\"github.com/EliCDavis/vector/vector2\"\n\t\"github.com/EliCDavis/vector/vector3\"\n)\n\ntype Field[T vector.Number] func(v vector3.Vector[T]) float64\n\nfunc Sphere[T vector.Number](pos vector3.Vector[T], radius float64) Field[T] {\n\treturn func(v vector3.Vector[T]) float64 {\n\t\treturn v.Distance(pos) - radius\n\t}\n}\n\nfunc Box[T vector.Number](pos vector3.Vector[T], bounds vector3.Vector[T]) Field[T] {\n\thalfBounds := bounds.Scale(0.5)\n\t// It's best to watch the video to understand\n\t// https://www.youtube.com/watch?v=62-pRVZuS5c\n\treturn func(v vector3.Vector[T]) float64 {\n\t\tq := v.Sub(pos).Abs().Sub(halfBounds)\n\t\tinside := math.Min(float64(q.MaxComponent()), 0)\n\t\treturn vector3.Max(q, vector3.Zero[T]()).Length() + inside\n\t}\n}\n\nfunc Union[T vector.Number](fields ...Field[T]) Field[T] {\n\treturn func(v vector3.Vector[T]) float64 {\n\t\tmin := math.MaxFloat64\n\n\t\tfor _, f := range fields {\n\t\t\tfv := f(v)\n\t\t\tif fv \u003c min {\n\t\t\t\tmin = fv\n\t\t\t}\n\t\t}\n\n\t\treturn min\n\t}\n}\n\nfunc Intersect[T vector.Number](fields ...Field[T]) Field[T] {\n\treturn func(v vector3.Vector[T]) float64 {\n\t\tmax := -math.MaxFloat64\n\n\t\tfor _, f := range fields {\n\t\t\tfv := f(v)\n\t\t\tif fv \u003e max {\n\t\t\t\tmax = fv\n\t\t\t}\n\t\t}\n\n\t\treturn max\n\t}\n}\n\nfunc Subtract[T vector.Number](minuend, subtrahend Field[T]) Field[T] {\n\treturn func(f vector3.Vector[T]) float64 {\n\t\treturn math.Max(minuend(f), -subtrahend(f))\n\t}\n}\n\nfunc Translate[T vector.Number](field Field[T], translation vector3.Vector[T]) Field[T] {\n\treturn func(v vector3.Vector[T]) float64 {\n\t\treturn field(v.Sub(translation))\n\t}\n}\n\nfunc evaluateAtDepth[T vector.Number](dimension int, field Field[T], depth T, i int) {\n\timg := image.NewRGBA(image.Rectangle{\n\t\timage.Point{0, 0},\n\t\timage.Point{dimension, dimension},\n\t})\n\n\tfor x := 0; x \u003c dimension; x++ {\n\t\tfor y := 0; y \u003c dimension; y++ {\n\t\t\tv := field(vector3.New[T](T(x), T(y), depth))\n\t\t\tbyteVal := (v / float64(dimension/20)) * 255\n\t\t\tvar c color.Color\n\t\t\tif v \u003e 0 {\n\t\t\t\tc = color.RGBA{R: 0, G: 0, B: byte(byteVal), A: 255}\n\t\t\t} else {\n\t\t\t\tc = color.RGBA{R: 0, G: byte(-byteVal), B: 0, A: 255}\n\t\t\t}\n\t\t\timg.Set(x, y, c)\n\t\t}\n\t}\n\n\tf, err := os.Create(fmt.Sprintf(\"field_%04d.png\", i))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer f.Close()\n\n\terr = png.Encode(f, img)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n\nfunc main() {\n\tdimension := 512\n\tquarterDim := float64(dimension) / 4.\n\n\tmiddleCoord := vector2.\n\t\tFill(dimension).\n\t\tScale(0.5).\n\t\tToFloat64()\n\n\tmiddleCord3D := vector3.New(middleCoord.X(), middleCoord.Y(), 0)\n\n\tsmallRing := Subtract(\n\t\tSphere(middleCord3D, 100),\n\t\tSphere(middleCord3D, 50),\n\t)\n\n\tfield := Intersect(\n\t\tSubtract(\n\t\t\tSphere(middleCord3D, 200),\n\t\t\tSphere(middleCord3D, 100),\n\t\t),\n\t\tUnion(\n\t\t\tTranslate(smallRing, vector3.New(1., 1., 0.).Scale(quarterDim)),\n\t\t\tTranslate(smallRing, vector3.New(1., -1., 0.).Scale(quarterDim)),\n\t\t\tTranslate(smallRing, vector3.New(-1., 1., 0.).Scale(quarterDim)),\n\t\t\tTranslate(smallRing, vector3.New(-1., -1., 0.).Scale(quarterDim)),\n\t\t\tBox(middleCord3D, middleCord3D),\n\t\t),\n\t)\n\n\tfor i := 0; i \u003c 100; i++ {\n\t\tevaluateAtDepth(dimension, field, float64(-dimension/2)+(float64(i*dimension)*0.01), i)\n\t}\n}\n\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felicdavis%2Fvector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felicdavis%2Fvector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felicdavis%2Fvector/lists"}