[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

Today's challenge is to graphically simulate the "Pursuit Curve" path of a predator chasing a prey by sight when the prey runs in a straight line.

Background & Techniques

I recently received an interesting book titled "Chases and Escapes", (Paul Nahin, Princeton University Press). The math gets a little heavy (i.e. Differential Equations), but the initial approach, the "Pursuit Curve" illustrated here, is straight forward and fun to simulate. The Dog chases the Rabbit by always heading toward its current location. If the dog runs faster than the rabbit (and the field is large enough), he will always achieve his goal.

I provided a Hole for the rabbit to reach safety. In this initial version, the rabbit runs due North to reach his hole. The dog can be dragged to any starting position and the speed for each animal can be set as desired. In this sample, the dog is running 3 times faster than the rabbit and is about to to catch him.

Notes for Programmers


Although we may eventually use  sprites or pictures of the animals, this version uses TShape control whe we  prove the concept.  We dropped DogShape, RabbitShape, and HoleShape ellipses on a TPanel which defines  our "playing field".   It turns out that even though TPanels have a "Color" property, it is ignored, at least in D7.  To get a "grassy" field, I overlaid a TImage to make a green  background which must be and "sent to back" of the shapes  so that they are remain visible.


When the MoveBtn is clicked, the OnClick exit sets up a loop to move the animals.  RabbitCenter and DogCenter TPoint variables are derived from the RabbitShape and DogShape controls and are use to determine the line between  dog and the mouse. The user set speed variables, DogSpeed and RabbitSpeed, specify the number of pixels to move in their current direction each time through the loop.  The loop exits when one of three conditions is met:

bulletThe dog catches the rabbit. Actual distance between the dog and rabbit when "caught" depends on whether the dog is hungry.  (You'll figure it out I'm sure J.) 
bulletThe rabbit reaches the safety of his hole, or
bulletThe StopFlag is true,  This happens when the when the Reset button is clicked which clears the field, moves the dog and rabbit back to their starting positions, sets the StopFlag to True.    


.I decided to allow the Dog's starting position to be changed by the user by by dragging it to another position.   This implementation is just about as simple as it gets

  1. Change DogShape's DragMode property from dmManual to dmAutomatic.
  2. Define an OnDragOver exit for Image1 which resets location DogShape to the current mouse location if DogShape is the Source parameter.
  3. Define an OnDragEnd event exit for DogShape to move the DogName TLable (the word "Dog") to its location above the DogShape.

 Also, the screenshot posted above did not originally have the animal tracks displayed butI decided that it needed them.  I now display a"*" character for the dog each time it is moved in the MoveBtn.   For the rabbit "track", I draw a line from its home position to the current position each time through the loop.        


Running/Exploring the Program 

bulletDownload  executable
bulletDownload source
bulletDownload current library file (DFFLibV15) required to retrieve UGeometry unit when recompiling the Pursuit program.

Suggestions for Further Explorations

Allow Rabbit and Hole to also be dragged to new starting locations.
Rotate the animal graphics to always point in the direction of their current heading.(The problem was avoided for now by representing the dog as a circle restrcting mouse to vertical movement).
Allow rabbit to deviate from straight line path (zig-zag?).
Evaluate th Pursuit Curve ODE (Ordinary Differential Equation) to allow the dog head straight to the point where the rabbit will be when he intersects is path..
Explore other pursuit problems from the book:

- Duck swimming in a circle around a pond.
- Catch a bad guy boat at sea knowing only where it is now and
that it travels in a straight line at a fixed speed in some unkown
direction (heavy fog blocks seeing it during the chase).



Original:  February 3, 2018

Modified:  February 06, 2018

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