Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add offset, polar, and interpolate to Point class #764

Merged
merged 4 commits into from Dec 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/easeljs/geom/Point.js
Expand Up @@ -99,6 +99,53 @@ this.createjs = this.createjs||{};
return this;
};

/**
* Offsets the Point 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 {Number} dx The amount by which to offset the horizontal coordinate, x.
* @param {Number} dy The amount by which to offset the vertical coordinate, y.
* @return {Point} This instance. Useful for chaining method calls.
* @chainable
*/
p.offset = function(dx, dy) {
this.x += dx;
this.y += dy;
return this;
};

/**
* Converts a pair of polar coordinates to a Cartesian point coordinate.
* @method polar
* @param {Number} len The length coordinate of the polar pair.
* @param {Number} angle The angle, in radians, of the polar pair.
* @param {Point | Object} [pt] An object to copy the result into. If omitted a generic object with x/y properties will be returned.
* @return {Point} The new, interpolated point.
* @chainable
*/
Point.polar = function(len, angle, pt) {
pt = pt||{};
pt.x = len * (Math.cos(angle));
pt.y = len * (Math.sin(angle));
return pt;
};

/**
* 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 `pt1`). The closer the value of the parameter `f` is to 0, the closer the interpolated point is to the second point (parameter `pt2`).
* @method interpolate
* @param {Point | Object} pt1 The first point as a Point or generic object.
* @param {Point | Object} pt2 The second point as a Point or generic object.
* @param {Number} f The level of interpolation between the two points. Indicates where the new point will be, along the line between `pt1` and `pt2`. If `f=1`, `pt1` is returned; if `f=0`, `pt2` is returned.
* @param {Point | Object} [pt] An object to copy the result into. If omitted a generic object with x/y properties will be returned.
* @return {Point} The new, interpolated point.
* @chainable
*/
Point.interpolate = function(pt1, pt2, f, pt) {
pt = pt||{};
pt.x = pt2.x + (f * (pt1.x - pt2.x));
pt.y = pt2.y + (f * (pt1.y - pt2.y));
return pt;
};

/**
* Copies all properties from the specified point to this point.
* @method copy
Expand Down