B
Brian Byrne
Happy Thanksgiving everyone,
I have a collection of various simple geometric objects (point, ray,
cube, box, sphere, pyramid, etc) and I want to write (fast, and thus
specific) functions that returns whether two objects intersect. I am
unsure how to structure this setup.
I see it in two basic ways. First, in each class I could add a member
function:
class Ray {
...
bool Intersects( const Box& box ) { ... }
bool Intersects( const Sphere& sphere ) { ... }
bool Intersects( ... ) { ... } /* Rest of objects */
...
};
Or I could place all "bool Intersects( ..., ... )" functions in a
single file for all pairs:
bool Intersects( const Ray& ray, const Box& box ) { ... }
....
bool Intersects( const Box& box, const Sphere& sphere ) { ... }
....
My number of geometric objects is limited so that writing all (N choose
2) functions is managable.
The first way seems more advantageous if each geometric object had a
common subclass (is this true?), but it seems messier because every
geometric class has to be able to "see" every other geometric class.
For example, adding a cone class would require me to edit every single
file. The second way seems more abstract in that each object need not
know of the others and all intersection code is centralized into a
single area.
Is there a better, solid, or more convectional way of doing this?
Thanks in advance,
Brian Byrne
I have a collection of various simple geometric objects (point, ray,
cube, box, sphere, pyramid, etc) and I want to write (fast, and thus
specific) functions that returns whether two objects intersect. I am
unsure how to structure this setup.
I see it in two basic ways. First, in each class I could add a member
function:
class Ray {
...
bool Intersects( const Box& box ) { ... }
bool Intersects( const Sphere& sphere ) { ... }
bool Intersects( ... ) { ... } /* Rest of objects */
...
};
Or I could place all "bool Intersects( ..., ... )" functions in a
single file for all pairs:
bool Intersects( const Ray& ray, const Box& box ) { ... }
....
bool Intersects( const Box& box, const Sphere& sphere ) { ... }
....
My number of geometric objects is limited so that writing all (N choose
2) functions is managable.
The first way seems more advantageous if each geometric object had a
common subclass (is this true?), but it seems messier because every
geometric class has to be able to "see" every other geometric class.
For example, adding a cone class would require me to edit every single
file. The second way seems more abstract in that each object need not
know of the others and all intersection code is centralized into a
single area.
Is there a better, solid, or more convectional way of doing this?
Thanks in advance,
Brian Byrne