Skip to content

Commit

Permalink
Fix *.sub and *.mul
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Feb 18, 2024
1 parent c6d2bc8 commit df30e11
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
5 changes: 2 additions & 3 deletions src/vec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ export namespace vec2 {
}

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

const [first, ...rest] = vs
Expand Down Expand Up @@ -143,7 +142,7 @@ export namespace vec2 {
}

if (vs.length === 1) {
return divide(one, vs[0])
return [1 / vs[0][0], 1 / vs[0][1]]
}

const [first, ...rest] = vs
Expand Down
23 changes: 15 additions & 8 deletions src/vec3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export namespace vec3 {
}

if (vs.length === 1) {
return [-vs[0], -vs[1], -vs[2]]
return [-vs[0][0], -vs[0][1], -vs[0][2]]
}

const [first, ...rest] = vs
Expand Down Expand Up @@ -142,16 +142,23 @@ export namespace vec3 {
export function divide(...vs: vec3[]): vec3 {
if (vs.length === 0) {
return one
} else 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
if (vs.length === 1) {
return [1 / vs[0][0], 1 / vs[0][1], 1 / vs[0][2]]
}

return [a[0] / b[0], a[1] / b[1], a[2] / b[2]]
const [first, ...rest] = vs

let [x, y, z] = first

for (const v of rest) {
x /= v[0]
y /= v[1]
z /= v[2]
}

return [x, y, z]
}

/**
Expand Down
25 changes: 16 additions & 9 deletions src/vec4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export namespace vec4 {
}

if (vs.length === 1) {
return [-vs[0], -vs[1], -vs[2], -vs[3]]
return [-vs[0][0], -vs[0][1], -vs[0][2], -vs[0][3]]
}

const [first, ...rest] = vs
Expand Down Expand Up @@ -153,18 +153,25 @@ export namespace vec4 {
export function divide(...vs: vec4[]): vec4 {
if (vs.length === 0) {
return one
} else 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
if (vs.length === 1) {
return [1 / vs[0][0], 1 / vs[0][1], 1 / vs[0][2], 1 / vs[0][3]]
}

return [a[0] / b[0], a[1] / b[1], a[2] / b[2], a[3] / b[3]]
}
const [first, ...rest] = vs

let [x, y, z, w] = first

for (const v of rest) {
x /= v[0]
y /= v[1]
z /= v[2]
w /= v[3]
}

return [x, y, z, w]
}
/**
* symmetric round the components of a vec4
*/
Expand Down

0 comments on commit df30e11

Please sign in to comment.