├── .editorconfig ├── .gitignore ├── .npmignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── __test__ └── all-classes.test.ts ├── dist └── .keepme ├── docs ├── .nojekyll ├── assets │ ├── css │ │ ├── main.css │ │ └── main.css.map │ ├── images │ │ ├── icons.png │ │ ├── icons@2x.png │ │ ├── widgets.png │ │ └── widgets@2x.png │ └── js │ │ ├── main.js │ │ └── search.js ├── classes │ ├── _abstractvector_.abstractvector.html │ ├── _arrayvector_.arrayvector.html │ ├── _float32vector_.float32vector.html │ └── _vector_.vector.html ├── globals.html ├── index.html ├── interfaces │ └── _abstractvector_.vectorconstructable.html └── modules │ ├── _abstractvector_.html │ ├── _arrayvector_.html │ ├── _float32vector_.html │ ├── _vec2d_.html │ └── _vector_.html ├── package-lock.json ├── package.json ├── src ├── AbstractVector.ts ├── ArrayVector.ts ├── Float32Vector.ts ├── Vec2D.ts └── Vector.ts └── tsconfig.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | trim_trailing_whitespace = true 7 | insert_final_newline = true 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | *.log 4 | .idea 5 | .vscode 6 | coverage 7 | 8 | dist/*.js 9 | 10 | src/*.js 11 | src/*.js.map 12 | src/*.d.ts 13 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # No need to publish typescript source files 2 | *.ts 3 | 4 | # These are ignored by Git, but we DO want to publish them to npm 5 | !src/*.js 6 | !src/*.d.ts 7 | 8 | # Tests, examples, etc. should be ignored too 9 | __test__ 10 | dist 11 | example 12 | .travis.yml 13 | .editorconfig 14 | docs 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - "10" 5 | - "9" 6 | - "8" 7 | - "6" 8 | 9 | after_success: 10 | - npm run coveralls 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | Dates are YYYY-MM-DD. 4 | 5 | ## 3.0.0 / 2018-05-24 6 | * Convert codebase to TypeScript 7 | * Add support for TypeScript 8 | * Expose `AbstractVector` class for extension if desired 9 | * Enforce need for `new` keyword when creating vectors 10 | * Publish code coverage to Coveralls 11 | * Increase code coverage 12 | * Test against multiple Node.js versions in CI 13 | * Add generated documentation 14 | 15 | ## 2.x 16 | * Build using browserify 17 | * Removed static methods from exposed API 18 | * Various performance improvements 19 | 20 | ## 1.x 21 | * Here be dragons 22 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | All contributors are welcome! Be respectful of everyone involved and together we 4 | can all get awesome things done in the world of OSS 🤘 5 | 6 | ## Testing 7 | If you're adding new functions or changing functionality please create or update 8 | the unit tests. Simple. 9 | 10 | ## Submitting Changes 11 | When submitting a Pull Request please: 12 | 13 | * Reference an Issue (if one exists) 14 | * Provide a clear description of the change 15 | * Use a clear and concise commit message 16 | 17 | ## Coding Conventions 18 | A style guide is enforced using 19 | [Prettier](https://www.npmjs.com/package/prettier) and it will run as part of 20 | your commit along with the unit tests. Let it do it's thing and everything will 21 | be good. 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2018 Evan Shortiss 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | 'Software'), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vector2D - 2D Vectors for TypeScript & JavaScript 2 | 3 | [![Travis CI](https://travis-ci.org/evanshortiss/vector2d.svg?branch=master)](https://travis-ci.org/evanshortiss/vector2d) 4 | [![Coverage Status](https://coveralls.io/repos/github/evanshortiss/vector2d/badge.svg?branch=master)](https://coveralls.io/github/evanshortiss/vector2d?branch=master) 5 | [![TypeScript](https://badges.frapsoft.com/typescript/code/typescript.svg?v=101)](https://github.com/ellerbrock/typescript-badges/) 6 | 7 | ## Documentation 8 | 9 | Detailed documentation is available [here](http://evanshortiss.com/vector2d/). 10 | 11 | ## Installation and Usage 12 | 13 | Install via npm: 14 | 15 | ```bash 16 | npm install vector2d 17 | ``` 18 | 19 | Usage with JavaScript: 20 | 21 | ```js 22 | var Vec2D = require('vector2d'); 23 | 24 | const v = new Vec2D.Vector(2, 3) 25 | ``` 26 | 27 | Usage with TypeScript/ES6: 28 | 29 | ```ts 30 | import * as Vec2D from 'vector2d' 31 | 32 | const v = new Vec2D.Vector(2, 3) 33 | ``` 34 | 35 | It's also possible to use this module directly in the browser through 36 | `window.Vec2D`. Simply clone the code locally and run the following: 37 | 38 | ``` 39 | npm install 40 | npm run browserify 41 | npm run uglify 42 | ``` 43 | 44 | This will produce `vec2d.js` and `vec2d.min.js` files in `dist/` the folder of 45 | the repository that you can include via ` 178 | 179 | 180 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | vector2d 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |
15 |
16 | 27 |
28 |
29 | Options 30 |
31 |
32 | All 33 |
    34 |
  • Public
  • 35 |
  • Public/Protected
  • 36 |
  • All
  • 37 |
38 |
39 | 40 | 41 | 42 | 43 | 44 | 45 |
46 |
47 | Menu 48 |
49 |
50 |
51 |
52 |
53 |
54 | 59 |

vector2d

60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |

Vector2D - 2D Vectors for TypeScript & JavaScript

68 |

Travis CI 69 | Coverage Status 70 | TypeScript

71 |

Documentation

72 |

Detailed documentation is available here.

73 |

Installation and Usage

74 |

Install via npm:

75 |
npm install vector2d
 76 | 
77 |

Usage with JavaScript:

78 |
var Vec2D = require('vector2d');
 79 | 
 80 | const v = new Vec2D.Vector(2, 3)
 81 | 
82 |

Usage with TypeScript/ES6:

83 |
import * as Vec2D from 'vector2d'
 84 | 
 85 | const v = new Vec2D.Vector(2, 3)
 86 | 
87 |

It's also possible to use this module directly in the browser through 88 | window.Vec2D. Simply clone the code locally and run the following:

89 |
npm install
 90 | npm run browserify
 91 | npm run uglify
 92 | 

This will produce vec2d.js and vec2d.min.js files in dist/ the folder of 93 | the repository that you can include via <script> tags.

94 |

About

95 |

An easy to use 2D Vector library with 3 methods of Vector representation to 96 | optimise for your particular use case.

97 |

Vector2D provides 3 main modes of Vector representations (classes):

98 |
    99 |
  • Vec2D.ArrayVector
  • 100 |
  • Vec2D.Float32Vector
  • 101 |
  • Vec2D.Vector
  • 102 |
103 |

The different representations is really more of an experiment than a necessity 104 | since for most users Vector or ArrayVector types will be fine.

105 |

Regardless of the class used all methods can be used in the same manner and 106 | you don't need to worry about the underlying representation.

107 |

API

108 |

Vector Instance Methods

109 |

All instance methods modify the underlying vector where possible. For example 110 | calling multiplyByScalar will multiply the vector x and y components by the 111 | provided number and return the updated vector itself (a reference to this) 112 | rather than a new instance. The benefit if this is that less objects are created 113 | which reduces garbage collection and makes chaining possible.

114 |

If you don't want to modfy the vector itself you can call v.clone() and modify 115 | the newly returned vector instead. Here's an example.

116 |
const av0 = new Vec2D.ArrayVector(10, 0)
117 | const av1 = new Vec2D.ArrayVector(0, 5)
118 | 
119 | // Methods are chainable where you'd expect
120 | const newVec = av0.clone().add(av1);
121 | newVec.toString(); // (10, 5)
122 | 
123 | // Original vector hasn't changed!
124 | av0.toString(); // (10, 0)
125 | 
126 |

setAxes(x, y)

127 |

Set the x and y values of this vector.

128 |

toString()

129 |

Convert vector to a String with format "(0, 0)"

130 |

toArray()

131 |

Convert vector to standard JavaScript Array [2.823, 1.541]

132 |

toObject()

133 |

Covert vector to a JSON object with format { x: 1.4, y: 8 }.

134 |

add(vector)

135 |

Modify this vector by adding the provided vector to it.

136 |

subtract(vector)

137 |

Modify this vector by subtracting the provided vector from it.

138 |

equals(vector)

139 |

Check does this vector equal the provided vector.

140 |

mulV(vector)

141 |

Multiply this vector by provided vector.

142 |

divV(vector)

143 |

Divide this vector by provided vector.

144 |

mulS(number)

145 |

Multiply this vector by a number.

146 |

divS(number)

147 |

Divide this vector by a number.

148 |

dot(vector)

149 |

Returns dot product from this vector and the provided vector.

150 |

cross(vector)

151 |

Returns cross product from this vector and the provided vector.

152 |

reverse()

153 |

Reverse the values in this vector.

154 |

abs()

155 |

Convert stored values to absolute values.

156 |

distance(vec)

157 |

Find the distance between this vector and the provided vec;

158 |

zero()

159 |

Set vector values to 0.

160 |

rotate(radians)

161 |

Rotate vector by provided radians.

162 |

clone()

163 |

Returns a clone of this vector.

164 |
165 |
166 | 194 |
195 |
196 | 255 |
256 |

Generated using TypeDoc

257 |
258 |
259 | 260 | 261 | 262 | -------------------------------------------------------------------------------- /docs/interfaces/_abstractvector_.vectorconstructable.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | VectorConstructable | vector2d 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |
15 |
16 | 27 |
28 |
29 | Options 30 |
31 |
32 | All 33 |
    34 |
  • Public
  • 35 |
  • Public/Protected
  • 36 |
  • All
  • 37 |
38 |
39 | 40 | 41 | 42 | 43 | 44 | 45 |
46 |
47 | Menu 48 |
49 |
50 |
51 |
52 |
53 |
54 | 65 |

Interface VectorConstructable<T>

66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |

Type parameters

74 |
    75 |
  • 76 |

    T

    77 |
  • 78 |
79 |
80 |
81 |

Hierarchy

82 |
    83 |
  • 84 | VectorConstructable 85 |
  • 86 |
87 |
88 |
89 |

Index

90 |
91 |
92 |
93 |

Constructors

94 | 97 |
98 |
99 |
100 |
101 |
102 |

Constructors

103 |
104 | 105 |

constructor

106 |
    107 |
  • new VectorConstructable(x: number, y: number): T
  • 108 |
109 |
    110 |
  • 111 | 116 |

    Parameters

    117 |
      118 |
    • 119 |
      x: number
      120 |
    • 121 |
    • 122 |
      y: number
      123 |
    • 124 |
    125 |

    Returns T

    126 |
  • 127 |
128 |
129 |
130 |
131 | 177 |
178 |
179 | 238 |
239 |

Generated using TypeDoc

240 |
241 |
242 | 243 | 244 | 245 | -------------------------------------------------------------------------------- /docs/modules/_abstractvector_.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | "AbstractVector" | vector2d 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |
15 |
16 | 27 |
28 |
29 | Options 30 |
31 |
32 | All 33 |
    34 |
  • Public
  • 35 |
  • Public/Protected
  • 36 |
  • All
  • 37 |
38 |
39 | 40 | 41 | 42 | 43 | 44 | 45 |
46 |
47 | Menu 48 |
49 |
50 |
51 |
52 |
53 |
54 | 62 |

External module "AbstractVector"

63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |

Index

71 |
72 |
73 |
74 |

Classes

75 | 78 |
79 |
80 |

Interfaces

81 | 84 |
85 |
86 |

Variables

87 | 90 |
91 |
92 |
93 |
94 |
95 |

Variables

96 |
97 | 98 |

Const precision

99 |
precision: number[] = [1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000]
100 | 105 |
106 |
107 |

These values are used by the AbstractVector.round method to increase 108 | performance vs. using Number.toFixed.

109 |
110 |
111 |
112 |
113 |
114 | 151 |
152 |
153 | 212 |
213 |

Generated using TypeDoc

214 |
215 |
216 | 217 | 218 | 219 | -------------------------------------------------------------------------------- /docs/modules/_arrayvector_.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | "ArrayVector" | vector2d 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |
15 |
16 | 27 |
28 |
29 | Options 30 |
31 |
32 | All 33 |
    34 |
  • Public
  • 35 |
  • Public/Protected
  • 36 |
  • All
  • 37 |
38 |
39 | 40 | 41 | 42 | 43 | 44 | 45 |
46 |
47 | Menu 48 |
49 |
50 |
51 |
52 |
53 |
54 | 62 |

External module "ArrayVector"

63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |

Index

71 |
72 |
73 |
74 |

Classes

75 | 78 |
79 |
80 |
81 |
82 |
83 | 114 |
115 |
116 | 175 |
176 |

Generated using TypeDoc

177 |
178 |
179 | 180 | 181 | 182 | -------------------------------------------------------------------------------- /docs/modules/_float32vector_.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | "Float32Vector" | vector2d 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |
15 |
16 | 27 |
28 |
29 | Options 30 |
31 |
32 | All 33 |
    34 |
  • Public
  • 35 |
  • Public/Protected
  • 36 |
  • All
  • 37 |
38 |
39 | 40 | 41 | 42 | 43 | 44 | 45 |
46 |
47 | Menu 48 |
49 |
50 |
51 |
52 |
53 |
54 | 62 |

External module "Float32Vector"

63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |

Index

71 |
72 |
73 |
74 |

Classes

75 | 78 |
79 |
80 |
81 |
82 |
83 | 114 |
115 |
116 | 175 |
176 |

Generated using TypeDoc

177 |
178 |
179 | 180 | 181 | 182 | -------------------------------------------------------------------------------- /docs/modules/_vec2d_.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | "Vec2D" | vector2d 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |
15 |
16 | 27 |
28 |
29 | Options 30 |
31 |
32 | All 33 |
    34 |
  • Public
  • 35 |
  • Public/Protected
  • 36 |
  • All
  • 37 |
38 |
39 | 40 | 41 | 42 | 43 | 44 | 45 |
46 |
47 | Menu 48 |
49 |
50 |
51 |
52 |
53 |
54 | 62 |

External module "Vec2D"

63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 | 98 |
99 |
100 | 159 |
160 |

Generated using TypeDoc

161 |
162 |
163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /docs/modules/_vector_.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | "Vector" | vector2d 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |
15 |
16 | 27 |
28 |
29 | Options 30 |
31 |
32 | All 33 |
    34 |
  • Public
  • 35 |
  • Public/Protected
  • 36 |
  • All
  • 37 |
38 |
39 | 40 | 41 | 42 | 43 | 44 | 45 |
46 |
47 | Menu 48 |
49 |
50 |
51 |
52 |
53 |
54 | 62 |

External module "Vector"

63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |

Index

71 |
72 |
73 |
74 |

Classes

75 | 78 |
79 |
80 |
81 |
82 |
83 | 114 |
115 |
116 | 175 |
176 |

Generated using TypeDoc

177 |
178 |
179 | 180 | 181 | 182 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vector2d", 3 | "keywords": [ 4 | "physics", 5 | "math", 6 | "vector", 7 | "vectors", 8 | "2d", 9 | "float", 10 | "float32", 11 | "Float32Array", 12 | "typescript", 13 | "tsc" 14 | ], 15 | "description": "2D Vector library offering Float32Array, Array or standard Object based vectors.", 16 | "main": "src/Vec2D.js", 17 | "typings": "src/Vec2D.d.ts", 18 | "author": "Evan Shortiss (http://evanshortiss.com)", 19 | "version": "3.0.0", 20 | "repository": { 21 | "type": "git", 22 | "url": "git+https://github.com/evanshortiss/vector2d.git" 23 | }, 24 | "scripts": { 25 | "tsc": "tsc", 26 | "test": "tsc && jest", 27 | "coveralls": "cat coverage/lcov.info | coveralls", 28 | "prepublish": "tsc", 29 | "prettier": "prettier src/*.ts !src/*.d.ts --write --no-semi", 30 | "precommit": "npm run prettier && npm run test && npm run typedoc", 31 | "browserify": "browserify -s Vec2D -e src/Vec2D.js -o dist/vec2d.js", 32 | "uglify": "uglifyjs -c -m -o dist/vec2d.min.js dist/vec2d.js", 33 | "typedoc": "typedoc --out docs/ src/ && touch docs/.nojekyll && git add ." 34 | }, 35 | "jest": { 36 | "testEnvironment": "node", 37 | "cache": false, 38 | "collectCoverage": true, 39 | "transform": { 40 | "^.+\\.tsx?$": "ts-jest" 41 | }, 42 | "coveragePathIgnorePatterns": [ 43 | "/node_modules/" 44 | ], 45 | "coverageThreshold": { 46 | "global": { 47 | "branches": 90, 48 | "functions": 90, 49 | "lines": 90, 50 | "statements": 90 51 | } 52 | }, 53 | "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", 54 | "moduleFileExtensions": [ 55 | "ts", 56 | "tsx", 57 | "js", 58 | "jsx", 59 | "json", 60 | "node" 61 | ] 62 | }, 63 | "devDependencies": { 64 | "@types/jest": "~22.2.3", 65 | "@types/node": "~10.0.8", 66 | "browserify": "~16.2.2", 67 | "coveralls": "~3.0.1", 68 | "husky": "~0.14.3", 69 | "jest": "~22.0.6", 70 | "prettier": "~1.12.1", 71 | "ts-jest": "~22.0.4", 72 | "typedoc": "~0.11.1", 73 | "typescript": "~2.8.3", 74 | "uglify-js": "~3.3.27" 75 | }, 76 | "bugs": { 77 | "url": "https://github.com/evanshortiss/vector2d/issues" 78 | }, 79 | "license": "MIT", 80 | "engines": { 81 | "node": ">=6" 82 | }, 83 | "homepage": "https://github.com/evanshortiss/vector2d#readme", 84 | "directories": { 85 | "doc": "docs" 86 | }, 87 | "dependencies": {} 88 | } 89 | -------------------------------------------------------------------------------- /src/AbstractVector.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * These values are used by the `AbstractVector.round` method to increase 3 | * performance vs. using Number.toFixed. 4 | */ 5 | const precision = [ 6 | 1, 7 | 10, 8 | 100, 9 | 1000, 10 | 10000, 11 | 100000, 12 | 1000000, 13 | 10000000, 14 | 100000000, 15 | 1000000000, 16 | 10000000000 17 | ] 18 | 19 | export interface VectorConstructable { 20 | new (x: number, y: number): T 21 | } 22 | 23 | /** 24 | * The class that all other vector representations are derived from. 25 | * 26 | * Contains the core implementation for all methods that will be exposed by 27 | * vector instances. 28 | * 29 | * Example of creating a custom implementation: 30 | * 31 | * ```ts 32 | * import { AbstractVector } from "./AbstractVector" 33 | * 34 | * export class MyCustomVector extends AbstractVector { 35 | * constructor (x: number, y: number) { 36 | * super(CustomVectorType) 37 | * } 38 | * } 39 | * ``` 40 | */ 41 | export abstract class AbstractVector { 42 | constructor(protected ctor: VectorConstructable) {} 43 | 44 | abstract set x(x: number) 45 | abstract get x(): number 46 | 47 | abstract set y(y: number) 48 | abstract get y(): number 49 | 50 | /** 51 | * Set both x and y axis value 52 | * @param x New x val 53 | * @param y New y val 54 | */ 55 | setAxes(x: number, y: number) { 56 | this.x = x 57 | this.y = y 58 | return this 59 | } 60 | 61 | /** 62 | * Getter for x the axis value 63 | */ 64 | getX() { 65 | return this.x 66 | } 67 | 68 | /** 69 | * Setter for x axis value 70 | */ 71 | setX(x: number) { 72 | this.x = x 73 | 74 | return this 75 | } 76 | 77 | /** 78 | * Getter for y axis value 79 | */ 80 | getY() { 81 | return this.y 82 | } 83 | 84 | /** 85 | * Setter for y axis. 86 | */ 87 | setY(y: number) { 88 | this.y = y 89 | 90 | return this 91 | } 92 | 93 | /** 94 | * Return the vector as a formatted string, e.g "(0, 4)" 95 | */ 96 | toString(round = false) { 97 | if (round) { 98 | return `(${Math.round(this.x)}, ${Math.round(this.y)})` 99 | } 100 | return `(${this.x}, ${this.y})` 101 | } 102 | 103 | /** 104 | * Return an Array containing the vector axes, e.g [0, 4] 105 | */ 106 | toArray() { 107 | return [this.x, this.y] 108 | } 109 | 110 | /** 111 | * Return an Object containing the vector axes, e.g { x: 0, y: 4 } 112 | */ 113 | toObject() { 114 | return { 115 | x: this.x, 116 | y: this.y 117 | } 118 | } 119 | 120 | /** 121 | * Add the provided vector to this one 122 | */ 123 | add(vec: AbstractVector) { 124 | this.x += vec.x 125 | this.y += vec.y 126 | 127 | return this 128 | } 129 | 130 | /** 131 | * Subtract the provided vector from this one 132 | */ 133 | subtract(vec: AbstractVector) { 134 | this.x -= vec.x 135 | this.y -= vec.y 136 | 137 | return this 138 | } 139 | 140 | /** 141 | * Check if the provided vector equal to this one 142 | */ 143 | equals(vec: AbstractVector) { 144 | return vec.x === this.x && vec.y === this.y 145 | } 146 | 147 | /** 148 | * Multiply this vector by the provided vector 149 | */ 150 | multiplyByVector(vec: AbstractVector) { 151 | this.x *= vec.x 152 | this.y *= vec.y 153 | 154 | return this 155 | } 156 | 157 | /** 158 | * Multiply this vector by the provided vector 159 | */ 160 | mulV(vec: AbstractVector) { 161 | return this.multiplyByVector(vec) 162 | } 163 | 164 | /** 165 | * Divide this vector by the provided vector 166 | */ 167 | divideByVector(vec: AbstractVector) { 168 | this.x /= vec.x 169 | this.y /= vec.y 170 | return this 171 | } 172 | 173 | /** 174 | * Divide this vector by the provided vector 175 | */ 176 | divV(v: AbstractVector) { 177 | return this.divideByVector(v) 178 | } 179 | 180 | /** 181 | * Multiply this vector by the provided number 182 | */ 183 | multiplyByScalar(n: number) { 184 | this.x *= n 185 | this.y *= n 186 | 187 | return this 188 | } 189 | 190 | /** 191 | * Multiply this vector by the provided number 192 | */ 193 | mulS(n: number) { 194 | return this.multiplyByScalar(n) 195 | } 196 | 197 | /** 198 | * Divive this vector by the provided number 199 | */ 200 | divideByScalar(n: number) { 201 | this.x /= n 202 | this.y /= n 203 | return this 204 | } 205 | 206 | /** 207 | * Divive this vector by the provided number 208 | */ 209 | divS(n: number) { 210 | return this.divideByScalar(n) 211 | } 212 | 213 | /** 214 | * Normalise this vector 215 | */ 216 | normalise() { 217 | return this.divideByScalar(this.magnitude()) 218 | } 219 | 220 | /** 221 | * For American spelling. Same as unit/normalise function 222 | */ 223 | normalize() { 224 | return this.normalise() 225 | } 226 | 227 | /** 228 | * The same as normalise and normalize 229 | */ 230 | unit() { 231 | return this.normalise() 232 | } 233 | 234 | /** 235 | * Returns the magnitude (length) of this vector 236 | */ 237 | magnitude() { 238 | const x = this.x 239 | const y = this.y 240 | 241 | return Math.sqrt(x * x + y * y) 242 | } 243 | 244 | /** 245 | * Returns the magnitude (length) of this vector 246 | */ 247 | length() { 248 | return this.magnitude() 249 | } 250 | 251 | /** 252 | * Returns the squred length of this vector 253 | */ 254 | lengthSq() { 255 | const x = this.x 256 | const y = this.y 257 | 258 | return x * x + y * y 259 | } 260 | 261 | /** 262 | * Returns the dot product of this vector by another 263 | */ 264 | dot(vec: AbstractVector) { 265 | return vec.x * this.x + vec.y * this.y 266 | } 267 | 268 | /** 269 | * Returns the cross product of this vector by another. 270 | */ 271 | cross(vec: AbstractVector) { 272 | return this.x * vec.y - this.y * vec.x 273 | } 274 | 275 | /** 276 | * Reverses this vector i.e multiplies it by -1 277 | */ 278 | reverse() { 279 | this.x = -this.x 280 | this.y = -this.y 281 | return this 282 | } 283 | 284 | /** 285 | * Set the vector axes values to absolute values 286 | */ 287 | abs() { 288 | this.x = Math.abs(this.x) 289 | this.y = Math.abs(this.y) 290 | 291 | return this 292 | } 293 | 294 | /** 295 | * Zeroes the vector i.e sets all axes to 0 296 | */ 297 | zero() { 298 | this.x = this.y = 0 299 | 300 | return this 301 | } 302 | 303 | /** 304 | * Returns the distance between this vector and another 305 | */ 306 | distance(v: AbstractVector) { 307 | var x = this.x - v.x 308 | var y = this.y - v.y 309 | 310 | return Math.sqrt(x * x + y * y) 311 | } 312 | 313 | /** 314 | * Rotates the vetor by provided radians 315 | */ 316 | rotate(rads: number) { 317 | const cos = Math.cos(rads) 318 | const sin = Math.sin(rads) 319 | 320 | const ox = this.x 321 | const oy = this.y 322 | 323 | this.x = ox * cos - oy * sin 324 | this.y = ox * sin + oy * cos 325 | 326 | return this 327 | } 328 | 329 | /** 330 | * Rounds this vector to n decimal places 331 | */ 332 | round(n = 2) { 333 | var p = precision[n] 334 | 335 | // This performs waaay better than toFixed and give Float32 the edge again. 336 | // http://www.dynamicguru.com/javascript/round-numbers-with-precision/ 337 | this.x = ((0.5 + this.x * p) << 0) / p 338 | this.y = ((0.5 + this.y * p) << 0) / p 339 | 340 | return this 341 | } 342 | 343 | /** 344 | * Returns a copy of this vector 345 | */ 346 | clone() { 347 | return new this.ctor(this.x, this.y) 348 | } 349 | } 350 | -------------------------------------------------------------------------------- /src/ArrayVector.ts: -------------------------------------------------------------------------------- 1 | import { AbstractVector } from "./AbstractVector" 2 | 3 | /** 4 | * A vector representation that stores the axes in an Array 5 | * 6 | * ``` 7 | * const v = new Vec2D.ArrayVector(2, 5) 8 | * ``` 9 | */ 10 | export class ArrayVector extends AbstractVector { 11 | protected axes: number[] 12 | 13 | constructor(x: number, y: number) { 14 | super(ArrayVector) 15 | this.axes = [x, y] 16 | this.ctor = ArrayVector 17 | } 18 | 19 | get x(): number { 20 | return this.axes[0] 21 | } 22 | set x(x: number) { 23 | this.axes[0] = x 24 | } 25 | 26 | get y(): number { 27 | return this.axes[1] 28 | } 29 | set y(y: number) { 30 | this.axes[1] = y 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Float32Vector.ts: -------------------------------------------------------------------------------- 1 | import { AbstractVector } from "./AbstractVector" 2 | 3 | /** 4 | * A vector representation that stores the axes in a Float32Array 5 | * 6 | * ``` 7 | * const v = new Vec2D.Float32Vector(2, 5) 8 | * ``` 9 | */ 10 | export class Float32Vector extends AbstractVector { 11 | protected axes: Float32Array 12 | 13 | constructor(x: number, y: number) { 14 | super(Float32Vector) 15 | this.axes = new Float32Array(2) 16 | this.axes[0] = x 17 | this.axes[1] = y 18 | } 19 | 20 | get x(): number { 21 | return this.axes[0] 22 | } 23 | set x(x: number) { 24 | this.axes[0] = x 25 | } 26 | 27 | get y(): number { 28 | return this.axes[1] 29 | } 30 | set y(y: number) { 31 | this.axes[1] = y 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Vec2D.ts: -------------------------------------------------------------------------------- 1 | export * from "./AbstractVector" 2 | export * from "./ArrayVector" 3 | export * from "./Float32Vector" 4 | export * from "./Vector" 5 | -------------------------------------------------------------------------------- /src/Vector.ts: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | import { AbstractVector } from "./AbstractVector" 4 | 5 | /** 6 | * A vector representation that stores the axes as part of the instance itself 7 | * 8 | * ```ts 9 | * const v = new Vec2D.Vector(2, 5) 10 | * ``` 11 | */ 12 | export class Vector extends AbstractVector { 13 | protected _x: number 14 | protected _y: number 15 | 16 | constructor(x: number, y: number) { 17 | super(Vector) 18 | this._x = x 19 | this._y = y 20 | } 21 | 22 | get x(): number { 23 | return this._x 24 | } 25 | set x(x: number) { 26 | this._x = x 27 | } 28 | 29 | get y(): number { 30 | return this._y 31 | } 32 | set y(y: number) { 33 | this._y = y 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "noImplicitAny": true, 5 | "declaration": true, 6 | "preserveConstEnums": true, 7 | "sourceMap": true, 8 | "target": "es5", 9 | "lib": [ 10 | "es2015" 11 | ], 12 | "strict": true, 13 | "removeComments": false 14 | }, 15 | "include": [ 16 | "src/*.ts" 17 | ] 18 | } 19 | --------------------------------------------------------------------------------