@mborecki/geo2d 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
1
|
+
import { describe, expect, it, test } from "vitest";
|
|
2
2
|
import { Vec2 } from "./vec2"
|
|
3
3
|
|
|
4
4
|
describe('Vec2', () => {
|
|
5
|
+
|
|
6
|
+
describe('contructor', () => {
|
|
7
|
+
test('x,y', () => {
|
|
8
|
+
const v = new Vec2(1, 2);
|
|
9
|
+
expect(v.x).toBe(1);
|
|
10
|
+
expect(v.y).toBe(2);
|
|
11
|
+
})
|
|
12
|
+
});
|
|
13
|
+
|
|
5
14
|
describe('getters', () => {
|
|
6
15
|
it('[0]', () => {
|
|
7
16
|
const v = new Vec2(1, 2);
|
|
@@ -52,4 +61,32 @@ describe('Vec2', () => {
|
|
|
52
61
|
expect(v.y).toBe(2);
|
|
53
62
|
})
|
|
54
63
|
})
|
|
64
|
+
|
|
65
|
+
describe('set()', () => {
|
|
66
|
+
it('number', () => {
|
|
67
|
+
const v = Vec2.from(0);
|
|
68
|
+
v.set(3);
|
|
69
|
+
expect(v.x).toBe(3);
|
|
70
|
+
expect(v.y).toBe(3);
|
|
71
|
+
})
|
|
72
|
+
it('array', () => {
|
|
73
|
+
const v = Vec2.from([1, 2]);
|
|
74
|
+
v.set([1, 2]);
|
|
75
|
+
expect(v.x).toBe(1);
|
|
76
|
+
expect(v.y).toBe(2);
|
|
77
|
+
})
|
|
78
|
+
it('object', () => {
|
|
79
|
+
const v = Vec2.from({ x: 1, y: 2 });
|
|
80
|
+
v.set({ x: 1, y: 2 });
|
|
81
|
+
expect(v.x).toBe(1);
|
|
82
|
+
expect(v.y).toBe(2);
|
|
83
|
+
})
|
|
84
|
+
it('object+', () => {
|
|
85
|
+
const obj = { x: 1, y: 2, id: 'foobar' }
|
|
86
|
+
const v = Vec2.from(obj);
|
|
87
|
+
v.set(obj);
|
|
88
|
+
expect(v.x).toBe(1);
|
|
89
|
+
expect(v.y).toBe(2);
|
|
90
|
+
})
|
|
91
|
+
})
|
|
55
92
|
})
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export interface Vec2ParsableObject {x: number, y: number}
|
|
1
|
+
export interface Vec2ParsableObject { x: number, y: number }
|
|
2
2
|
|
|
3
3
|
export type Vec2Source = number | number[] | Required<Vec2ParsableObject>;
|
|
4
4
|
|
|
5
5
|
export class Vec2 {
|
|
6
6
|
|
|
7
|
-
x: number;
|
|
8
|
-
y: number;
|
|
7
|
+
x: number = 0;
|
|
8
|
+
y: number = 0;
|
|
9
9
|
|
|
10
10
|
get 0(): number {
|
|
11
11
|
return this.x;
|
|
@@ -53,6 +53,21 @@ export class Vec2 {
|
|
|
53
53
|
this.y = y;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
set(source: Vec2Source) {
|
|
57
|
+
if (typeof source === 'number') {
|
|
58
|
+
this.x = source;
|
|
59
|
+
this.y = source;
|
|
60
|
+
} else if (Array.isArray(source)) {
|
|
61
|
+
this.x = source[0] ?? 0;
|
|
62
|
+
this.y = source[1] ?? 0;
|
|
63
|
+
} else if (typeof source === 'object' && typeof source.x === 'number' && typeof source.y === 'number') {
|
|
64
|
+
this.x = source.x;
|
|
65
|
+
this.y = source.y;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
56
71
|
add(v: Vec2): Vec2 {
|
|
57
72
|
return new Vec2(this.x + v.x, this.y + v.y);
|
|
58
73
|
}
|