Search code examples
c#functioninterfaceproductcartesian

Cartesian product of functions in classes implementing one interface


I cant even describe my issue in title. So I'll try to explain it properly now.

Let's assume i have theses classes:

class Circle:IInteractable
{
     bool Intersects(Line line)...
     bool Intersects(Square line)...
     bool Intersects(Circle line)...        
}

class Square:IInteractable
{
     bool Intersects(Line line)...
     bool Intersects(Square line)...    
     bool Intersects(Circle line)...      
}

class Line:IInteractable
{
     bool Intersects(Line line)...
     bool Intersects(Square line)...   
     bool Intersects(Circle line)...       
}

So, you can see I want to make Cartesian product of intersects function to catch all possibilities.

I want to put Circles, Squares and Lines into one data structure. So, I've decided to create interface:

interface IInteractable
{
     bool Intersects(Line line);
     bool Intersects(Square line);   
     bool Intersects(Circle line); 
}

Now, for example I make List of some IInteractable objects:

List<IInteractable> bodies = new List<IInteractable>();
bodies.Add(new Circle());
bodies.Add(new Line());
bodies.Add(new Circle());
bodies.Add(new Square());
bodies.Add(new Line());

And I need find out if some objects are intersecting. So, I'am going to do...

        foreach ( IInteractablea in bodies)
            foreach ( IInteractable b in bodies)
                if( a != b)
                    if(a.Intersects(b))
                    // And here is the issue, because I have Intersects function for
                    // all object implementing IInteractable, but now i dont
                    // know with what type I am dealing with in parameter.
                    {
                    ...
                    }

So, it seems like good idea add flowing function to the interface:

bool Intersects(IInteractable obj);

I have all function Intersects() in each class implementig IInteractable. So it looks easy to make function above in every class. But then again I have to somehow find out,the type I am dealing with, to call appropriate function. That means, I need to switch according to types, but switch does not allow that. So I can still use if... else if else ... but it's quite monstrous.

My question is: Is there any better way to solve my problem ?

Edited: My solution:

I am blind and stupid. Now, I see the solution, dont know if it is optimal, but it is for sure better then the previous one.

In the interface put :

bool Intersects(IInteractable obj);

and in each class

    public virtual bool Intersects(IInteractable obj)
    {
        return obj.Intersects(this);
    }

no cast required :)


Solution

  • This is a computational problem similar to collision detection in video games. If it were me, I'd have an Intersects method on your interface. Internally, you can start naively by having on object ask the other if it has any points within its own space. You can optimize this by having a rectangular bounding box for each item, which makes calculation easy since everything is calculated against the four corners of the boxes. Shapes that are not rectangular can then do more fine grained testing only on potential intersects that are known to be inside the bounding box. It might be useful to expose, at least internally, a sorted array of points occupied by the shape's boundary to aid in that check as well as some indicator if the shape is open (line, arc) or closed (circle, square)