1

Okay so I have these images:

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

Basically what I'm trying to do is to create a "mosaic" of about 5 to 12 hexagons, with most of them roughly centralised, and where all of the lines meet up.

For example:

enter image description here enter image description here enter image description here

I'm aware that I could probably just brute-force it, but as I'm developing for Android I need a faster, more efficient and less processor-intensive way of doing it.

Can anybody provide me with a solution, or even just point me in the right direction?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Ben Ezard
  • 463
  • 6
  • 18

2 Answers2

1

Nice and tricky question. What you can start with is define object for each image which has attributes that specify which edge has a line attached to it. Then while adding the images in the layout you can rotate it in such a way that the edge with line in one image lies adjacent to the other image's edge with line. It may be little complicated but I hope you can at least start with something like this.

Deepak Senapati
  • 1,103
  • 2
  • 11
  • 30
  • Thanks, I was thinking of starting with this However, other problems I'm facing are things like fitting it all on a phone screen (so the whole thing can't be more than about 4 hexagons across), and also getting the algorithm to terminate (i.e. making all the lines completely join up) – Ben Ezard Jul 11 '13 at 09:15
  • welcome :) please do remember to upvote the answer or accept the answer if you find it to be useful in anyway :) – Deepak Senapati Jul 11 '13 at 09:18
1

A random idea that I had is to go with what Deepak said about defining a class that tracks the state of each of its six edges (say, in an int[] neighbor in which neighbor[0] states if top edge has neighbor, neighbor[1] states if top-right edge has neighbor, and so on going clockwise)

Then for each hexagon on screen, convert its array to an integer via binary. Based on that integer, use a lookup table to determine which hexagon image to use + how it should be oriented/flipped, then assign that hexagon object to that image.

For instance, let's take the central hexagon with four neighbors in your first screenshot. Its array would be [1, 0, 1, 1, 0, 1] based on the scheme mentioned above. Take neighbor[0] to be the least-significant bit (2^0) and neighbor[5] to be the most-significant bit (2^5), and we have [1, 0, 1, 1, 0, 1] --> 45. Somewhere in a lookup table we would have already defined 45 to mean the 5th hexagon image, flipped horizontally*, among the seven base hexagon icons you've posted.

Yes, brute-force is involved, but it's a "smarter" brute-force since you're not rotating to see if a hexagon will fit. Rather, it involves a more efficient look-up table.

*or rotated 120 degrees clockwise if you prefer ;)

  • Changed this to my accepted answer as it gives a lot more detail. I'll have a go of this and see how I get on, thanks :) – Ben Ezard Jul 11 '13 at 21:09