romgrk / 2d-geometry

Performant & ergonomic 2D geometry in Typescript
MIT License
30 stars 2 forks source link

Feature Request: Add mutating methods on vectors #1

Open davjhan opened 2 months ago

davjhan commented 2 months ago

It would be great to have a way to do vector operations without allocating more memory. It's very common when doing graphical programming to have lines like:

vector(x,y).sub(...).normalize().mulitply(...)

It would be great to be able to write this more performantly as:

vector(x,y).subMut(...).normalizeMut().mulitplyMut(...)

Approach 1

Name the mutable methods in the same way as the rest of the lib: .subMut or .subtractMut, addMut

Approach 2

Matter.js takes in as input an output vector to assign the result. https://brm.io/matter-js/docs/classes/Vector.html

romgrk commented 2 months ago

I'm not sure if I should add mutable methods for everything though, I'm trying to balance performance and bundle size, and I'm currently already unhappy with the bundle size. I've been adding mutable & non-allocating methods to the hot paths that I found while implementing pencil, but Vector wasn't used very much compared to Point and Matrix so I've focused my efforts there.

The approach 2 is interesting though, I'll think about it.