Skip to content

Commit

Permalink
Refactor vec2.add, vec2.sub, vec2.mul, vec2.div
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Feb 18, 2024
1 parent 2157971 commit f3b9ccc
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ export namespace scalar {
nmax: number
) {
if (omax === omin) {
console.log('here')
return lerp(nmin, nmax, 0.5)
}

Expand Down
36 changes: 36 additions & 0 deletions src/vec2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@ 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', () => {
expect(vec2.divide()).toStrictEqual([1, 1])
expect(vec2.divide([0, 1])).toStrictEqual([0, 1])
expect(vec2.divide([2, 3], [4, 5])).toStrictEqual([2 / 4, 3 / 5])
expect(vec2.divide([2, 3], [4, 5], [6, 7])).toStrictEqual([
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
76 changes: 57 additions & 19 deletions src/vec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ 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 @@ -62,13 +68,18 @@ export namespace vec2 {
/**
* Add the given vectors
*/
export function add(...vs: vec2[]): vec2 {
export function add(...vs: OrNumber[]): vec2 {
let x = 0,
y = 0

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

return [x, y]
Expand All @@ -77,25 +88,36 @@ 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: vec2[]): vec2 {
export function subtract(...vs: OrNumber[]): vec2 {
if (vs.length === 0) {
return zero
}

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

const [first, ...rest] = vs

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

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

return ret
return [x, y]
}

/**
Expand All @@ -114,13 +136,18 @@ export namespace vec2 {
return [b[0] - a[0], b[1] - a[1]]
}

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

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

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

export function divide(...vs: vec2[]): vec2 {
export function divide(...vs: OrNumber[]): vec2 {
if (vs.length === 0) {
return one
} else if (vs.length === 1) {
}

if (vs.length === 1) {
return divide(one, vs[0])
} else if (vs.length > 2) {
const [a, b, ...rest] = vs
return divide(divide(a, b), ...rest)
}

const [a, b] = vs
const [first, ...rest] = vs

return [a[0] / b[0], a[1] / b[1]]
let [x, y] = 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]
}
}

return [x, y]
}

/**
Expand Down

0 comments on commit f3b9ccc

Please sign in to comment.