Advertisement

Hexagon game, holes in a region

Started by February 02, 2020 05:52 PM
16 comments, last by Alberth 4 years, 8 months ago

Hey @Alberth , I'm sorry I didn't mean for it to come across as criticism.

Your contribution was very helpful in solving my problem, and I clicked ‘like’ on your response after reading it.

I too would have liked a solution that works for an infinite map, or an infinitely wrapping map, and for that reason I didn't make it clear that the map could be of a predetermined size.

You are absolutely right, the assumptions we can make are often problem-specific. In the end I implemented a solution which works for fixed sized maps, and makes use of the fact that an area which contains a boundary tile cannot be a hole.

All the responses I got were essentially the same approach, (flood-fills, determining whether a tile is inside or outside) and your initial response definitely got me and others thinking in the right direction, so again, thanks for your valuable input.

Here's a relatively simple solution that works will with infinite maps. Pick a tile bordering your input and walk around the border of your input until you get pack to the tile you picked. This will form a ring-like shape. Keep track of the rings, and repeat this until all tiles bordering your input are assigned to one such ring. Now sort your rings by length. The longest ring is around the outside of your input region, and all others are holes which you can fill in using a flood-fill algorithm.

Performance should be no worse than O(n), where n is the size of your input, so this also works well for maps that are not technically infinite but just very large. Just pretend that the map extends past its actual borders for the purpose of the algorithm.

Advertisement

a light breeze said:
Here's a relatively simple solution that works will with infinite maps. Pick a tile bordering your input and walk around the border of your input until you get pack to the tile you picked.

Walking around on an infinite map takes infinitely many steps, ie you never complete the first ring.

Also, there will be infinitely many rings, so you never find all of them.

@Alberth I don't think that's true. I think he's talking about identifying and iterating through all the tiles in the input set that have a neighbour not in the set, and foreach: walking around in a ring (probably by rotating in one direction through neighbours from the index last used until another tile in the set is found then moving on to that next tile until the ring is complete) storing them in a list, then starting a new ring from the next edge tile that hasn't already been flagged in a ring. I've not actually implemented this, but it sounds like a completely plausible and effective solution.

Edit: Just thinking about this again - there could be some edge tiles that exist in more than one ring, so you probably couldn't flag and skip previously visited tiles.

dan4 said:
I don't think that's true. I think he's talking about identifying and iterating through all the tiles in the input set

What's an infinite map then? Don't you have infinitely many hexagons then?

@Alberth An infinite map is a map that could theoretically contain a tile at any given coordinate. The ‘input set’ is the region, or if you look at the original image I posted, the set of red tiles. Which is of a finite size.

Advertisement

ah, ok, thanks for clarifying that.

This topic is closed to new replies.

Advertisement