Packageflash.geom
Classpublic class Point
InheritancePoint Inheritance Object

Player version: Flash Player 8

The Point class represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.

The following code creates a point at (0,0):

var myPoint:Point = new Point();



Public Properties
 Property
  length : Number
The length of the line segment from (0,0) to this point.
  x : Number
The horizontal coordinate of the point.
  y : Number
The vertical coordinate of the point.
 Properties inherited from class Object
 __proto__, __resolve, constructor, prototype
Public Methods
 Method
  
Creates a new point.
  
Adds the coordinates of another point to the coordinates of this point to create a new point.
  
Creates a copy of this Point object.
  
[static]Returns the distance between pt1 and pt2.
  
equals(toCompare:Object):Boolean
Determines whether two points are equal.
  
[static]Determines a point between two specified points.
  
normalize(length:Number):Void
Scales the line segment between (0,0) and the current point to a set length.
  
offset(dx:Number, dy:Number):Void
Offsets the Point object by the specified amount.
  
polar(len:Number, angle:Number):Point
[static]Converts a pair of polar coordinates to a Cartesian point coordinate.
  
Subtracts the coordinates of another point from the coordinates of this point to create a new point.
  
Returns a string that contains the values of the x and y coordinates.
 Methods inherited from class Object
 addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch
Property detail
lengthproperty
public var length:Number

Player version: Flash Player 8

The length of the line segment from (0,0) to this point.

See also


Example
The following example creates a Point object, myPoint, and determines the length of a line from (0, 0) to that Point.
import flash.geom.Point;
var myPoint:Point = new Point(3,4);
trace(myPoint.length); // 5

xproperty 
public var x:Number

Player version: Flash Player 8

The horizontal coordinate of the point. The default value is 0.


Example
The following example creates a Point object myPoint and sets the x coordinate value.
import flash.geom.Point;
var myPoint:Point = new Point();
trace(myPoint.x); // 0
myPoint.x = 5;
trace(myPoint.x); // 5

yproperty 
public var y:Number

Player version: Flash Player 8

The vertical coordinate of the point. The default value is 0.


Example
The following example creates a Point object myPoint and sets the y coordinate value.
import flash.geom.Point;
var myPoint:Point = new Point();
trace(myPoint.y); // 0
myPoint.y = 5;
trace(myPoint.y); // 5

Constructor detail
Point()constructor
public function Point(x:Number, y:Number)

Player version: Flash Player 8

Creates a new point. If you pass no parameters to this method, a point is created at (0,0).

Parameters
x:Number — The horizontal coordinate. The default value is 0.
 
y:Number — The vertical coordinate. The default value is 0.

Example
The first example creates a Point object point_1 with the default constructor.
import flash.geom.Point;
var point_1:Point = new Point();
trace(point_1.x); // 0
trace(point_1.y); // 0
The second example creates a Point object point_2 with the coordinates x = 1 and y = 2.
import flash.geom.Point;
var point_2:Point = new Point(1, 2);
trace(point_2.x); // 1
trace(point_2.y); // 2

Method detail
add()method
public function add(v:Point):Point

Player version: Flash Player 8

Adds the coordinates of another point to the coordinates of this point to create a new point.

Parameters
v:Point — The point to be added.

Returns
Point — The new point.

Example
The following example creates a Point object resultPoint by adding point_2 to point_1.
import flash.geom.Point;
var point_1:Point = new Point(4, 8);
var point_2:Point = new Point(1, 2);
var resultPoint:Point = point_1.add(point_2);
trace(resultPoint.toString()); // (x=5, y=10)

clone()method 
public function clone():Point

Player version: Flash Player 8

Creates a copy of this Point object.

Returns
Point — The new Point object.

Example
The following example creates a copy of the Point object called clonedPoint from the values found in the myPoint object. The clonedPoint object contains all of the values from myPoint, but it is not the same object.
import flash.geom.Point;
var myPoint:Point = new Point(1, 2);
var clonedPoint:Point = myPoint.clone();
trace(clonedPoint.x); // 1
trace(clonedPoint.y); // 2
trace(myPoint.equals(clonedPoint)); // true
trace(myPoint === clonedPoint); // false

distance()method 
public static function distance(pt1:Point, pt2:Point):Number

Player version: Flash Player 8

Returns the distance between pt1 and pt2.

Parameters
pt1:Point — The first point.
 
pt2:Point — The second point.

Returns
Number — The distance between the first and second points.

Example
The following example creates point_1 and point_2, then determines the distance between them (distanceBetween).
import flash.geom.Point;
var point_1:Point = new Point(-5, 0);
var point_2:Point = new Point(5, 0);
var distanceBetween:Number = Point.distance(point_1, point_2);
trace(distanceBetween); // 10

equals()method 
public function equals(toCompare:Object):Boolean

Player version: Flash Player 8

Determines whether two points are equal. Two points are equal if they have the same x and y values.

Parameters
toCompare:Object — The point to be compared.

Returns
Boolean — If the object is equal to this Point object, true; if it is not equal, false.

Example
The following example determines whether the values of one point are equal to the values of another point. If the objects are the same, equals() does not return the same result that the strict equality operator (===) does.
import flash.geom.Point;
var point_1:Point = new Point(1, 2);
var point_2:Point = new Point(1, 2);
var point_3:Point = new Point(4, 8);
trace(point_1.equals(point_2)); // true
trace(point_1.equals(point_3)); // false
trace(point_1 === point_2); // false
trace(point_1 === point_3); // false

interpolate()method 
public static function interpolate(pt1:Point, pt2:Point, f:Number):Point

Player version: Flash Player 8

Determines a point between two specified points. The parameter f determines where the new interpolated point is located relative to the two end points specified by parameters pt1 and pt2. The closer parameter f is to 1.0, the closer the interpolated point will be to the first point (parameter pt1). The closer parameter f is to 0, the closer the interpolated point will be to the second point (parameter pt2).

Parameters
pt1:Point — The first point.
 
pt2:Point — The second point.
 
f:Number — The level of interpolation between the two points. Indicates where the new point will be, along the line between pt1 and pt2. If f=1, pt1 is returned; if f=0, pt2 is returned.

Returns
Point — The new, interpolated point.

Example
The following example locates the interpolated point (interpolatedPoint) half way (50%) between point_1 and point_2.
import flash.geom.Point;
var point_1:Point = new Point(-100, -100);
var point_2:Point = new Point(50, 50);
var interpolatedPoint:Point = Point.interpolate(point_1, point_2, 0.5);
trace(interpolatedPoint.toString()); // (x=-25, y=-25)

normalize()method 
public function normalize(length:Number):Void

Player version: Flash Player 8

Scales the line segment between (0,0) and the current point to a set length.

Parameters
length:Number — The scaling value. For example, if the current point is (0,5), and you normalize it to 1, the point returned is at (0,1).

See also


Example
The following example extends the length of the normalizedPoint object from 5 to 10.
import flash.geom.Point;
var normalizedPoint:Point = new Point(3, 4);
trace(normalizedPoint.length); // 5
trace(normalizedPoint.toString()); // (x=3, y=4)
normalizedPoint.normalize(10);
trace(normalizedPoint.length); // 10
trace(normalizedPoint.toString()); // (x=6, y=8)

offset()method 
public function offset(dx:Number, dy:Number):Void

Player version: Flash Player 8

Offsets the Point object by the specified amount. The value of dx is added to the original value of x to create the new x value. The value of dy is added to the original value of y to create the new y value.

Parameters
dx:Number — The amount by which to offset the horizontal coordinate, x.
 
dy:Number — The amount by which to offset the vertical coordinate, y.

See also


Example
The following example offsets a point's position by specified x and y amounts.
import flash.geom.Point;
var myPoint:Point = new Point(1, 2);
trace(myPoint.toString()); // (x=1, y=2)
myPoint.offset(4, 8);
trace(myPoint.toString()); // (x=5, y=10) 

polar()method 
public static function polar(len:Number, angle:Number):Point

Player version: Flash Player 8

Converts a pair of polar coordinates to a Cartesian point coordinate.

Parameters
len:Number — The length coordinate of the polar pair.
 
angle:Number — The angle, in radians, of the polar pair.

Returns
Point — The Cartesian point.

See also


Example
The following example creates a Point object cartesianPoint from the value of angleInRadians and a line length of 5. The angleInRadians value equal to Math.atan(3/4) is used because of the characteristics of right triangles with sides that have ratios of 3:4:5.
import flash.geom.Point;
var len:Number = 5;
var angleInRadians:Number = Math.atan(3/4);
var cartesianPoint:Point = Point.polar(len, angleInRadians);
trace(cartesianPoint.toString()); // (x=4, y=3)
When computers work with transcendental numbers such as pi, some round-off error occurs because floating-point arithmetic has only finite precision. When you use Math.PI, consider using the Math.round() function, as shown in the following example.
import flash.geom.Point;
var len:Number = 10;
var angleInRadians:Number = Math.PI;
var cartesianPoint:Point = Point.polar(len, angleInRadians);
trace(cartesianPoint.toString()); // should be (x=-10, y=0), but is (x=-10, y=1.22460635382238e-15)
trace(Math.round(cartesianPoint.y)); // 0

subtract()method 
public function subtract(v:Point):Point

Player version: Flash Player 8

Subtracts the coordinates of another point from the coordinates of this point to create a new point.

Parameters
v:Point — The point to be subtracted.

Returns
Point — The new point.

Example
The following example creates point_3 by subtracting point_2 from point_1.
import flash.geom.Point;
var point_1:Point = new Point(4, 8);
var point_2:Point = new Point(1, 2);
var resultPoint:Point = point_1.subtract(point_2);
trace(resultPoint.toString()); // (x=3, y=6)

toString()method 
public function toString():String

Player version: Flash Player 8

Returns a string that contains the values of the x and y coordinates. It has the form (x, y), so a Point at 23,17 would report "(x=23, y=17)".

Returns
String — A string.

Example
The following example creates a point and converts its values to a string in the format (x=x, y=y).
import flash.geom.Point;
var myPoint:Point = new Point(1, 2);
trace("myPoint: " + myPoint.toString()); // (x=1, y=2)