maath icon indicating copy to clipboard operation
maath copied to clipboard

add(buffer): Normalize

Open gsimone opened this issue 3 years ago • 0 comments

normalize(buffer, size)

const myRandoms = pipe(
   new Float32Array(10_000 * 3),
   b => inSphere(b),
   b => normalize(b, 3),
  b => map(b, 
)

impl:

const normalize = (buffer: TypedArray, stride: number) => {
  for (let i = 0; i < buffer.length; i += stride) {
    const x = buffer[i + 0];
    const y = buffer[i + 1];
    const z = buffer[i + 2];
    const l = Math.sqrt(x * x + y * y + z * z);
    buffer[i + 0] = x / l;
    buffer[i + 1] = y / l;
    buffer[i + 2] = z / l;
  }
  return buffer;
};

gsimone avatar Dec 08 '22 10:12 gsimone