[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
Here's a "doodling" program that allows mouse drawing with optional reflections on 2 or 4 axes.
Background & Techniques
While investigating of symmetry, I decided to post this program as an introduction. It might be fun for youngsters to play around with. A second version is planned that will be more complete, so this one is primarily to let novice programmers investigate use of the TPaintbox component and the OnPaint exit to maintain an image.
No new objects defined in this program, just a TPoints record type to keep track of the path traveled by the mouse. An array, Points, of TPoints records is maintained until the Reset button is pressed. In Paintbox1, the OnMouseDown exit sets a boolean flag, Drawing. OnMouseMove adds a point with the current characteristics if Drawing is true, OnMouseUp sets Drawing to false and sets the Break variable to true in the last point added. Break allows us to keep track of multiple segments in the drawing which may be of different pen colors or widths.
The OnPaint exit, PaintBox1Paint, does the actual drawing when Refresh method is called (or automatically whenever required, for example if you minimize and restore the screen). X and Y coordinates are saved relative to the center of the paint area. For drawings reflected 4 ways, points are rotated by 0, 90, 180 and 270 degrees for display. For 8-way reflections, they are displayed at 45 degree increments.
The equations for rotating a point through angle A are:
In this case, the Sin and Cos function values for 45 degree multiples are approximately plus or minus 0.707, so I just plugged that value as you'll see in the code.
As usual, setting form property Doublebuffered to true eliminates flickering as the screen is redrawn.
TColorDialog is called to change pen colors and a TUpDown component associated with an TEdit box is used to control pen width.
Running/Exploring the Program
Suggestions for Further ExplorationsLots of room to add features to this version
Modified: May 15, 2018
[Feedback] [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby All rights reserved.