Points and Vectors in 3D
Simon
s.d.hammett at gmail.com
Sat Mar 12 14:20:30 PST 2011
On 12/03/2011 20:51, Caligo wrote:
> Given everything that D offers, what would be the best way to implement
> a Point and a Vector type? The same (x, y, z) can be used to represent
> vectors, but a point represents a position, whereas a vector represents
> a direction. So, would you define two different structs for each? or
> define and implement an interface? a fixed array or POD members?
I've done lots of 3d over the years and used quite a lot of different
libraries and I've come to prefer code that makes a distinction between
points and vectors. Makes code easier to read and more type safe, though
it's a bit more inconvenient when you need to mix things up.
I use:
struct pt {
float[3] _vals;
}
struct vec {
float[3] _vals;
}
Using the float[3] allows you to use vector ops:
pt p0;
vec v;
p0._vals[] += v._vals[];
You don't want an interface; you don't get anything more value type than
points & vectors. In a modern 3d models you could be dealing with a 1/2
million vertices.
--
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
More information about the Digitalmars-d-learn
mailing list