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}
|
|
|
|
*/
|
|
|
|
|
2017-09-20 22:10:37 +00:00
|
|
|
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
|
|
|
|
|
2017-10-13 15:39:41 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var Curve = require('../curve/Curve');
|
|
|
|
var DegToRad = require('../../math/DegToRad');
|
|
|
|
var GetValue = require('../../utils/object/GetValue');
|
|
|
|
var RadToDeg = require('../../math/RadToDeg');
|
|
|
|
var Vector2 = require('../../math/Vector2');
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class EllipseCurve
|
|
|
|
* @extends Phaser.Curves.Curve
|
|
|
|
* @memberOf Phaser.Curves
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} [x=0] - [description]
|
|
|
|
* @param {number} [y=0] - [description]
|
|
|
|
* @param {number} [xRadius=0] - [description]
|
|
|
|
* @param {number} [yRadius=0] - [description]
|
|
|
|
* @param {number} [startAngle=0] - [description]
|
|
|
|
* @param {number} [endAngle=360] - [description]
|
|
|
|
* @param {boolean} [clockwise=false] - [description]
|
|
|
|
* @param {number} [rotation=0] - [description]
|
|
|
|
*/
|
2017-09-20 22:10:37 +00:00
|
|
|
var EllipseCurve = new Class({
|
|
|
|
|
|
|
|
Extends: Curve,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-09-22 00:34:39 +00:00
|
|
|
function EllipseCurve (x, y, xRadius, yRadius, startAngle, endAngle, clockwise, rotation)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
2017-10-02 21:42:47 +00:00
|
|
|
if (typeof x === 'object')
|
|
|
|
{
|
|
|
|
var config = x;
|
|
|
|
|
|
|
|
x = GetValue(config, 'x', 0);
|
|
|
|
y = GetValue(config, 'y', 0);
|
|
|
|
xRadius = GetValue(config, 'xRadius', 0);
|
|
|
|
yRadius = GetValue(config, 'yRadius', xRadius);
|
|
|
|
startAngle = GetValue(config, 'startAngle', 0);
|
|
|
|
endAngle = GetValue(config, 'endAngle', 360);
|
|
|
|
clockwise = GetValue(config, 'clockwise', false);
|
|
|
|
rotation = GetValue(config, 'rotation', 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (yRadius === undefined) { yRadius = xRadius; }
|
|
|
|
if (startAngle === undefined) { startAngle = 0; }
|
|
|
|
if (endAngle === undefined) { endAngle = 360; }
|
|
|
|
if (clockwise === undefined) { clockwise = false; }
|
|
|
|
if (rotation === undefined) { rotation = 0; }
|
|
|
|
}
|
2017-09-21 02:07:42 +00:00
|
|
|
|
2017-10-02 21:42:47 +00:00
|
|
|
Curve.call(this, 'EllipseCurve');
|
2017-09-21 00:19:27 +00:00
|
|
|
|
2017-09-29 10:43:29 +00:00
|
|
|
// Center point
|
2018-01-25 05:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {[type]} p0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this.p0 = new Vector2(x, y);
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {[type]} _xRadius
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this._xRadius = xRadius;
|
2018-01-25 05:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {[type]} _yRadius
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this._yRadius = yRadius;
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-21 02:07:42 +00:00
|
|
|
// Radians
|
2018-01-25 05:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {[type]} _startAngle
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this._startAngle = DegToRad(startAngle);
|
2018-01-25 05:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {[type]} _endAngle
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this._endAngle = DegToRad(endAngle);
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-21 02:07:42 +00:00
|
|
|
// Boolean (anti-clockwise direction)
|
2018-01-25 05:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {[type]} _clockwise
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this._clockwise = clockwise;
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-21 02:07:42 +00:00
|
|
|
// The rotation of the arc
|
2017-09-22 15:41:11 +00:00
|
|
|
this._rotation = DegToRad(rotation);
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#getStartPoint
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} out - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-27 01:32:26 +00:00
|
|
|
getStartPoint: function (out)
|
2017-09-22 15:41:11 +00:00
|
|
|
{
|
2017-09-27 01:32:26 +00:00
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
2017-09-29 10:43:29 +00:00
|
|
|
return this.getPoint(0, out);
|
2017-09-20 22:10:37 +00:00
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#getResolution
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} divisions - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-21 16:12:16 +00:00
|
|
|
getResolution: function (divisions)
|
|
|
|
{
|
|
|
|
return divisions * 2;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#getPoint
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} t - [description]
|
|
|
|
* @param {[type]} out - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-21 02:07:42 +00:00
|
|
|
getPoint: function (t, out)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
2017-09-21 02:07:42 +00:00
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
2017-09-20 22:10:37 +00:00
|
|
|
var twoPi = Math.PI * 2;
|
2017-09-22 15:41:11 +00:00
|
|
|
var deltaAngle = this._endAngle - this._startAngle;
|
|
|
|
var samePoints = Math.abs(deltaAngle) < Number.EPSILON;
|
2017-09-20 22:10:37 +00:00
|
|
|
|
|
|
|
// ensures that deltaAngle is 0 .. 2 PI
|
|
|
|
while (deltaAngle < 0)
|
|
|
|
{
|
|
|
|
deltaAngle += twoPi;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (deltaAngle > twoPi)
|
|
|
|
{
|
|
|
|
deltaAngle -= twoPi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deltaAngle < Number.EPSILON)
|
|
|
|
{
|
|
|
|
if (samePoints)
|
|
|
|
{
|
|
|
|
deltaAngle = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
deltaAngle = twoPi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
if (this._clockwise && !samePoints)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
|
|
|
if (deltaAngle === twoPi)
|
|
|
|
{
|
|
|
|
deltaAngle = - twoPi;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
deltaAngle = deltaAngle - twoPi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
var angle = this._startAngle + t * deltaAngle;
|
|
|
|
var x = this.p0.x + this._xRadius * Math.cos(angle);
|
|
|
|
var y = this.p0.y + this._yRadius * Math.sin(angle);
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
if (this._rotation !== 0)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
2017-09-22 15:41:11 +00:00
|
|
|
var cos = Math.cos(this._rotation);
|
|
|
|
var sin = Math.sin(this._rotation);
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
var tx = x - this.p0.x;
|
|
|
|
var ty = y - this.p0.y;
|
2017-09-20 22:10:37 +00:00
|
|
|
|
|
|
|
// Rotate the point about the center of the ellipse.
|
2017-09-22 15:41:11 +00:00
|
|
|
x = tx * cos - ty * sin + this.p0.x;
|
|
|
|
y = tx * sin + ty * cos + this.p0.y;
|
2017-09-20 22:10:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 02:07:42 +00:00
|
|
|
return out.set(x, y);
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#setXRadius
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} value - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-29 10:43:29 +00:00
|
|
|
setXRadius: function (value)
|
|
|
|
{
|
|
|
|
this.xRadius = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#setYRadius
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} value - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-29 10:43:29 +00:00
|
|
|
setYRadius: function (value)
|
|
|
|
{
|
|
|
|
this.yRadius = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#setWidth
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} value - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-29 10:43:29 +00:00
|
|
|
setWidth: function (value)
|
|
|
|
{
|
|
|
|
this.xRadius = value * 2;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#setHeight
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} value - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-29 10:43:29 +00:00
|
|
|
setHeight: function (value)
|
|
|
|
{
|
|
|
|
this.yRadius = value * 2;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#setStartAngle
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} value - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-29 10:43:29 +00:00
|
|
|
setStartAngle: function (value)
|
|
|
|
{
|
|
|
|
this.startAngle = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#setEndAngle
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} value - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-29 10:43:29 +00:00
|
|
|
setEndAngle: function (value)
|
|
|
|
{
|
|
|
|
this.endAngle = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#setClockwise
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} value - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-29 10:43:29 +00:00
|
|
|
setClockwise: function (value)
|
|
|
|
{
|
|
|
|
this.clockwise = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#setRotation
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} value - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-29 10:43:29 +00:00
|
|
|
setRotation: function (value)
|
|
|
|
{
|
|
|
|
this.rotation = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
x: {
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.p0.x;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this.p0.x = value;
|
|
|
|
}
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
y: {
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.p0.y;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this.p0.y = value;
|
|
|
|
}
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
xRadius: {
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._xRadius;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._xRadius = value;
|
|
|
|
}
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
yRadius: {
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._yRadius;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._yRadius = value;
|
|
|
|
}
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
startAngle: {
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
2017-09-29 11:58:30 +00:00
|
|
|
return RadToDeg(this._startAngle);
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._startAngle = DegToRad(value);
|
|
|
|
}
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
endAngle: {
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
2017-09-29 11:58:30 +00:00
|
|
|
return RadToDeg(this._endAngle);
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._endAngle = DegToRad(value);
|
|
|
|
}
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
clockwise: {
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._clockwise;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._clockwise = value;
|
|
|
|
}
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
rotation: {
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._rotation;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._rotation = DegToRad(value);
|
|
|
|
}
|
2017-09-29 11:58:30 +00:00
|
|
|
|
2017-10-02 21:42:47 +00:00
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.EllipseCurve#toJSON
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-10-02 21:42:47 +00:00
|
|
|
toJSON: function ()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
type: this.type,
|
|
|
|
x: this.p0.x,
|
|
|
|
y: this.p0.y,
|
|
|
|
xRadius: this._xRadius,
|
|
|
|
yRadius: this._yRadius,
|
|
|
|
startAngle: RadToDeg(this._startAngle),
|
|
|
|
endAngle: RadToDeg(this._endAngle),
|
|
|
|
clockwise: this._clockwise,
|
|
|
|
rotation: RadToDeg(this._rotation)
|
|
|
|
};
|
2017-09-20 22:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-10-02 21:42:47 +00:00
|
|
|
EllipseCurve.fromJSON = function (data)
|
|
|
|
{
|
|
|
|
return new EllipseCurve(data);
|
|
|
|
};
|
|
|
|
|
2017-09-20 22:10:37 +00:00
|
|
|
module.exports = EllipseCurve;
|