User Guide

418 Working with Geometry
Finding the distance between two points
You can use the distance() method of the Point class to find the distance between two
points in a coordinate space. For example, the following code finds the distance between the
registration points of two display objects,
circle1 and circle2, in the same display object
container:
import flash.geom.*;
var pt1:Point = new Point(circle1.x, circle1.y);
var pt2:Point = new Point(circle2.x, circle2.y);
var distance:Number = Point.distance(pt1, pt2);
Translating coordinate spaces
If two display objects are in different display object containers, they may be in different
coordinate spaces. You can use the
localToGlobal() method of the DisplayObject class to
translate the coordinates to the same (global) coordinate space, that of the Stage. For example,
the following code finds the distance between the registration points of two display objects,
circle1 and circle2, in the different display object containers:
import flash.geom.*;
var pt1:Point = new Point(circle1.x, circle1.y);
pt1 = circle1.localToGlobal(pt1);
var pt2:Point = new Point(circle1.x, circle1.y);
pt2 = circle2.localToGlobal(pt2);
var distance:Number = Point.distance(pt1, pt2);
Similarly, to find the distance of the registration point of a display object named target from
a specific point on the Stage, you can use the
localToGlobal() method of the DisplayObject
class:
import flash.geom.*;
var stageCenter:Point = new Point();
stageCenter.x = this.stage.stageWidth / 2;
stageCenter.y = this.stage.stageHeight / 2;
var targetCenter:Point = new Point(target.x, target.y);
targetCenter = target.localToGlobal(targetCenter);
var distance:Number = Point.distance(stageCenter, targetCenter);