Tangram 1

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

 

 

Search

Search WWW

Search DelphiForFun.org

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.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

 

Problem Description

 

Square and Cat 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 & Techniques

This is a blatant rip-off 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 Program 

I've not included object code for this version since the game isn't really playable yet..

bulletDownload source

Suggestions for Further Explorations

Continue working toward an operational Tangram puzzle program.  Tasks I've recognized so far: 

Flip pieces
Define data structure to define figures (pieces, their location  and orientation)
Draw outline of figure without interior lines.
Detect when user tries to drop a piece in invalid location, i.e.  overlapping another piece or a boundary line of the figure.
Detect when the figure is completed
 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.