servo / euclid

Geometry primitives (basic linear algebra) for Rust
Other
462 stars 102 forks source link

Component-wise multiplication for Vector2 #475

Closed alexkirsz closed 3 years ago

alexkirsz commented 3 years ago

Hey,

I don't see an obvious way to do component-wise multiplication and division for Vector2-like types. Is this a deliberate API decision or just a use case that hasn't showed up?

Example usage:

let v1 = Vector2::new(1, 2);
let v2 = Vector2::new(3, 4);
let v3 = v1.component_mul(&v2); // x: 3, y: 8

See also: https://docs.rs/nalgebra/0.24.1/nalgebra/base/struct.Matrix.html#method.component_mul

Cheers, Alex

nical commented 3 years ago

Hi,

Sounds good to me. I suspect that the need hasn't shown up enough for someone to add the API but it makes sense.

I'm wondering if in addition to a properly explicit function like component_mul, adding a Mul operator would make sense. I'm used to having it in glsl.

alexkirsz commented 3 years ago

@nical Thanks for the quick implementation!

I think it would make sense to also have component-wise division, as well as component-wise ceiling division for integer types.

See: