phaser/src/geom/circle/Circle.js

358 lines
8.8 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var Contains = require('./Contains');
var GetPoint = require('./GetPoint');
var GetPoints = require('./GetPoints');
var Random = require('./Random');
2016-12-28 23:40:47 +00:00
2018-02-01 05:48:56 +00:00
/**
2018-02-07 15:27:21 +00:00
* @classdesc
2018-02-01 05:48:56 +00:00
* A Circle object.
*
* This is a geometry object, containing numerical values and related methods to inspect and modify them.
* It is not a Game Object, in that you cannot add it to the display list, and it has no texture.
* To render a Circle you should look at the capabilities of the Graphics class.
*
* @class Circle
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Geom
2018-02-01 05:48:56 +00:00
* @constructor
* @since 3.0.0
*
* @param {number} [x=0] - The x position of the center of the circle.
* @param {number} [y=0] - The y position of the center of the circle.
* @param {number} [radius=0] - The radius of the circle.
*/
var Circle = new Class({
2016-12-28 23:40:47 +00:00
initialize:
2016-12-28 23:40:47 +00:00
function Circle (x, y, radius)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (radius === undefined) { radius = 0; }
2016-12-28 23:40:47 +00:00
/**
2018-01-26 04:18:22 +00:00
* The x position of the center of the circle.
*
2018-02-07 15:27:21 +00:00
* @name Phaser.Geom.Circle#x
* @type {number}
* @default 0
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*/
this.x = x;
2016-12-28 23:40:47 +00:00
/**
2018-01-26 04:18:22 +00:00
* The y position of the center of the circle.
*
2018-02-07 15:27:21 +00:00
* @name Phaser.Geom.Circle#y
* @type {number}
* @default 0
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*/
this.y = y;
/**
2018-01-26 04:18:22 +00:00
* The internal radius of the circle.
*
2018-02-07 15:27:21 +00:00
* @name Phaser.Geom.Circle#_radius
* @type {number}
* @private
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*/
this._radius = radius;
/**
2018-01-26 04:18:22 +00:00
* The internal diameter of the circle.
*
2018-02-07 15:27:21 +00:00
* @name Phaser.Geom.Circle#_diameter
* @type {number}
* @private
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*/
this._diameter = radius * 2;
},
2016-12-28 23:40:47 +00:00
2018-01-26 04:18:22 +00:00
/**
* Check to see if the Circle contains the given x / y coordinates.
*
* @method Phaser.Geom.Circle#contains
* @since 3.0.0
*
* @param {number} x - The x coordinate to check within the circle.
* @param {number} y - The y coordinate to check within the circle.
*
* @return {boolean} True if the coordinates are within the circle, otherwise false.
*/
contains: function (x, y)
{
return Contains(this, x, y);
},
2018-01-26 04:18:22 +00:00
/**
* Returns a Point object containing the coordinates of a point on the circumference of the Circle
* based on the given angle normalized to the range 0 to 1. I.e. a value of 0.5 will give the point
* at 180 degrees around the circle.
*
* @method Phaser.Geom.Circle#getPoint
* @since 3.0.0
*
2018-03-27 13:27:08 +00:00
* @generic {Phaser.Geom.Point} O - [out,$return]
*
* @param {number} position - A value between 0 and 1, where 0 equals 0 degrees, 0.5 equals 180 degrees and 1 equals 360 around the circle.
2018-03-20 15:01:08 +00:00
* @param {(Phaser.Geom.Point|object)} [out] - An object to store the return values in. If not given a Point object will be created.
2018-01-26 04:18:22 +00:00
*
2018-03-20 15:01:08 +00:00
* @return {(Phaser.Geom.Point|object)} A Point, or point-like object, containing the coordinates of the point around the circle.
2018-01-26 04:18:22 +00:00
*/
getPoint: function (position, point)
{
return GetPoint(this, position, point);
},
2018-01-26 04:18:22 +00:00
/**
* Returns an array of Point objects containing the coordinates of the points around the circumference of the Circle,
* based on the given quantity or stepRate values.
*
* @method Phaser.Geom.Circle#getPoints
* @since 3.0.0
*
2018-03-27 13:27:08 +00:00
* @generic {Phaser.Geom.Point[]} O - [output,$return]
*
2018-01-26 04:18:22 +00:00
* @param {integer} quantity - The amount of points to return. If a falsey value the quantity will be derived from the `stepRate` instead.
* @param {number} [stepRate] - Sets the quantity by getting the circumference of the circle and dividing it by the stepRate.
2018-03-27 13:27:08 +00:00
* @param {(array|Phaser.Geom.Point[])} [output] - An array to insert the points in to. If not provided a new array will be created.
2018-01-26 04:18:22 +00:00
*
2018-03-27 13:27:08 +00:00
* @return {(array|Phaser.Geom.Point[])} An array of Point objects pertaining to the points around the circumference of the circle.
2018-01-26 04:18:22 +00:00
*/
getPoints: function (quantity, stepRate, output)
{
return GetPoints(this, quantity, stepRate, output);
},
/**
2018-01-26 04:18:22 +00:00
* Returns a uniformly distributed random point from anywhere within the Circle.
*
* @method Phaser.Geom.Circle#getRandomPoint
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*
2018-03-27 13:27:08 +00:00
* @generic {Phaser.Geom.Point} O - [point,$return]
*
2018-03-20 15:01:08 +00:00
* @param {(Phaser.Geom.Point|object)} [point] - A Point or point-like object to set the random `x` and `y` values in.
*
2018-03-20 15:01:08 +00:00
* @return {(Phaser.Geom.Point|object)} A Point object with the random values set in the `x` and `y` properties.
*/
getRandomPoint: function (point)
{
return Random(this, point);
},
/**
2018-01-26 04:18:22 +00:00
* Sets the x, y and radius of this circle.
*
* @method Phaser.Geom.Circle#setTo
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*
2018-01-26 04:18:22 +00:00
* @param {number} [x=0] - The x position of the center of the circle.
* @param {number} [y=0] - The y position of the center of the circle.
* @param {number} [radius=0] - The radius of the circle.
*
* @return {Phaser.Geom.Circle} This Circle object.
*/
2016-12-28 23:40:47 +00:00
setTo: function (x, y, radius)
{
this.x = x;
this.y = y;
this._radius = radius;
this._diameter = radius * 2;
return this;
},
/**
2018-01-26 04:18:22 +00:00
* Sets this Circle to be empty with a radius of zero.
* Does not change its position.
*
* @method Phaser.Geom.Circle#setEmpty
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*
* @return {Phaser.Geom.Circle} This Circle object.
*/
2016-12-28 23:40:47 +00:00
setEmpty: function ()
{
2018-01-26 04:18:22 +00:00
this._radius = 0;
this._diameter = 0;
return this;
2016-12-28 23:40:47 +00:00
},
/**
2018-01-26 04:18:22 +00:00
* Sets the position of this Circle.
*
* @method Phaser.Geom.Circle#setPosition
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*
2018-01-26 04:18:22 +00:00
* @param {number} [x=0] - The x position of the center of the circle.
* @param {number} [y=0] - The y position of the center of the circle.
*
* @return {Phaser.Geom.Circle} This Circle object.
*/
2016-12-28 23:40:47 +00:00
setPosition: function (x, y)
{
if (y === undefined) { y = x; }
this.x = x;
this.y = y;
return this;
},
/**
2018-01-26 04:18:22 +00:00
* Checks to see if the Circle is empty: has a radius of zero.
*
* @method Phaser.Geom.Circle#isEmpty
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*
2018-01-26 04:18:22 +00:00
* @return {boolean} True if the Circle is empty, otherwise false.
*/
isEmpty: function ()
{
return (this._radius <= 0);
},
2016-12-28 23:40:47 +00:00
/**
2018-01-26 04:18:22 +00:00
* The radius of the Circle.
2018-03-20 15:01:08 +00:00
*
2018-01-26 04:18:22 +00:00
* @name Phaser.Geom.Circle#radius
2018-02-07 15:27:21 +00:00
* @type {number}
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*/
2016-12-28 23:40:47 +00:00
radius: {
get: function ()
{
return this._radius;
},
set: function (value)
{
this._radius = value;
this._diameter = value * 2;
}
},
/**
2018-01-26 04:18:22 +00:00
* The diameter of the Circle.
2018-03-20 15:01:08 +00:00
*
2018-01-26 04:18:22 +00:00
* @name Phaser.Geom.Circle#diameter
2018-02-07 15:27:21 +00:00
* @type {number}
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*/
2016-12-28 23:40:47 +00:00
diameter: {
get: function ()
{
return this._diameter;
},
set: function (value)
{
this._diameter = value;
this._radius = value * 0.5;
}
},
/**
2018-01-26 04:18:22 +00:00
* The left position of the Circle.
2018-03-20 15:01:08 +00:00
*
2018-01-26 04:18:22 +00:00
* @name Phaser.Geom.Circle#left
2018-02-07 15:27:21 +00:00
* @type {number}
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*/
2016-12-28 23:40:47 +00:00
left: {
get: function ()
{
return this.x - this._radius;
},
set: function (value)
{
this.x = value + this._radius;
}
},
/**
2018-01-26 04:18:22 +00:00
* The right position of the Circle.
2018-03-20 15:01:08 +00:00
*
2018-01-26 04:18:22 +00:00
* @name Phaser.Geom.Circle#right
2018-02-07 15:27:21 +00:00
* @type {number}
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*/
2016-12-28 23:40:47 +00:00
right: {
get: function ()
{
return this.x + this._radius;
},
set: function (value)
{
this.x = value - this._radius;
}
},
/**
2018-01-26 04:18:22 +00:00
* The top position of the Circle.
2018-03-20 15:01:08 +00:00
*
2018-01-26 04:18:22 +00:00
* @name Phaser.Geom.Circle#top
2018-02-07 15:27:21 +00:00
* @type {number}
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*/
2016-12-28 23:40:47 +00:00
top: {
get: function ()
{
return this.y - this._radius;
},
set: function (value)
{
this.y = value + this._radius;
}
},
/**
2018-01-26 04:18:22 +00:00
* The bottom position of the Circle.
2018-03-20 15:01:08 +00:00
*
2018-01-26 04:18:22 +00:00
* @name Phaser.Geom.Circle#bottom
2018-02-07 15:27:21 +00:00
* @type {number}
2018-01-26 04:18:22 +00:00
* @since 3.0.0
*/
2016-12-28 23:40:47 +00:00
bottom: {
get: function ()
{
return this.y + this._radius;
},
set: function (value)
{
this.y = value - this._radius;
}
}
});
module.exports = Circle;