API Docs for: 最后更新日期:2014年8月25日
Google搜索   
Show:

File: geom/LPoint.js

/** @language english
 * Creates a new point. The LPoint object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.
 * @class LPoint
 * @constructor
 * @param {float} x The horizontal coordinate.
 * @param {float} y The vertical coordinate.
 * @example
 * 	var myPoint = new LPoint(100,100);
 * @since 1.7.7
 * @public
 */
var LPoint = (function () {
	function LPoint (x, y) {
		var s = this;
		/** @language english
		 * The horizontal coordinate of the point.
		 * @property x
		 * @type float
		 * @default 0
		 * @since 1.7.7
		 * @public
		 */
		s.x = x;
		/** @language english
		 * The vertical coordinate of the point.
		 * @property y
		 * @type float
		 * @default 0
		 * @since 1.7.7
		 * @public
		 */
		s.y = y;
	}
	/** @language english
	 * [static]Returns the distance between p1 and p2.
	 * @method distance
	 * @static
	 * @param {LPoint} p1 The first point.
	 * @param {LPoint} p2 The second point.
	 * @return {float} The distance between the first and second points.
	 * @since 1.8.5
	 * @public
	 */
	LPoint.distance = function (p1, p2) {
		return LPoint.distance2(p1.x, p1.y, p2.x, p2.y);
	};
	/** @language english
	 * [static]Returns the distance between p1 and p2.
	 * @method distance2
	 * @static
	 * @param {LPoint} x1 The horizontal coordinate of the first point.
	 * @param {LPoint} y1 The vertical coordinate of the first point.
	 * @param {LPoint} x2 2 The horizontal coordinate of the second point.
	 * @param {LPoint} y2 2 The vertical coordinate of the second point.
	 * @return {float} The distance between the first and second points.
	 * @since 1.8.5
	 * @public
	 */
	LPoint.distance2 = function (x1, y1, x2, y2) {
		var x = x1 - x2, y = x1 - x2;
		return Math.sqrt(x * x + y * y);
	};
	/** @language english
	 * [static]Determines a point between two specified points. The parameter f determines where the new interpolated point is located relative to the two end points specified by parameters pt1 and pt2. The closer the value of the parameter f is to 1.0, the closer the interpolated point is to the first point (parameter p1). The closer the value of the parameter f is to 0, the closer the interpolated point is to the second point (parameter p2).
	 * @method interpolate
	 * @static
	 * @param {LPoint} p1 The first point.
	 * @param {LPoint} p2 The second point.
	 * @param {float} f The level of interpolation between the two points. Indicates where the new point will be, along the line between p1 and p2. If f=1, p1 is returned; if f=0, p2 is returned.
	 * @return {LPoint} The new, interpolated point.
	 * @since 1.8.5
	 * @public
	 */
	LPoint.interpolate = function (p1, p2, f) {
		return new LPoint(p1.x + (p2.x - p1.x) * (1 - f), p1.y + (p2.y - p1.y) * (1 - f));
	};
	/** @language english
	 * [static]Converts a pair of polar coordinates to a Cartesian point coordinate.
	 * @method polar
	 * @static
	 * @param {float} len The length coordinate of the polar pair.
	 * @param {float} angle The angle, in radians, of the polar pair.
	 * @return {LPoint} The Cartesian point.
	 * @since 1.8.5
	 * @public
	 */
	LPoint.polar = function (l, a) {
		return new LPoint(l * Math.cos(a), l * Math.sin(a));
	};
	LPoint.prototype = {
		toString : function () {
			return '[object LPoint(' + this.x + ',' + this.y + ')]';
		},
		/** @language english
		 * Returns the length of the line segment from (0,0) to this point.
		 * @method length
		 * @return {float} The length of the line segment from (0,0) to this point.
		 * @since 1.8.5
		 * @public
		 */
		length : function () {
			return LPoint.distance2(this.x, this.y, 0, 0);
		},
		/** @language english
		 * Adds the coordinates of another point to the coordinates of this point to create a new point.
		 * @method add
		 * @param {LPoint} v The point to be added.
		 * @return {LPoint} The new point.
		 * @since 1.8.5
		 * @public
		 */
		add : function (v) {
			return LPoint(this.x + v.x, this.y + v.y);
		},
		/** @language english
		 * Creates a copy of this LPoint object.
		 * @method clone
		 * @return {LPoint} The new LPoint object.
		 * @since 1.8.5
		 * @public
		 */
		clone : function () {
			return new LPoint(this.x, this.y);
		},
		/** @language english
		 * Sets the members of LPoint to the specified values
		 * @method setTo
		 * @param {float} x the x values to set the point to.
		 * @param {float} y the y values to set the point to.
		 * @since 1.8.5
		 * @public
		 */
		setTo : function (x, y) {
			this.x = x, this.y = y;
		},
		/** @language english
		 * Copies all of the point data from the source LPoint object into the calling LPoint object.
		 * @method copyFrom
		 * @param {LPoint} sourcePoint The LPoint object from which to copy the data.
		 * @since 1.8.5
		 * @public
		 */
		copyFrom : function (s) {
			this.setTo(s.x, s.y);
		},
		/** @language english
		 * Determines whether two points are equal. Two points are equal if they have the same x and y values.
		 * @method equals
		 * @param {LPoint} The point to be compared.
		 * @return {Boolean}  A value of true if the object is equal to this Point object; false if it is not equal.
		 * @since 1.8.5
		 * @public
		 */
		equals : function (t) {
			return this.x == t.x && this.y == t.y;
		},
		/** @language english
		 * Scales the line segment between (0,0) and the current point to a set length.
		 * @method normalize
		 * @param {float} thickness The scaling value. For example, if the current point is (0,5), and you normalize it to 1, the point returned is at (0,1).
		 * @since 1.8.5
		 * @public
		 */
		normalize : function (t) {
			var s = this, scale = t / s.length();
			s.x *= scale, s.y *= scale;
		},
		/** @language english
		 * Offsets the LPoint object by the specified amount. The value of dx is added to the original value of x to create the new x value. The value of dy is added to the original value of y to create the new y value.
		 * @method offset
		 * @param {float} dx The amount by which to offset the horizontal coordinate, x
		 * @param {float} dy The amount by which to offset the vertical coordinate, y.
		 * @since 1.8.5
		 * @public
		 */
		offset : function (dx, dy) {
			this.x += dx;
			this.y += dy;
		},
		/** @language english
		 * Subtracts the coordinates of another point from the coordinates of this point to create a new point.
		 * @method subtract
		 * @param {LPoint} v The point to be subtracted.
		 * @return {LPoint} The new point.
		 * @since 1.8.5
		 * @public
		 */
		subtract : function (v) {
			return new LPoint(this.x  - v.x, this.y - v.y);
		}
	};
	return LPoint;
})();