leftiness / hex_math

MIT License
4 stars 1 forks source link

Figure out point iterations #49

Closed leftiness closed 8 years ago

leftiness commented 8 years ago

I wrote a HasValues#into_vec(). I'm not so sure that's the proper solution here.

For example, the work I'm doing for ray() has a step where I need to do A + B * C where A: i32, B: f32, C: f32 for each element of three structs (A, A, A, A), (B, B, B, B), (C, C, C, C). So A0 + B0 * C0, A1 + B1 * C1, etc.

I can't use HasValues#into_vec() here even if I wanted to. This isn't a HasValues. It's a tuple of f32.

Here's some primitive work:

pub fn merge<T, U, V, W>(
  a: (T, T, T, T),
  b: (U, U, U, U),
  op: V,
) -> (W, W, W, W) where V: Fn(T, U) -> W {
  (op(a.0, b.0), op(a.1, b.1), op(a.2, b.2), op(a.3, b.3))
}

pub fn map<T, U, V>(
  a: (T, T, T, T),
  op: U,
) -> (V, V, V, V) where U: Fn(T) -> V {
  (op(a.0), op(a.1), op(a.2), op(a.3))
}

pub fn main() {

  let a = (1, 1, 1, 1);
  let b = (2.5f32, 1f32, 2.5f32, 2.5f32);
  let op = |x, y| x as f32 == y;
  let result = merge(a, b, op);

  println!("{:?}", result);

  println!("{:?}", map(a, |x| x as f32 + 1.5f32));

}

I don't like that either. I'd have to write a lot of functions, and I'd have to duplicate the work for tuples of three, tuples of four... Just no.

So maybe a HasValues should be different. It could be a struct which is backed by a private vector. I don't want someone adding or removing or even altering values in this vector. If I give someone access to it, I would just provide an iterator and let them do map(), collect(), etc.

This HasValues would need to be HasValues<T>. It would impl From<(T, T, T, T)>.

Maybe it wouldn't even be the HasValues trait but maybe more of a struct. It could still implement HasValues. So... maybe this work would be done on the Point struct. With the ability to provide a function for mapping, I could iterate over a Point<f32>, convert from f32 to i32, and collect back into a Point<i32>.

Idk though. I kind of appreciate the idea of having a Point.q property, but at the same time I've been using point.values() and stuff like that. I'm not even using that Point.q property.

leftiness commented 8 years ago

Arrays. Arrays are obviously the answer. An array is an immutable collection of values with a length defined in the type declaration. All elements of an array are the same type. You can array.iter().map(). point.array().iter().map(). impl From<[i32;3]> for Point.

leftiness commented 8 years ago

asdf. Asdf! You can't collect back into an array because array doesn't implement Iterator. I just spent a bunch of time writing some array functions for HasValues, and it doesn't work. You collect back into a vector. I'd still be stuck writing functions to check vector sizes and manually convert from vec to point.

Big sigh. Maybe I can avoid the worst of the problem with #52.

leftiness commented 8 years ago

Yeah. It's not as bad now: https://github.com/leftiness/hex_math/commit/0f811d43642690d93ac129857f332c7964d8de16?diff=split

leftiness commented 8 years ago

Closing. Issue is circumvented.