File:Circle.js

/**
 * @module Native Display
 * @namespace springroll.native
 * @requires Core
 */
(function()
{
	/**
	 * The Circle object can be used to specify a hit area for displayobjects
	 *
	 * @class Circle
	 * @constructor
	 * @param x {Number} The X coord of the upper-left corner of the framing rectangle of this circle
	 * @param y {Number} The Y coord of the upper-left corner of the framing rectangle of this circle
	 * @param radius {Number} The radius of the circle
	 */
	var Circle = function(x, y, radius)
	{
		/**
		 * @property x
		 * @type Number
		 * @default 0
		 */
		this.x = x || 0;

		/**
		 * @property y
		 * @type Number
		 * @default 0
		 */
		this.y = y || 0;

		/**
		 * @property radius
		 * @type Number
		 * @default 0
		 */
		this.radius = radius || 0;
	};

	var p = extend(Circle);

	/**
	 * Creates a clone of this Circle instance
	 *
	 * @method clone
	 * @return {Circle} a copy of the polygon
	 */
	p.clone = function()
	{
		return new Circle(this.x, this.y, this.radius);
	};

	/**
	 * Checks if the x, and y coords passed to this function are contained within this circle
	 *
	 * @method contains
	 * @param x {Number} The X coord of the point to test
	 * @param y {Number} The Y coord of the point to test
	 * @return {Boolean} if the x/y coords are within this polygon
	 */
	p.contains = function(x, y)
	{
		if (this.radius <= 0)
			return false;

		var dx = (this.x - x),
			dy = (this.y - y),
			r2 = this.radius * this.radius;

		dx *= dx;
		dy *= dy;

		return (dx + dy <= r2);
	};

	// constructor
	p.constructor = Circle;

	// Assign to namespace
	namespace('springroll.native').Circle = Circle;

}());