Then tell me, computergeek, how can you detect intersection between two circles without knowing they're both circles?
Each shape (child class) needs to be handled differently. And each interaction needs its own logic.
Your previous suggestion of "making an array of points available in the base class" is one approach... but that would be extremely slow and clunky... and would restrict you to a finite, discrete coordinate system, rather than being able to work with real numbers.
Compare the logic for whether or not 2 circles intersect:
bool circleIntersect( Circle a, Circle b )
auto vec = a.center - b.center;
auto distance2 = (vec.x * vec.x) + (vec.y * vec.y);
auto radius = a.radius + b.radius;
return distance2 <= (radius*radius);
To the logic of:
- Generating a finite list of discrete points in a massive array.
- Checking each and every one of those points to see if they exist inside of another circle.
|You can map pairs of std::type_index to std::functions rather easily, and the map can be generated by global static initialization without any one part of the code having to know about all the classes.|
Ultimately you're going to have to have a series of functions which cover every permutation of all shapes' intersections. So at least that
part of the code will need to be aware of all classes.
Building this map automatically is also a challenge without a centralized area knowing all the classes. It doesn't seem to be as easy as you make it sound.
Whenever the N'th class is added... you have to add (at least) N permutations to the map. How can you do generate permutations without knowing what classes you're dealing with?
Ex: If I already have Square and Triangle functions set up.... when I add a 3rd class Circle... I have to add at least 3 more functions (OP said he will actually need 5):
Circle/Square (need to be aware of Square's existence)
Circle/Triangle (need to be aware of Triangle's existence)
Square/Circle (OP said order is important)
If you have a way to automate this that doesn't require maintaining a central list... that is exactly
what the OP is asking for. I don't have a good solution. If you do, I've love to hear it, as I'm interested in this as well.