-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a basic implementation for a 2D vector class
- Loading branch information
Showing
4 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const EPSILON = 0.00001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |