2-dimensional point class

Pooling

To avoid creating new instances, unnecessarily, used points can be for later use. Rather than creating a new instance directly, call FlxPoint.get(x, y) and it will retrieve a point from the pool, if one exists, otherwise it will create a new instance. Similarly, when you're done using a point, call myPoint.put() to place it back.

You can disable point pooling entirely with FLX_NO_POINT_POOL.

Weak points

Weak points are points meant for a singular use, rather than calling put on every point you get, you can create a weak point, and have it placed back once used. All FlxPoint methods and Flixel utilities automatically call putWeak() on every point passed in.

In the following example, a weak point is created, and passed into p.degreesTo where putWeak is called on it, putting it back in the pool.

var angle = p.degreesTo(FlxPoint.weak(FlxG.mouse.x, FlxG.mouse.y));

Overloaded Operators

  • A += B adds the value of B to A
  • A -= B subtracts the value of B from A
  • A *= k scales A by float k in both x and y components
  • A + B returns a new point that is sum of A and B
  • A - B returns a new point that is difference of A and B
  • A * k returns a new point that is A scaled with coefficient k

Note: also accepts openfl.geom.Point, but always returns a FlxPoint.

Note: that these operators get points from the pool, but do not put points back in the pool, unless they are weak.

Example: 4 total points are created, but only 3 are put into the pool

var a = FlxPoint.get(1, 1);
var b = FlxPoint.get(1, 1);
var c = (a * 2.0) + b;
a.put();
b.put();
c.put();

To put all 4 back, it should look like this:

var a = FlxPoint.get(1, 1);
var b = FlxPoint.get(1, 1);
var c = a * 2.0;
var d = c + b;
a.put();
b.put();
c.put();
d.put();

Otherwise, the remainging points will become garbage, adding to the heap, potentially triggering a garbage collection when you don't want.

Static variables

@:value(0.0000001)staticinlineread onlyEPSILON:Float = 0.0000001

@:value(EPSILON * EPSILON)staticinlineread onlyEPSILON_SQUARED:Float = EPSILON * EPSILON

staticdegrees:Float

The angle formed by the point with the horizontal axis (in degrees)

staticread onlydx:Float

The horizontal component of the unit point

staticread onlydy:Float

The vertical component of the unit point

staticlength:Float

Length of the point

staticread onlylengthSquared:Float

length of the point squared

staticread onlylx:Float

The horizontal component of the left normal of the point

staticread onlyly:Float

The vertical component of the left normal of the point

staticradians:Float

The angle formed by the point with the horizontal axis (in radians)

staticread onlyrx:Float

The horizontal component of the right normal of the point

staticread onlyry:Float

The vertical component of the right normal of the point

staticx:Float

staticy:Float

Static methods

@:value({ y : 0, x : 0 })staticinlineadd(this:FlxBasePoint, x:Float = 0, y:Float = 0):FlxPoint

Adds to the coordinates of this point.

Parameters:

x

Amount to add to x

y

Amount to add to y

Returns:

This point.

staticinlineaddNew(this:FlxBasePoint, p:FlxPoint):FlxPoint

Return new point which equals to sum of this point and passed p point.

Parameters:

p

point to add

Returns:

addition result

staticinlineaddPoint(this:FlxBasePoint, point:FlxPoint):FlxPoint

Adds the coordinates of another point to the coordinates of this point.

Parameters:

point

The point to add to this point

Returns:

This point.

staticinlineaddToFlash(this:FlxBasePoint, p:Point):Point

Helper function, just increases the values of the specified Flash point by the values of this point.

Parameters:

p

Any Point.

Returns:

A reference to the altered point parameter.

staticangleBetween(this:FlxBasePoint, point:FlxPoint):Float

Deprecated: "angleBetween is deprecated, use degreesTo instead"

DEPRECATED

Calculates the angle between this and another point. 0 degrees points straight up.

Note: Every other flixel function treats straight right as 0 degrees.

Also Note: The result is very innacurate.

Parameters:

point

The other point.

Returns:

The angle in degrees, between -180 and 180.

See also:

@:value({ bounceCoeff : 1 })staticinlinebounce(this:FlxBasePoint, normal:FlxPoint, bounceCoeff:Float = 1):FlxPoint

Reflect the point with respect to the normal of the "wall".

Parameters:

normal

left normal of the "wall". It must be normalized (no checks)

bounceCoeff

bounce coefficient (0 <= bounceCoeff <= 1)

Returns:

reflected point (angle of incidence equals to angle of reflection)

@:value({ friction : 0, bounceCoeff : 1 })staticinlinebounceWithFriction(this:FlxBasePoint, normal:FlxPoint, bounceCoeff:Float = 1, friction:Float = 0):FlxPoint

Reflect the point with respect to the normal. This operation takes "friction" into account.

Parameters:

normal

left normal of the "wall". It must be normalized (no checks)

bounceCoeff

bounce coefficient (0 <= bounceCoeff <= 1)

friction

friction coefficient

Returns:

reflected point

staticinlineceil(this:FlxBasePoint):FlxPoint

Rounds x and y using Math.ceil()

staticinlineclone(this:FlxBasePoint, ?p:FlxPoint):FlxPoint

Copies this point.

Parameters:

p

optional point to copy this point to

Returns:

copy of this point

staticinlinecopyFrom(this:FlxBasePoint, p:FlxPoint):FlxPoint

Helper function, just copies the values from the specified point.

Parameters:

p

Any FlxPoint.

Returns:

A reference to itself.

staticinlinecopyFromFlash(this:FlxBasePoint, p:Point):FlxPoint

Helper function, just copies the values from the specified Flash point.

Parameters:

p

Any Point.

Returns:

A reference to itself.

staticinlinecopyTo(this:FlxBasePoint, ?p:FlxPoint):FlxPoint

Helper function, just copies the values from this point to the specified point.

Parameters:

p

optional point to copy this point to

Returns:

copy of this point

staticinlinecopyToFlash(this:FlxBasePoint, ?p:Point):Point

Helper function, just copies the values from this point to the specified Flash point.

Parameters:

p

Any Point.

Returns:

A reference to the altered point parameter.

staticinlinecrossProductLength(this:FlxBasePoint, p:FlxPoint):Float

Find the length of cross product between two points.

Parameters:

p

point to multiply

Returns:

the length of cross product of two points

staticinlinedegreesBetween(this:FlxBasePoint, p:FlxPoint):Float

The angle between points (in degrees).

Parameters:

p

second point, which we find the angle

Returns:

the angle in degrees

staticinlinedegreesFrom(this:FlxBasePoint, point:FlxPoint):Float

Calculates the angle from another point to this. If this is straight right of the point, 0 is returned.

Parameters:

point

The other point.

Returns:

The angle, in degrees, between -180 and 180

Available since

5.0.0

.

staticinlinedegreesTo(this:FlxBasePoint, point:FlxPoint):Float

Calculates the angle from this to another point. If the point is straight right of this, 0 is returned.

Parameters:

point

The other point.

Returns:

The angle, in degrees, between -180 and 180

Available since

5.0.0

.

staticinlinedist(this:FlxBasePoint, p:FlxPoint):Float

The distance between points

staticinlinedistSquared(this:FlxBasePoint, p:FlxPoint):Float

The squared distance between points

staticdistanceTo(this:FlxBasePoint, point:FlxPoint):Float

Calculate the distance to another point.

Parameters:

point

A FlxPoint object to calculate the distance to.

Returns:

The distance between the two points as a Float.

staticinlinedot(this:FlxBasePoint, p:FlxPoint):Float

Short for dot product.

Parameters:

p

point to multiply

Returns:

dot product of two points

staticinlinedotProdWithNormalizing(this:FlxBasePoint, p:FlxPoint):Float

Dot product of points with normalization of the second point.

Parameters:

p

point to multiply

Returns:

dot product of two points

staticinlinedotProduct(this:FlxBasePoint, p:FlxPoint):Float

Dot product between two points.

Parameters:

p

point to multiply

Returns:

dot product of two points

staticfindIntersection(this:FlxBasePoint, a:FlxPoint, b:FlxPoint, p:FlxPoint, ?intersection:FlxPoint):FlxPoint

Finding the point of intersection of points.

Parameters:

a

start point of the point

b

start point of the p point

p

the second point

Returns:

the point of intersection of points

staticfindIntersectionInBounds(this:FlxBasePoint, a:FlxPoint, b:FlxPoint, p:FlxPoint, ?intersection:FlxPoint):FlxPoint

Finding the point of intersection of points if it is in the "bounds" of the points.

Parameters:

a

start point of the point

b

start point of the p point

p

the second point

Returns:

the point of intersection of points if it is in the "bounds" of the points

staticinlinefloor(this:FlxBasePoint):FlxPoint

Rounds x and y using Math.floor()

@:value({ y : 0, x : 0 })staticinlineget(x:Float = 0, y:Float = 0):FlxPoint

Recycle or create new FlxPoint. Be sure to put() them back into the pool after you're done with them!

Parameters:

x

The X-coordinate of the point in space.

y

The Y-coordinate of the point in space.

staticinlineinCoords(this:FlxBasePoint, x:Float, y:Float, width:Float, height:Float):Bool

Returns true if this point is within the given rectangular bounds

Parameters:

x

The X value of the region to test within

y

The Y value of the region to test within

width

The width of the region to test within

height

The height of the region to test within

Returns:

True if the point is within the region, otherwise false

staticinlineinRect(this:FlxBasePoint, rect:FlxRect):Bool

Returns true if this point is within the given rectangular block

Parameters:

rect

The FlxRect to test within

Returns:

True if pointX/pointY is within the FlxRect, otherwise false

staticinlineisNormalized(this:FlxBasePoint):Bool

Check the point for unit length

staticinlineisParallel(this:FlxBasePoint, p:FlxPoint):Bool

Check for parallelism of two points.

Parameters:

p

point to check

Returns:

true - if they are parallel

staticinlineisPerpendicular(this:FlxBasePoint, p:FlxPoint):Bool

Check the perpendicularity of two points.

Parameters:

p

point to check

Returns:

true - if they are perpendicular

staticinlineisValid(this:FlxBasePoint):Bool

Checking if this is a valid point.

Returns:

true - if the point is valid

staticinlineisZero(this:FlxBasePoint):Bool

Check if this point has zero length.

Returns:

true - if the point is zero

staticleftNormal(this:FlxBasePoint, ?p:FlxPoint):FlxPoint

Left normal of the point

staticinlinenegate(this:FlxBasePoint):FlxPoint

Change direction of the point to opposite

staticinlinenegateNew(this:FlxBasePoint):FlxPoint

staticnormalize(this:FlxBasePoint):FlxPoint

Normalization of the point (reduction to unit length)

staticinlineperpProduct(this:FlxBasePoint, p:FlxPoint):Float

Dot product of left the normal point and point p.

staticinlinepivotDegrees(this:FlxBasePoint, pivot:FlxPoint, degrees:Float):FlxPoint

Rotates this point clockwise in 2D space around another point by the given degrees. Note: To rotate a point around 0,0 you can use p.degrees += angle

Parameters:

pivot

The pivot you want to rotate this point around

degrees

Rotate the point by this many degrees clockwise.

Returns:

A FlxPoint containing the coordinates of the rotated point.

Available since

5.0.0

.

staticpivotRadians(this:FlxBasePoint, pivot:FlxPoint, radians:Float):FlxPoint

Rotates this point clockwise in 2D space around another point by the given radians. Note: To rotate a point around 0,0 you can use p.radians += angle

Parameters:

pivot

The pivot you want to rotate this point around

radians

Rotate the point by this many radians clockwise.

Returns:

A FlxPoint containing the coordinates of the rotated point.

Available since

5.0.0

.

staticprojectTo(this:FlxBasePoint, p:FlxPoint, ?proj:FlxPoint):FlxPoint

The projection of this point to point that is passed as an argument (without modifying the original point!).

Parameters:

p

point to project

proj

optional argument - result point

Returns:

projection of the point

staticprojectToNormalized(this:FlxBasePoint, p:FlxPoint, ?proj:FlxPoint):FlxPoint

Projecting this point on the normalized point p.

Parameters:

p

this point has to be normalized, ie have unit length

proj

optional argument - result point

Returns:

projection of the point

staticinlineradiansBetween(this:FlxBasePoint, p:FlxPoint):Float

Get the angle between points (in radians).

Parameters:

p

second point, which we find the angle

Returns:

the angle in radians

staticinlineradiansFrom(this:FlxBasePoint, point:FlxPoint):Float

Calculates the angle from another point to this. If this is straight right of the point, 0 is returned.

Parameters:

point

The other point.

Returns:

The angle, in radians, between -PI and PI

Available since

5.0.0

.

staticinlineradiansTo(this:FlxBasePoint, point:FlxPoint):Float

Calculates the angle from this to another point. If the point is straight right of this, 0 is returned.

Parameters:

point

The other point.

Returns:

The angle, in radians, between -PI and PI

Available since

5.0.0

.

staticinlineratio(this:FlxBasePoint, a:FlxPoint, b:FlxPoint, p:FlxPoint):Float

Find the ratio between the perpProducts of this point and p point. This helps to find the intersection point.

Parameters:

a

start point of the point

b

start point of the p point

p

the second point

Returns:

the ratio between the perpProducts of this point and p point

staticrightNormal(this:FlxBasePoint, ?p:FlxPoint):FlxPoint

Right normal of the point

staticrotate(this:FlxBasePoint, pivot:FlxPoint, degrees:Float):FlxPoint

Deprecated: "rotate is deprecated, use pivotDegrees"

Rotates this point clockwise in 2D space around another point by the given degrees.

Parameters:

pivot

The pivot you want to rotate this point around

degrees

Rotate the point by this many degrees clockwise.

Returns:

A FlxPoint containing the coordinates of the rotated point.

staticinlinerotateByDegrees(this:FlxBasePoint, degs:Float):FlxPoint

Rotate the point for a given angle.

Parameters:

degs

angle to rotate

Returns:

rotated point

staticinlinerotateByRadians(this:FlxBasePoint, rads:Float):FlxPoint

Rotate the point for a given angle.

Parameters:

rads

angle to rotate

Returns:

rotated point

staticinlinerotateWithTrig(this:FlxBasePoint, sin:Float, cos:Float):FlxPoint

Rotate the point with the values of sine and cosine of the angle of rotation.

Parameters:

sin

the value of sine of the angle of rotation

cos

the value of cosine of the angle of rotation

Returns:

rotated point

staticinlineround(this:FlxBasePoint):FlxPoint

Rounds x and y using Math.round()

staticinlinescale(this:FlxBasePoint, x:Float, ?y:Float):FlxPoint

Scale this point.

Parameters:

x
  • scale x coefficient
y
  • scale y coefficient, if omitted, x is used

Returns:

scaled point

staticinlinescaleNew(this:FlxBasePoint, k:Float):FlxPoint

Returns scaled copy of this point.

Parameters:

k
  • scale coefficient

Returns:

scaled point

staticinlinescalePoint(this:FlxBasePoint, point:FlxPoint):FlxPoint

Scale this point by another point.

Parameters:

point
  • The x and y scale coefficient

Returns:

scaled point

@:value({ y : 0, x : 0 })staticinlineset(this:FlxBasePoint, x:Float = 0, y:Float = 0):FlxPoint

Set the coordinates of this point object.

Parameters:

x

The X-coordinate of the point in space.

y

The Y-coordinate of the point in space.

staticinlinesetPolarDegrees(this:FlxBasePoint, length:Float, degrees:Float):FlxPoint

Sets the polar coordinates of the point

Parameters:

length

The length to set the point

degrees

The angle to set the point, in degrees

Returns:

The rotated point

Available since

4.10.0

.

staticsetPolarRadians(this:FlxBasePoint, length:Float, radians:Float):FlxPoint

Sets the polar coordinates of the point

Parameters:

length

The length to set the point

radians

The angle to set the point, in radians

Returns:

The rotated point

Available since

4.10.0

.

staticsign(this:FlxBasePoint, a:FlxPoint, b:FlxPoint):Int

The sign of half-plane of point with respect to the point through the a and b points.

Parameters:

a

start point of the wall-point

b

end point of the wall-point

@:value({ y : 0, x : 0 })staticinlinesubtract(this:FlxBasePoint, x:Float = 0, y:Float = 0):FlxPoint

Subtracts from the coordinates of this point.

Parameters:

x

Amount to subtract from x

y

Amount to subtract from y

Returns:

This point.

staticinlinesubtractFromFlash(this:FlxBasePoint, p:Point):Point

Helper function, just decreases the values of the specified Flash point by the values of this point.

Parameters:

p

Any Point.

Returns:

A reference to the altered point parameter.

staticinlinesubtractNew(this:FlxBasePoint, p:FlxPoint):FlxPoint

Returns new point which is result of subtraction of p point from this point.

Parameters:

p

point to subtract

Returns:

subtraction result

staticinlinesubtractPoint(this:FlxBasePoint, point:FlxPoint):FlxPoint

Subtracts the coordinates of another point from the coordinates of this point.

Parameters:

point

The point to subtract from this point

Returns:

This point.

staticinlinetransform(this:FlxBasePoint, matrix:Matrix):FlxPoint

Applies transformation matrix to this point

Parameters:

matrix

transformation matrix

Returns:

transformed point

staticinlinetruncate(this:FlxBasePoint, max:Float):FlxPoint

Limit the length of this point.

Parameters:

max

maximum length of this point

@:value({ y : 0, x : 0 })staticinlineweak(x:Float = 0, y:Float = 0):FlxPoint

Recycle or create a new FlxPoint which will automatically be released to the pool when passed into a flixel function.

Parameters:

x

The X-coordinate of the point in space.

y

The Y-coordinate of the point in space.

Available since

4.6.0

.

staticinlinezero(this:FlxBasePoint):FlxPoint

point reset