# Point (class)

Categories:Geom

## Overview

### Summary

Point object which represents and manipulates 2D points

### Concept

Native objects are used as Points in various places in JSFL, but the xJSFL Point object brings new object-oriented functionality to the table. The Point class can be used in any function which accepts an Object value with x and y properties.

## API

### Point(x, y)

The Point constructor

Parameters:

• x Number The x coordinate in pixels
• yx Number The y coordinate in pixels

The following example :

```var point = new Point(5, 5);
trace(point);```
`[object Point x=5 y=5]	`

## Properties

### x

The x coordinate of the Point

• Type: Number

### y

The y coordinate of the Point

• Type: Number

### length

The length of the Point from [0,0]

• Type: Number

The following example :

```var pt  = new Point(5, 5);
trace(pt.length);```
`7.0710678118654755	`

## Manipulation methods

Adds the coordinates of another Point to the existing point, and returns a new Point

Parameters:

• pt Point A Point object

Returns:

•   Point A new Point object

The following example :

`var pt1 = new Point(5, 5);var pt2 = new Point(10, 20);pt1     = pt1.add(pt2);trace(pt1);`
`[object Point x=15 y=25]	`

### subtract(pt)

Subtracts the coordinates of another Point from the existing point, and returns a new Point

Parameters:

• pt Point A Point object

Returns:

•   Point A new Point object

The following example :

`var pt1 = new Point(5, 5);var pt2 = new Point(10, 20);pt1     = pt1.subtract(pt2);trace(pt1);`
`[object Point x=-5 y=-15]	`

### equals(pt)

Tests whether two points share the same position

Parameters:

• pt Point A Point object

Returns:

•   Point A new Point object

The following example :

`var pt1 = new Point(5, 5);var pt2 = new Point(10, 20);trace(pt1.equals(pt2));`
`false`

### offset(dx, dy)

Offsets the Point by specified x and y amounts

Parameters:

• dx Number A value to offset the Point in x by
• dy Number A value to offset the Point in y by

Returns:

•   Point The current Point

The following example :

`var pt1 = new Point(5, 5);trace(pt1.offset(10, 10));`
`[object Point x=15 y=15]`

### normalize(scalar)

Normalises the Point's x and y values (based on the length from the origin)

Parameters:

• scalar Object Description

Returns:

•   Object Description

The following example :

`var pt1 = new Point(5, 5);trace(pt1.normalize());`
`[object Point x=0.7071067811865475 y=0.7071067811865475]	`

### orbit(pt, arcWidth, arcHeight, degrees)

Orbits the Point around another Point

Parameters:

• pt Point The Point to orbit around
• arcWidth Number The width of the orbit
• arcHeight Number The height of the orbit
• degrees Number The degrees of the orbit (0 - 360)

Returns:

•   Point The existing Point

The following example draws a circle on the stage:

```var src = new Point(0, 0);
var pt1 = new Point();
var pt2 = new Point();

for (var i = 0; i < 360; i+=36)
{
var pt2 = src.orbit(src, 10, 10, i)
trace(pt2);
pt1 = pt2.clone();
}```
`[object Point x=10 y=0][object Point x=18.090169943749473 y=5.877852522924732][object Point x=21.180339887498945 y=15.388417685876266][object Point x=18.090169943749473 y=24.898982848827803][object Point x=10 y=30.776835371752536][object Point x=0 y=30.776835371752536][object Point x=-8.090169943749476 y=24.898982848827806][object Point x=-11.180339887498953 y=15.388417685876272][object Point x=-8.09016994374948 y=5.877852522924735][object Point x=-7.105427357601002e-15 y=1.7763568394002505e-15]	`

## Calculation methods

### degreesTo(pt)

Gets the angle in degrees from this Point to another Point

Parameters:

• pt Point The target Point

Returns:

•   Number The number of degrees

The following example traces how many degrees from pt1 to pt2:

`var pt1 = new Point(5, 5);var pt2 = new Point(10, 20);trace(pt1.degreesTo(pt2));`
`-108.43494882292202	`

### distanceTo(pt)

Gets the distance in pixels from this Point to another Point

Parameters:

• pt Point The target Point

Returns:

•   Number The distance in pixels

The following example traces the distance from pt1 to pt2:

`var pt1 = new Point(5, 5);var pt2 = new Point(10, 20);trace(pt1.distanceTo(pt2));`
`15.811388300841896	`

### interpolate(pt, f)

Gets the interpolated distance in pixels from this Point to another Point

Parameters:

• pt Point The target Point
• f Number A number from 0 to 1

Returns:

•   Point The point object interpolated between the two points

The following example creates 11 points between pt1 and pt2:

```var pt1 = new Point(5, 5);
var pt2 = new Point(10, 20);

for (var i = 0; i <= 1; i+= 0.1)
{
var pt3 = pt1.interpolate(pt2, i);
trace(pt3);
}```
`[object Point x=0 y=0][object Point x=1.5 y=2.5][object Point x=3 y=5][object Point x=4.500000000000001 y=7.500000000000001][object Point x=6 y=10][object Point x=7.5 y=12.5][object Point x=9 y=15][object Point x=10.5 y=17.5][object Point x=11.999999999999998 y=20][object Point x=13.499999999999998 y=22.499999999999996][object Point x=14.999999999999998 y=24.999999999999996]`

## Utility methods

### clone()

Returns a copy of the current Point

Returns:

•   Point A new Point

The following example clones a point then changes the properties of the first point to check:

```var pt1 = new Point(5, 5);
var pt2 = pt1.clone();
pt1.x = 20;
trace(pt2);```
`[object Point x=5 y=5]`

### toString()

Returns a String representation of the Point

Returns:

•   String The String representation of the Point

The following example traces a Point instance:

```var pt1 = new Point(5, 5);
trace(pt1);```
`[object Point x=5 y=5]`

## Static methods

### interpolate(pt1, pt2, f)

Gets the interpolated distance in pixels from a source Point a target Point

Parameters:

• pt1 Point The source Point
• pt2 Point The target Point
• f Number A number from 0 to 1

Returns:

•   Number The distance in pixels

The following example creates 11 points between pt1 and pt2:

```var pt1 = new Point(5, 5);
var pt2 = new Point(10, 20);

for (var i = 0; i <= 1; i+= 0.1)
{
var pt3 = Point.interpolate(pt1, pt2, i);
trace(pt3);
}```
`[object Point x=0 y=0][object Point x=1.5 y=2.5][object Point x=3 y=5][object Point x=4.500000000000001 y=7.500000000000001][object Point x=6 y=10][object Point x=7.5 y=12.5][object Point x=9 y=15][object Point x=10.5 y=17.5][object Point x=11.999999999999998 y=20][object Point x=13.499999999999998 y=22.499999999999996][object Point x=14.999999999999998 y=24.999999999999996]`

### polar(length, angle)

Returns a new Point, based on an angle around and length from the Origin (0, 0)

Parameters:

• length Number The length from the Origin
• angle Number The angle in degrees to rotate around the origin

Returns:

•   Point A new Point object

The following example traces the points in a circle around the origin (0,0):

```for (var i = 0; i <= 360; i+= 36)
{
trace(Point.polar(10, i));
}```
`[object Point x=0 y=10][object Point x=-9.917788534431157 y=-1.2796368962740468][object Point x=2.5382336276203628 y=-9.672505882738825][object Point x=9.26818505417785 y=3.7550959776701207][object Point x=-4.9102159389846936 y=8.711474010323435][object Point x=-8.011526357338305 y=-5.984600690578581][object Point x=6.960584883449115 y=-7.179850839697136][object Point x=6.23012211003653 y=7.822121099422712][object Point x=-8.555043707508208 y=5.177955886508133][object Point x=-4.040652194563607 y=-9.147301779353752][object Point x=9.589157234143066 y=-2.8369109148652734]	`

### distance(pt1, pt2)

Gets the distance in pixels from a source Point a target Point

Parameters:

• pt1 Point The source Point
• pt2 Point The target Point

Returns:

•   Number The distance in pixels

The following example gets the distance between 2 points:

```var pt1 = new Point(5, 5);
var pt2 = new Point(10, 20);

trace(Point.distance(pt1, pt2));```
`15.811388300841896	`