coreolyn has asked for the wisdom of the Perl Monks concerning the following question:

In all honesty this is where the music major in me regrets having such problems with math. I was reading reordering segments to form a polygon this morning and it scratched an old itch that I have tried many times in the past to itch. What I'd like to find is the most elegant way to express perfect hexagons from data. I have always marveled at the hex since the old Avalon Hill Bookshelf games and held a facination with them. Looking at the quick elagent responses to that node I was wondering if some 'big brain' around here could do that in a snap as it would be most appreciated.

As this expresses laziness not of the Perlish kind downvotes would be understood.

  • Comment on In search of the perfect digital hexagon

Replies are listed 'Best First'.
Re: In search of the perfect digital hexagon
by japhy (Canon) on Aug 13, 2004 at 13:02 UTC
    I'm not sure I understand the question... but, if you're dealing with a "perfect" hexagon (I believe you mean one with equal sides and equal angles (120 degrees)), then I think the simplest way to describe that hexagon in 2-d space would be with four values (xc, yc, ls, θ):
    • xc is the x-coordinate of the center of the hexagon
    • yc is the y-coordinate of the center of the hexagon
    • ls is the length of a side of the hexagon
    • θ is the angle (measured from the x-axis) of the tilt of the hexagon
    A tilt of 0 degrees would mean the hexagon looks like:
    _ / \ \_/
    where a tilt of 90 degrees would mean the hexagon looks like:
    /\ || \/
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: In search of the perfect digital hexagon
by BrowserUk (Patriarch) on Aug 13, 2004 at 13:01 UTC

    Perhaps you could clarify what you mean by "express perfect hexagons from data"?

    A quick google for "Perfect hexagons" doesn't turn up anything that immediately leaps off the page as a "well know definition".


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: In search of the perfect digital hexagon
by pbeckingham (Parson) on Aug 14, 2004 at 01:20 UTC

    I am shamelessly ripping off fletch's work here, but the following (hexagonal) grid:

    -8 . . . - - -8 -7 . . . . - ^ -7 -6 . . . . - - - - -6 -5 . . - . a a - ^ ? ? - -5 -4 . . . . k o ! - ^ ? ? ? -4 -3 . - . a h j a ^ ^ ^ ^ ^ - -3 -2 . . - . a a a ^ a a - - - -2 -1 . . . a a a b + + ^ ^ - -1 0 . . . m c u a a - ^ - ^ 0 1 . - . a a . a a - - ^ 1 2 - ^ ^ a a a ^ a - - ^ 2 3 - - - - - - - . . - 3 4 ^ - - - - - ^ - - 4 5 - ^ - - ^ - - - 5
    Is actually the same grid as this rectangular grid:
    -8 ...-- -8 -7 ....-^ -7 -6 ....---- -6 -5 ..-.aa-^??- -5 -4 ....ko!-^??? -4 -3.-.ahja^^^^^- -3 -2 ..-.aaa^aa--- -2 -1 ...aaab++^^- -1 0 ...mcuaa-^-^ 0 1 .-.aa.aa--^ 1 2 -^^aaa^a--^ 2 3 -------..- 3 4 ^-----^-- 4 5 -^--^--- 5
    It is just a question of
    1. How the grid is rendered.
    2. how a cell's neighboring cells are determined.
    Once the data structure is reduced to rectangular, the mystery of the hexagon disappears. It is this kind of thinking that reduces a checkerboard to an 8 x 4 board, for certain games.



    pbeckingham - typist, perishable vertebrate.
Re: In search of the perfect digital hexagon
by coreolyn (Parson) on Aug 13, 2004 at 14:34 UTC

    Thanks for the responses. I'll attempt to clarify.

    All of the responses are relevant and have given me things to play with and research. What I keep trying to express in code is 3 dimensional space. Much like a 10 sided dice in D&D hexagons fill space perfectly. Additionally smaller hexes fill larger hexes perfectly. So what I'm looking to do is figure a coordinate system for defining what is in a space by getting code to scale space via hexagonal scaling. The crux of this is figuring the best algorithm for representing and storing a hex ( as japhy pointed out ) with 120 degree angles.

    Update -> After finding the most efficient hex comes translating this type of 2D thinking into 3D. :)

      It sounds like what you're looking for is regular solid tilings. There's actually a large body of data on the subject. Try googling for regular tiling, or take a look at some math reference sites.

        Ouch, but yeah.. thanks for the leads.

Re: In search of the perfect digital hexagon
by water (Deacon) on Aug 13, 2004 at 13:06 UTC
    Also not sure what you mean by "express perfect hexagons", but the reference to board games makes me think of a using a graph (vertices V_1... V_n, adjacency matrix A_ij=1 when edge between V_i and V_j) to represent the 6 neighbors of each cell.

      Another way is to use a two dimensional array (matrix), but only some locations are valid. Empire uses a scheme sort of like this:

          ---------0000000000111111111
          9876543210123456789012345678
       -8        . . . - -             -8
       -7       . . . . - ^            -7
       -6    . . . . - - - -           -6
       -5   . . - . a a - ^ ? ? -      -5
       -4  . . . . k o ! - ^ ? ? ?     -4
       -3 . - . a h j a ^ ^ ^ ^ ^ -    -3
       -2  . . - . a a a ^ a a - - -   -2
       -1   . . . a a a b + + ^ ^ -    -1
        0  . . . m c u a a - ^ - ^     0
        1   . - . a a . a a - - ^      1
        2    - ^ ^ a a a ^ a - - ^     2
        3     - - - - - - - . . -      3
        4      ^ - - - - - ^ - -       4
        5       - ^ - - ^ - - -        5
          ---------0000000000111111111
          9876543210123456789012345678
      

      (From the docs for the map command)

      To move east or west you use an increment of two to the x index (i.e. west would be ( x - 2, y )); north / south is forbidden in this scheme since the faces are lined up W/NW/NE/E/SE/SW, although that's by convention and there's nothing saying you couldn't rule out east / west by adding to y instead (and in effect rotating the hex grid 90 degrees). To move any of the other four directions you add/subtract one to both coordinates (northeast would be ( x + 1, y - 1)). You can easily verify if a coordinate pair's valid as both x and y have to be both odd or both even.

      If this is anywhere near what you were asking about you might take a gander at PEI, the Perl Empire Interface, which has code for dealing with maps of this type.