Test Draw Rods Components

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




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


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

Search DelphiForFun.org only




Problem Description

Write a component to simplify the display of cutting patterns for lengths of linear material.  


Background & Techniques

 I have written a version of the Cutting Stock Problem (there will be a link here once the program is posted).   The problem is to figure out the best way to cut a set of required part lengths from a set of available stock pieces of specified lengths and costs.  Depending on  the application, the stock might be dimensional lumber, pipe, wire, rolls of material to be slit, etc.

When I got around to display results from the program I decided that a visual display would be the best, but most complex, technique.  Using the  "divide and conquer" problem solving method, this program defines the required components  and a simpler program to test them

Two new classes were developed,

bulletTRod is a TImage descendant which simulate drawing a cylindrical shape divided into given lengths.
bulletTDisplayPattern is a TPanel descendant and includes a TRod and two TLabel components.     


TRod  description  

There are 5 public fields which the user may set:

bullet HWratio: the ratio of rod diameter to length; type single; default 0.1;
bullet RodColor: the color of the rod; TColor; set by "Create"
bulletBGColor: the color for the area inside the control's boundaries but outside of the rod; TColor; set by "Create";
bulletLabels: Show lengths above each subdivision; Boolean; true.
bulletOpenLeft: Opening on the left end of the rod; Boolean; true.

Also three methods:

bullet constructor Create (proto:TImage; NewRodColor, NewBGColor: TColor); Proto is the prototype image which provides left, top, width, height, parent, and owner field information.  NewRodColor and NewBGColor provide rod color information.
bulletprocedure SetLengths (Val:TRodLengths);{assign a set of rod segment lengths}
bulletprocedure Draw; Draws the rod.

TDisplayPattern description

The only public variable here is Rod, the TRod component,  to allow user modification of the public variables defined above.

There are two methods

bulletconstructor create( Proto:TPanel);  Proto is the prototype TPanel which must contain a TImage and 2 TLabel controls.  The TImage becomes the prototype for the rod.  One of the  TLabels, which must have a Name or Caption field beginning with 'P', is intended to display the pattern identification number for the pattern being displayed.  The other must have a name or captioned beginning with 'N', and specifies the number of stock pieces to be cut including this pattern.
bulletprocedure MakePattern specifies the array of rod segment lengths and the "Pattern id" and "Number to cut" captions.

TestDrawRods program

The top button on the test program form draws two rods with random segment pattern just to test the TRod component.   The bottom part of the form contains buttons to "Make a Pattern", "Set Rod color", and "Clear all Displays".  Each pattern created will be added to a TScrollbox on the right side of the form.    

Lessons learned or relearned

bulletDrawing on a TImage Canvas:  For years I had thought that  the Height and Width properties of  Picture.Bitmap  must be set before we could draw on an image's canvas.  This is no longer true if you draw on the TImage Canvas property.  References to TImage.canvas call GetCanvas which checks (and sets if necessary) Picture.Bitmap.Height and Width.  
bulletThe arc command: Drawing the rod uses the Arc procedure of the image's Canvas property.  Arc requires 8 integer parameters to define the portion of an ellipse to be drawn.   These are four sets of (X,Y) coordinates.  Two sets define top left and bottom right extents of an ellipse.  The other two sets define the end the points of two imaginary line segments from the center of the ellipse to points on or outside of the ellipse.  The intersection points of these lines with the ellipse define the start and end points of the arc, always drawn in a counterclockwise direction. . 
bulletAdding entries to a Scrollbox:  This one was new to me.   The TScrollBox control has the ability to vertically expand its virtual size as necessary accommodate the objects placed on it.   In essence, the scroll box becomes a window into the virtual area which moves down as you scroll up and up as you scroll down.  However  the vertical coordinates for items inserted into a TScrollbox are relative to the current  top of this window (the Position property of the vertical scroll bar).   So, for example,  to move a panel to location 1000, we need to subtract the current VertScrollBar.position value.  (Top := 1000 - vertscrollbar.position;)
bulletSorting an array:  This is so easy to understand that no research is needed to write one, and it can be coded from scratch quicker than you could look one up.  Here's the sort algorithm:   In a double loop, compare each element to those above it.  If they are out of order swap them.   Search the U_TestRodDisplay unit for "Sort" to see the 10 or so lines of code in the procedure,   
bulletMemory leak checking:  Memory leaks, not releasing all of the memory allocated, is a common problem when your code creates and releases objects or other dynamic structures.  Procedure AllocMemSize returns the bytes of memory currently allocated.   By placing the current value on a label or other text display after key operations, memory leaks can be easily detected.  Fixing them may be a different matter.   I noticed atin Delphi 2006, AllocMemSize has be "deprecated" which I assume means that some improved facility is now available there.

Running/Exploring the Program 

bullet Download source
bullet Download  executable

Suggestions for Further Explorations



Original Date: March 13, 2007

Modified: May 15, 2018


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