Skip to content

Commit

Permalink
feat(vectors): Added Vector2 class
Browse files Browse the repository at this point in the history
Added a basic implementation for a 2D vector class
  • Loading branch information
aszecsei committed Nov 11, 2017
1 parent 37cfac7 commit 287c152
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const EPSILON = 0.00001
2 changes: 1 addition & 1 deletion src/ts-vector-math.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Import here Polyfills if needed. Recommended core-js (npm i -D core-js)
// import "core-js/fn/array.find"
// ...
export default class DummyClass {}
export * from './vec2'
32 changes: 32 additions & 0 deletions src/vec2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { EPSILON } from './common'

export class Vector2 {
public x: number
public y: number
get xy(): number[] {
return [this.x, this.y]
}
set xy(values: number[]) {
this.x = values[0]
this.y = values[1]
}
constructor(values?: number[]) {
if (values) {
this.xy = values
}
}
copy(dest?: Vector2): Vector2 {
if (!dest) dest = new Vector2()
dest.xy = this.xy
return dest
}
equals(other: Vector2, threshold = EPSILON): boolean {
if (Math.abs(this.x - other.x) > threshold) {
return false
}
if (Math.abs(this.y - other.y) > threshold) {
return false
}
return true
}
}
65 changes: 59 additions & 6 deletions test/ts-vector-math.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,67 @@
import DummyClass from '../src/ts-vector-math'
import { Vector2 } from '../src/ts-vector-math'

/**
* Dummy test
*/
describe('Dummy test', () => {
it('works if true is truthy', () => {
expect(true).toBeTruthy()
describe('Vector2', () => {
it('is instantiable', () => {
expect(new Vector2()).toBeInstanceOf(Vector2)
})

it('DummyClass is instantiable', () => {
expect(new DummyClass()).toBeInstanceOf(DummyClass)
it('can be set through swizzle', () => {
let v1 = new Vector2()
v1.xy = [1, 2]
let v2 = new Vector2()
v2.x = 1
v2.y = 2
expect(v1).toEqual(v2)
})

it('can be retrieved through swizzle', () => {
let v1 = new Vector2()
v1.xy = [1, 2]
expect(v1.xy).toEqual([1, 2])
})

it('detects non-equality', () => {
expect(new Vector2([1, 2])).not.toEqual(new Vector2([1, 1]))
expect(new Vector2([1, 2])).not.toEqual(new Vector2([2, 2]))
})

it('copies values', () => {
let v1 = new Vector2([1, 1])
let v2 = v1.copy()
v2.xy = [2, 2]
expect(v1).not.toEqual(v2)
})

it('copies values to a provided destination', () => {
let v1 = new Vector2([1, 1])
let v2 = new Vector2([1, 2])
let v3 = v1.copy(v2)
expect(v2).toEqual(v2)
expect(v1).toEqual(v2)
expect(v1).toEqual(v3)
v3.y = 3
expect(v2).toEqual(v3)
expect(v1).not.toEqual(v2)
expect(v1).not.toEqual(v3)
})

it('tests equality correctly', () => {
let v1 = new Vector2([1, 1])
let v2 = new Vector2([1, 2])
expect(v1.equals(v2)).toBeFalsy()
v2.xy = [2, 1]
expect(v1.equals(v2)).toBeFalsy()
v2.xy = [1, 1]
expect(v1.equals(v2)).toBeTruthy()
})

it('tests equality for a given threshold', () => {
let v1 = new Vector2([1, 1])
let v2 = new Vector2([2, 2])
expect(v1.equals(v2)).toBeFalsy()
expect(v1.equals(v2, 2)).toBeTruthy()
})
})

0 comments on commit 287c152

Please sign in to comment.