Draw/Calculate Area

Hey,

hope you have an idea for my problem.
I want to create a game like “zoo tycoon”, where you place your fences to draw an compound.
Here is my problem, how can I calculate/detect, that I have a closed compound without missing fences?

I need to calculate that so that the animals only search for food in that compound.

Any idea how I can archive this?

Thanks alot!

You will have to have each fence piece keep track of its neighbors. Then it’s a simple depth first search to determine if you have a closed compound.

Basically (I’ve not tested this, just a quick off-the-top-of-my-head recrusion):

public bool IsClosed(){
    return CheckRecursively(this, this);
}

public bool CheckRecursively(Fence startingFencePiece, Fence previousFencePiece){
    foreach(Fence fence in neighboringFences){
        if(fence == previousFencePiece) continue;
        if(fence == startingFencePiece)
            return true;
        else
            return fence.CheckRecursively(startingFencePiece, this);
    }
    return false;
}

If you can have loops in in your fence, then this depth first search algorithm will probly hang your computer, but luckily there are other algorithms.

Once you have determined that it is a closed compound, you can determine what area is inside the compound. If your areas are rectangluar, then by finding the upper left and lower right corners you will know the area.