Calculate Distance within a Canvas

Calculate distance between objects on the Canvas using the Pythagorean formula.


  • What distance? The distance can be between the center points of Balls (check OriginAtCenter for the Ball component using Properties on the Designer) otherwise use the default x,y coordinates. The distance can be to the center coordinates of a Sprite if you provide the code (otherwise the upper left hand corner coordinates of the Sprite is the default). The example assumes the 'center' of the Sprite for the calculation; you may want to use an edge (which is a more difficult calculation).
    The example uses Ball1 (red) as the object that you want to know the distance from. Drag the red ball around and see how the distances change.
  • The position of Ball4 (Violet) is determined by a screen touch.
  • Simple routines show how determine if you are within a certain distance.

This app is actually a demo showing how to calculate using some simple methods. What you do depends on what you mean by distance.

  • The Violet ball can be 'attached' to any Sprite. The example attaches the Violet ball to the center of the Sprite. Note that the Sprite is an image with a transparent background; that might cause you issues so experiment with a different sprite.

Hope someone finds this useful. Does it help you?

CanvasDistanceCalculation3.aia (62.2 KB)


Pitagoras i was thinking about this for days and forgot about this well thanks you are a live saver