Skip to content

Commit

Permalink
Stop supporting vec2.OrNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Feb 18, 2024
1 parent f3b9ccc commit c6d2bc8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 56 deletions.
12 changes: 0 additions & 12 deletions src/vec2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,20 @@ test('add', () => {
expect(vec2.add([0, 1])).toStrictEqual([0, 1])
expect(vec2.add([0, 1], [2, 3])).toStrictEqual([2, 4])
expect(vec2.add([0, 1], [2, 3], [4, 5])).toStrictEqual([6, 9])

expect(vec2.add(1)).toStrictEqual([1, 1])
expect(vec2.add(1, 2)).toStrictEqual([3, 3])
})

test('subtract', () => {
expect(vec2.subtract()).toStrictEqual([0, 0])
expect(vec2.subtract([0, 1])).toStrictEqual([0, -1])
expect(vec2.subtract([0, 1], [2, 3])).toStrictEqual([-2, -2])
expect(vec2.subtract([0, 1], [2, 3], [4, 5])).toStrictEqual([-6, -7])

expect(vec2.subtract(1)).toStrictEqual([-1, -1])
expect(vec2.subtract(1, 2)).toStrictEqual([-1, -1])
})

test('multiply', () => {
expect(vec2.multiply()).toStrictEqual([1, 1])
expect(vec2.multiply([0, 1])).toStrictEqual([0, 1])
expect(vec2.multiply([0, 1], [2, 3])).toStrictEqual([0, 3])
expect(vec2.multiply([0, 1], [2, 3], [4, 5])).toStrictEqual([0, 15])

expect(vec2.multiply(1)).toStrictEqual([1, 1])
expect(vec2.multiply(2, 3)).toStrictEqual([6, 6])
})

test('divice', () => {
Expand All @@ -47,9 +38,6 @@ test('divice', () => {
2 / 4 / 6,
3 / 5 / 7,
])

expect(vec2.divide(1)).toStrictEqual([1, 1])
expect(vec2.divide(2, 3)).toStrictEqual([2 / 3, 2 / 3])
})

test('ceil', () => {
Expand Down
58 changes: 14 additions & 44 deletions src/vec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ export namespace vec2 {
*/
export type Mutable = [x: number, y: number]

/**
* Vector value used in arguments. If it is a number `x`, it will be regarded as `[x, x]`
* @category Types
*/
export type OrNumber = vec2 | number

/**
* Creates a new vector from given elements
* @category Generators
Expand Down Expand Up @@ -68,18 +62,13 @@ export namespace vec2 {
/**
* Add the given vectors
*/
export function add(...vs: OrNumber[]): vec2 {
export function add(...vs: vec2[]): vec2 {
let x = 0,
y = 0

for (const v of vs) {
if (typeof v === 'number') {
x += v
y += v
} else {
x += v[0]
y += v[1]
}
x += v[0]
y += v[1]
}

return [x, y]
Expand All @@ -88,18 +77,14 @@ export namespace vec2 {
/**
* Subtracts the given vec2's. When the argument is a single vector, it negates it. Otherwise, it subtracts from left to right.
*/
export function subtract(...vs: OrNumber[]): vec2 {
export function subtract(...vs: vec2[]): vec2 {
if (vs.length === 0) {
return zero
}

if (vs.length === 1) {
const v = vs[0]
if (typeof v === 'number') {
return [-v, -v]
} else {
return [-v[0], -v[1]]
}
return [-v[0], -v[1]]
}

const [first, ...rest] = vs
Expand All @@ -108,13 +93,8 @@ export namespace vec2 {
typeof first === 'number' ? [first, first] : [...first]

for (const v of rest) {
if (typeof v === 'number') {
x -= v
y -= v
} else {
x -= v[0]
y -= v[1]
}
x -= v[0]
y -= v[1]
}

return [x, y]
Expand All @@ -136,18 +116,13 @@ export namespace vec2 {
return [b[0] - a[0], b[1] - a[1]]
}

export function multiply(...vs: OrNumber[]): vec2 {
export function multiply(...vs: vec2[]): vec2 {
let x = 1,
y = 1

for (const v of vs) {
if (typeof v === 'number') {
x *= v
y *= v
} else {
x *= v[0]
y *= v[1]
}
x *= v[0]
y *= v[1]
}

return [x, y]
Expand All @@ -162,7 +137,7 @@ export namespace vec2 {
*/
export const mul = multiply

export function divide(...vs: OrNumber[]): vec2 {
export function divide(...vs: vec2[]): vec2 {
if (vs.length === 0) {
return one
}
Expand All @@ -173,16 +148,11 @@ export namespace vec2 {

const [first, ...rest] = vs

let [x, y] = typeof first === 'number' ? [first, first] : [...first]
let [x, y] = first

for (const v of rest) {
if (typeof v === 'number') {
x /= v
y /= v
} else {
x /= v[0]
y /= v[1]
}
x /= v[0]
y /= v[1]
}

return [x, y]
Expand Down

0 comments on commit c6d2bc8

Please sign in to comment.