[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionRoboTracker program came about from discussions about the Lego MindstormsTM robotics system. It's neat but kind of expensive in my view. I'll bet someone that I could write a Delphi robot program to simulate Lego's RoboTracker in less than 50 lines of code. Background & TechniquesVersion 1, TrackEater, took about 30 lines of code, not counting comment lines. I call it TrackEater because that's what it does. Actually it doesn't have a redraw routine to put the track back after the robot passes over it, so the track disappears. It is a good introduction to exit events. The user holds down a mouse button and drags it around to form a track, then presses the Start button to have the robot follow it. There are separate methods for OnMouseDown, OnMouseMove and OnMouseUp events. The MouseDown procedure sets a flag to tell the MouseMove routine that we're drawing, it also does some initialization stuff like setting a nice fat pen we'll use for drawing the track. MouseMove draws a line from the last point to the current point. It also waits for 10ms so that we won't create more than about 100 points per second of drawing. MouseUp resets the flag and moves the robot to the beginning point. When the Start button is pressed, (creating an OnClick event), we just start moving the robot point by point to retrace the steps. The same 10ms delay is inserted between moves so it should move about as fast as the original line drawing speed. The only tricky thing about positioning the robot is that we want to center it on the track. All references to locations are by the top left corner though so we subtract half the width and half the height of the robot from the current point to get a location for its top left corner. A second version is included in the source download. It has a OnPaint routine to redraw the track after the robot moves over it and a few other enhancements that I'll leave for you to check out on your own. Running/Exploring the Program
Suggestions for Further Explorations
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |