Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2007-10-05 15:34:50


Michael Fawcett wrote:

>> I.e. point+point is undefined, point-point is a vector, etc...
>
> This has been the source of quite a bit of contention in the past.
> distinction is highly important, and then there are game developers
> (and others I'm sure) who frequently mix both for optimization tricks
> and compatibility with the graphics API.
>
Some folks in between those two groups argue that lineal combinations of
points in the affine space is also well defined, and that uses point
addition.

Thus, I typically choose to define:

- point -> point (reflexion)

scalar * point -> point (affine scaling)
point * scalar -> point (affine scaling)

point * matrix -> point ( affine transformation)

point + point -> point (lineal combination)

point - point -> vector (subtraction)

- vector -> vector (inversion)

scalar * vector ->
vector * scalar -> vector (scaling)

vector + vector -> vector (addition)

vector - vector -> vector (substraction)

point + vector -> point
point - vector -> point (displacement)

vector * matrix -> vector (lineal transfomation)

Which allow you to have your cacke and eat it too.

Of course you could say that (ptA - ptA) is not the same as (ptA + (-ptA) )
and that is a problem
(the former is a vector while the second is a point)

Yet OTOH both results have the same components, so, why would that be a
problem is a statically typed language?

Furthermore, I sometimes like to use subtyping as in:

cartesian <-- vector
              <-- point

which in conjuction with the the curiously recurring pattern (to inject the
"self" most-derived type into cartesian) can be used to define:

unary -
binary - + *

right in the base class.

(point-point) needs to be overriden in the point class though.

But of course, as Joel said, these sort of thing (inheritance) is unnecesary
when generic programming is _fully_ used.
But the inheritance comes in handy in non-generic but parametrized contexts
(where you can parametrize on "cartesian" and pass either points or vectors
without conversion)

Best

-- 
Fernando Cacciola
SciSoft
http://fcacciola.50webs.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk