[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]


Problem Description
Tangram is a classic Chinese puzzle consisting of 7 pieces cut from a square  5 triangles, a square and a parallelogram. The object is to reassemble them to form the original square, or any of 100's of other shapes given only the outline of the final figure. This is a posting of the basic graphic handling code. It displays the 7 pieces and allows dragging and rotation. There are about 500 lines of code so far. The final version will be able to flip pieces (some figures require the mirror image of the parallelogram). Also code is still required to load and save figure outlines, restrict placing of pieces to valid positions, recognizing when a puzzle is solved, etc. (Done! See Tangram 2) Background & TechniquesThis is a blatant ripoff of an excellent freeware Tangram program written by Mark Overmars and available from http://www.cs.ruu.nl/~markov/kids/ . If you just want to play the game, I recommend that you download it from the that site. My version will never be as complete ( Mark's version includes dozens of graded outlines to solve and even a Help file!) . But the download is object code only, and I was curious about the code required. And, as usual, it has been a learning experience. A TPiece class defines the basic Tangram piece. It includes fields to specify shape, color, the perimeter points, etc. Originally this class was defined as a descendent of TGraphicControl so that each piece could recognize mouse clicks, etc. This approach has problems, especially with triangles. Control areas are rectangular so clicking on a visible piece lying under the transparent half of a triangular control could never be recognized.. TPiece now is a descendant of TObject and all mouse and drawing control is handled by a tTangram control descended from a TPaintBox control. This approach solves the overlap problem but adds the task of recognizing which piece to select when the mouse is clicked. The solution comes via a cool PointInPoly function included in TPiece which returns true when a passed point is in the piece. It uses a well known algorithm which extends a line from the point in some arbitrary direction and then counts how many times this line intersects the sides of the polygon. If the number is odd, the point is inside of the polygon, otherwise the point is outside. tTangram also contains two arrays of pieces  one with the current positions and one as they were originally defined (for resetting the display). The biggest problem so far has been solving the
rounding problem in order to make pieces "snap" together. Single
pixel movement is too fine, especially for younger hands. But any grid
system seemed to not line up properly, especially after rotations.
If you analyze the figures, you'll find that the coordinates for pieces can
occur at multiples of 1 and multiples of Sqrt(2)/2. Running/Exploring the ProgramI've not included object code for this version since the game isn't really playable yet..
Suggestions for Further ExplorationsContinue working toward an operational Tangram puzzle program. Tasks I've recognized so far:

[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 20002017, Gary Darby All rights reserved. 