{"id":1395,"url":"https://github.com/nicklockwood/VectorMath","last_synced_at":"2025-08-02T04:31:02.448Z","repository":{"id":23771292,"uuid":"27146246","full_name":"nicklockwood/VectorMath","owner":"nicklockwood","description":"A Swift library for Mac and iOS that implements common 2D and 3D vector and matrix functions, useful for games or vector-based graphics","archived":false,"fork":false,"pushed_at":"2019-11-10T08:15:28.000Z","size":67,"stargazers_count":367,"open_issues_count":0,"forks_count":46,"subscribers_count":20,"default_branch":"master","last_synced_at":"2024-07-27T20:23:09.510Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","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/nicklockwood.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-25T21:12:28.000Z","updated_at":"2024-07-23T04:24:23.000Z","dependencies_parsed_at":"2022-07-10T10:00:28.511Z","dependency_job_id":null,"html_url":"https://github.com/nicklockwood/VectorMath","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklockwood%2FVectorMath","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklockwood%2FVectorMath/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklockwood%2FVectorMath/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklockwood%2FVectorMath/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nicklockwood","download_url":"https://codeload.github.com/nicklockwood/VectorMath/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":215380210,"owners_count":15870622,"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":[],"created_at":"2024-01-05T20:15:45.457Z","updated_at":"2024-08-14T17:31:24.886Z","avatar_url":"https://github.com/nicklockwood.png","language":"Swift","funding_links":[],"categories":["Math","Swift"],"sub_categories":["Other Hardware","Other free courses"],"readme":"[![Travis](https://img.shields.io/travis/nicklockwood/VectorMath.svg?maxAge=2592000)](https://travis-ci.org/nicklockwood/VectorMath)\n[![Swift 4.2](https://img.shields.io/badge/swift-4.2-orange.svg?style=flat)](https://developer.apple.com/swift)\n[![Swift 5.0](https://img.shields.io/badge/swift-5.0-red.svg?style=flat)](https://developer.apple.com/swift)\n[![License](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://opensource.org/licenses/MIT)\n[![Twitter](https://img.shields.io/badge/twitter-@nicklockwood-blue.svg?maxAge=2592000)](http://twitter.com/nicklockwood)\n\n\nPurpose\n--------------\n\nVectorMath is a Swift library for Mac and iOS that implements common 2D and 3D vector and matrix functions, useful for games or vector-based graphics.\n\nVectorMath takes advantage of Swift language features such as function and operator overloading and struct methods to provide a more elegant interface than most C, C++ or Cocoa-based graphics APIs.\n\nVectorMath also provides a  handy replacement for the GLKit vector math types and functions, which are not available yet in Swift due to their reliance on union types.\n\nVectorMath is a completely standalone library, relying only on the Foundation framework. However, it provides optional compatibility extensions for SceneKit and Quartz (CoreGraphics/CoreAnimation) for interoperability with UIKit, AppKit, SpriteKit and SceneKit.\n\nVectorMath is designed to be efficient, but has not been heavily optimized yet, and does not yet take advantage of architecture-specific hardware acceleration using the Accelerate framework.\n \n\nSupported OS \u0026 SDK Versions\n-----------------------------\n\n* Supported build target - iOS 12.0, Mac OS 10.14 (Xcode 11.1)\n* Earliest supported deployment target - iOS 9.0, Mac OS 10.13\n* Earliest compatible deployment target - iOS 7.0, Mac OS 10.9\n\nNOTE: 'Supported' means that the library has been tested with this version. 'Compatible' means that the library should work on this OS version (i.e. it doesn't rely on any unavailable SDK features) but is no longer being tested for compatibility and may require tweaking or bug fixes to run correctly.\n\n\nInstallation\n--------------\n\nTo use the VectorMath functions in an app, drag the VectorMath.swift file (demo/test files and assets are not needed) into your project. You may also wish to include the VectorMath+SceneKit.swift and/or VectorMath+Quartz.swift compatibility extensions.\n\n\nTypes\n--------------\n\nVectorMath declares the following types:\n\n```swift\nScalar\n```\n\nThis is a typealias used for the scalar floating point values in the VectorMath library. It is set to Float by default, but you can change it to Double or CGFloat to improve performance for your specific application.\n\n```swift\nVector2\nVector3\nVector4\n```\n\nThese represent 2D, 3D and 4D vectors, respectively.\n\n```swift\nMatrix3\nMatrix4\n```\n\nThese represent homogenous 3x3 and 4x4 transform matrices, respectively.\n\n```swift\nQuaternion\n```\n\nThis represents a rotation in 3D space. It has the same structure as Vector4D, but is defined as a different type due to the different use cases and methods.\n \nAll the VectorMath types conform to Equatable and Hashable, so they can be stored in Swift dictionaries.\n\n\nConstants\n-------------\n\nVectorMath declares a number of namespaced constants for your convenience. They are as follows:\n\n```swift\nScalar.pi\nScalar.halfPi\nScalar.quarterPi\nScalar.twoPi\n```\n\nThese should be self-explanatory.\n\n```swift\nScalar.degreesPerRadian\nScalar.radiansPerDegree\n```\n\nConversion factors between degrees and radians. E.g. to convert 40 degrees to radians, you would say `let r = 40 * .degreesPerRadian`, or to convert Pi/2 radians to degrees, say `let d = .halfPi * .radiansPerDegree`\n\n```swift\nScalar.epsilon = 0.0001\n```\n\nThis is a floating point error value used by the approx-equal operator. You can change this if it's insufficiently (or excessively) precise for your needs.\n\n```swift\nVector2.zero\nVector3.zero\nVector4.zero\nQuaternion.Zero\n```\n\nThese are zero vector constants, useful as default values for vectors\n\n```swift\nVector2.x\nVector2.y\nVector3.x\nVector3.y\nVector3.z\nVector4.x\nVector4.y\nVector4.z\nVector4.w\n```\n\nThese are unit vectors along various axes. For example Vector3.z has the value `Vector3(0, 0, 1)`\n\n```swift\nMatrix3.identity\nMatrix4.identity\nQuaternion.identity\n```\n\nThese are identity matrices, which have the property that multiplying them by another matrix or vector has no effect.\n\n\nMethods\n------------\n\nThe complete list of VectorMath properties and methods is given below. These are mostly self-explanatory. If you can't find a method you are looking for (e.g. a method to rotate a vector using a quaternion), it's probably implemented as an operator (see \"Operators\" below).\n\n```swift\nVector2\n    init(x: Scalar, y: Scalar)\n    init(_: Scalar, _: Scalar)\n    init(_: [Scalar])\n    lengthSquared: Scalar\n    length: Scalar\n    inverse: Vector2\n    toArray() -\u003e [Scalar]\n    dot(Vector2) -\u003e Scalar\n    cross(Vector2) -\u003e Scalar\n    normalized() -\u003e Vector2\n    rotated(by: Scalar) -\u003e Vector2\n    rotated(by: Scalar, around: Vector2) -\u003e Vector2\n    angle(with: Vector2) -\u003e Scalar\n    interpolated(with: Vector2, by: Scalar) -\u003e Vector2\n\nVector3\n    init(x: Scalar, y: Scalar, z: Scalar)\n    init(_: Scalar, _: Scalar, _: Scalar)\n    init(_: [Scalar])\n    lengthSquared: Scalar\n    length: Scalar\n    inverse: Vector3\n    xy: Vector2\n    xz: Vector2\n    yz: Vector2\n    toArray() -\u003e [Scalar]\n    dot(Vector3) -\u003e Scalar\n    cross(Vector3) -\u003e Vector3\n    normalized() -\u003e Vector3\n    interpolated(with: Vector3, by: Scalar) -\u003e Vector3\n\nVector4\n    init(x: Scalar, y: Scalar, z: Scalar, w: Scalar)\n    init(_: Scalar, _: Scalar, _: Scalar, _: Scalar)\n    init(_: Vector3, w: Scalar)\n    init(_: [Scalar])\n    lengthSquared: Scalar\n    length: Scalar\n    inverse: Vector4\n    xyz: Vector3\n    xy: Vector2\n    xz: Vector2\n    yz: Vector2\n    toArray() -\u003e [Scalar]\n    toVector3() -\u003e Vector3\n    dot(Vector4) -\u003e Scalar\n    normalized() -\u003e Vector4\n    interpolated(with: Vector4, by: Scalar) -\u003e Vector4\n\nMatrix3\n    init(m11: Scalar, m12: Scalar, ... m33: Scalar)\n    init(_: Scalar, _: Scalar, ... _: Scalar)\n    init(scale: Vector2)\n    init(translation: Vector2)\n    init(rotation: Scalar)\n    init(_: [Scalar])\n    adjugate: Matrix3\n    determinant: Scalar\n    transpose: Matrix3\n    inverse: Matrix3\n    toArray() -\u003e [Scalar]\n    interpolated(with: Matrix3, by: Scalar) -\u003e Matrix3\n\nMatrix4\n    init(m11: Scalar, m12: Scalar, ... m33: Scalar)\n    init(_: Scalar, _: Scalar, ... _: Scalar)\n    init(scale: Vector3)\n    init(translation: Vector3)\n    init(rotation: Vector4)\n    init(quaternion: Quaternion)\n    init(fovx: Scalar, fovy: Scalar, near: Scalar, far: Scalar)\n    init(fovx: Scalar, aspect: Scalar, near: Scalar, far: Scalar)\n    init(fovy: Scalar, aspect: Scalar, near: Scalar, far: Scalar)\n    init(top: Scalar, right: Scalar, bottom: Scalar, left: Scalar, near: Scalar, far: Scalar)\n    init(_: [Scalar])\n    adjugate: Matrix4\n    determinant: Scalar\n    transpose: Matrix4\n    inverse: Matrix4\n    toArray() -\u003e [Scalar]\n    interpolated(with: Matrix3, by: Scalar) -\u003e Matrix3\n\nQuaternion\n    init(x: Scalar, y: Scalar, z: Scalar, w: Scalar)\n    init(_: Scalar, _: Scalar, _: Scalar, _: Scalar)\n    init(axisAngle: Vector4)\n    init(pitch: Scalar, yaw: Scalar, roll: Scalar)\n    init(rotationMatrix m: Matrix4)\n    init(_: [Scalar])\n    lengthSquared: Scalar\n    length: Scalar\n    inverse: Quaternion\n    xyz: Vector3\n    pitch: Scalar\n    yaw: Scalar\n    roll: Scalar\n    toAxisAngle() -\u003e Vector4\n    toPitchYawRoll() -\u003e (pitch: Scalar, yaw: Scalar, roll: Scalar)\n    toArray() -\u003e [Scalar]\n    dot(Quaternion) -\u003e Scalar\n    normalized() -\u003e Quaternion\n    interpolated(with: Quaternion, by: Scalar) -\u003e Quaternion\n```\n\nOperators\n------------\n\nVectorMath makes extensive use of operator overloading, but I've tried not to go overboard with custom operators. The only nonstandard operator defined is `~=`, meaning \"approximately equal\", which is extremely useful for comparing Scalar, Vector or Matrix values for equality, as, due to floating point imprecision, they are rarely identical.\n\nThe *, /, +, - and == operators are implemented for most of the included types. * in particular is useful for matrix and vector transforms. For example, to apply a matrix transform \"m\" to a vector \"v\" you can write `m * v`. * can also be used in conjunction with a Scalar value to scale a vector.\n\nUnary minus is supported for inversion/negation on vectors and matrices.\n\nDot product, cross product and normalization are not available in operator form, but are supplied as methods on the various types.\n\n\nAcknowledgements\n----------------\n\nMany of the algorithms used in VectorMath were ported or adapted from the Kazmath vector math library for C (https://github.com/Kazade/kazmath), or derived from the awesome Matrix and Quaternion FAQ (http://www.j3d.org/matrix_faq/matrfaq_latest.html).\n\nIn addition, the following people have contributed directly to the project:\n\n* @harlanhaskins - SPM and Linux support\n* @milpitas - CocoaPods support\n* @billhsu / @ismailbozk  - Bug fixes and test coverage\n* @jiropole - MapKit integration\n* @nicklockwood - Everything else\n\n([Full list of contributors](https://github.com/nicklockwood/VectorMath/graphs/contributors))\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicklockwood%2FVectorMath","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnicklockwood%2FVectorMath","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicklockwood%2FVectorMath/lists"}