Search
As of October, 2016, Embarcadero is offering a free release
of Delphi (Delphi
10.1 Berlin Starter Edition ). There
are a few restrictions, but it is a welcome step toward making
more programmers aware of the joys of Delphi. They do say
"Offer may be withdrawn at any time", so don't delay if you want
to check it out. Please use the
feedback
link to let me know if the link stops working.
Support DFF  Shop
If you shop at Amazon anyway, consider using
this link. We receive a few cents from each purchase.
Thanks.
Support DFF  Donate
If you benefit from the website, in terms of
knowledge, entertainment value, or something otherwise useful,
consider making a donation via PayPal to help defray the
costs. (No PayPal account necessary to donate via credit
card.) Transaction is secure.
Contact
Feedback:
Send an
email with your comments about this program (or anything else).

 
Problem Description
Draw hexagonal patterns for use as game or puzzle boards using regular
hexagonal tiles.
Background & Techniques
Being
known to the family as a puzzle lover, I received one called "Aristotle's Number
Puzzle" for Christmas this year  nothing to do with Aristotle as far as I can
tell. It's a hexagon shaped board made up of of 19 hexagon shaped tiles numbered
1 through 19. The objective is to arrange them into a "magic" hexagon in
which values in every horizontal and diagonal row (15 in all) sum to 38.
Our "Brute Force" program can
find the 12 variations of the solution by defining 15 equations representing
sums for the 19 variables . These are all isomorphic versions of the single
solution, i.e. they represent rotations and mirroring of the solution. I decided
to investigate further, starting by figuring out how to draw the boards.
This program does that in various sizes and with other size and orientation
parameters. If we number boards based on the number of tiles, N, on one outside
edge, then the total number of tiles in the board is 3N^2  3N +1 and the length
of the longest horizontal or diagonal row is 2N+1. Search "Centered Hexagonal
Number" and/or "Magic Hexagons" for more information.
Here's
a puzzle board with solution hints if you want to work on it offline.
The program has about 200 lines of code, making it of intermediate
difficulty. The key problems to be solved included :
 Scaling individual hexagon tiles with specified size and
orientation. Procedure MakeHexagon creates a TPolygon type as a 6 entry
array of points representing the corners of the hexagon with given center
point, radius, and angle of one vertex from horizontal. The other 5
corners are each increased 60 degrees from the previous corner.
 Drawing tiles in appropriate position positions. For the board
with a long horizontal row. the number of rows is the same as the number of
tile in the longest row. We'll step across one radius length at
a time and for each position step up and down by the a radius
decreased by amount of overlap with the next row [sin(30 degrees) = 0.866].
 Drawing a version of the board rotated by 30 degrees so that the longest
diagonal becomes the longest column requires rotating each individual hex
tile forward (or backward) by 30 degrees and by interchanging the X, Y
roles in the drawing algorithm from the previous so that the long
vertical column is drawn left and right with individual tiles in each
column offset vertically one tile radius and the horizontal offset becoming
0.866 of the radius.
 The "puzzle hints" diagram displays solution values for one of the
solutions in the 6 outside corners of the board. The locations were
found during the board creation by temporarily assigning consecutive letters
as the tiles were created. The letters in the 6 corners were then
replaced solution values as the board was displayed and the other letters
replaced with blanks.
 When adding numbers or other text characters within tiles, font size can
be adjusted for the tile size by setting the Canvas.Font.Height
property of the TImage. I set it to Radius in this
program making the character height 1/2 of the tile diameter.
Characters can be centered by subtracting half of the unused height (and
width) from the tile center coordinates. Canvas.TextWidth
and Canvas,TextHeight methods can return those values.
Running/Exploring the Program
Suggestions for Further Explorations

User play could be implemented probably
by creating an internal square board representation with cell outside of the
board areas containing negative numbers, empty valid board tiles by 0
characters and filled tiles with the 18 valid tile values. 




Created: December 30, 2015 
Modified:
February 18, 2016

