Rectangle Inscribed in Polygon

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




Search WWW


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.)


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

Search only




Problem Description

The question is: "What is the largest rectangle that can be inscribed in an arbitrary given simple polygon"?

Background & Techniques

This is an exploration of techniques for finding the maximum area rectangle that can be inscribed in arbitrary concave or convex simple polygons.  Polygons can be defined by clicking vertex points on the image area or loaded from a text file.  All coordinates in this version are pixel locations on an  500 x 500 pixel image area.  

Once a polygon has been defined or loaded, the search strategy is based on finding the maximum area polygon from a given center point.  A trial and error loop successively creates rectangles with a range of major axis angles (typically 0 to 180 degrees)  and a range of Width/Height ratios (typically 1 to 5).   Parameters can be set controlling angle and ratio step size and the max ratio to be tested. 

Several strategies are available for searching for rectangles.  The "Strategies" options create trial rectangle centers which are then passed to the rectangle creation procedure, looking for new maximum area examples.

Load and Save buttons do what they say, preserving and restoring polygon vertices and maximal rectangle information if a search has been performed.  There are additional options to ignore best rectangle for a particular polygon to aid in evaluating specific strategies, and one to restrict rectangles to squares (Width/Height ratio set to 1.0) .

Note: This is not a finished project - there are several known rough spots to be polished as time permits.

Programmer's Notes:

A small book could be written describing the code written so far, however because of the exploratory nature of the program, the geometry involved, and time constraints it is probably best to delay better documentation  until the code has evolved to a more permanent form.  Much of the code was developed while "in the zone" which is not conducive to tidy organization so cleanup is needed. 

 It is still fun to play with though, watching various strategies improve results, sometimes quickly, sometimes slowly.  

The UGeometry library unit has been modified to add a TPolygon type and to override the PolygonArea function to return the Centroid (center of mass) point as well as the Area of a polygon.   The modified version has been included with the source download here and will be added to our DFFLib zip file with the next update.        

Running/Exploring the Program 

bulletDownload  executable
bulletDownload source 

Suggestions for Further Explorations

Automate successive strategy application to find the best rectangle.  
.Make searches "smarter" to stop when there is no chance that a better solution will be found with current parameters.
Code cleanup


Original:  December 23, 2015

Modified:  May 15, 2018

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.