Skip to content

Commit 4582b0e

Browse files
authored
Merge pull request #764 from jschatz1/js-offset-polar-interpolate
Add offset, polar, and interpolate to Point class
2 parents ee1141d + 3d77d51 commit 4582b0e

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/easeljs/geom/Point.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,53 @@ this.createjs = this.createjs||{};
8585
return this;
8686
};
8787

88+
/**
89+
* 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.
90+
* @method offset
91+
* @param {Number} dx The amount by which to offset the horizontal coordinate, x.
92+
* @param {Number} dy The amount by which to offset the vertical coordinate, y.
93+
* @return {Point} This instance. Useful for chaining method calls.
94+
* @chainable
95+
*/
96+
p.offset = function(dx, dy) {
97+
this.x += dx;
98+
this.y += dy;
99+
return this;
100+
};
101+
102+
/**
103+
* Converts a pair of polar coordinates to a Cartesian point coordinate.
104+
* @method polar
105+
* @param {Number} len The length coordinate of the polar pair.
106+
* @param {Number} angle The angle, in radians, of the polar pair.
107+
* @param {Point | Object} [pt] An object to copy the result into. If omitted a generic object with x/y properties will be returned.
108+
* @return {Point} The new, interpolated point.
109+
* @chainable
110+
*/
111+
Point.polar = function(len, angle, pt) {
112+
pt = pt||{};
113+
pt.x = len * (Math.cos(angle));
114+
pt.y = len * (Math.sin(angle));
115+
return pt;
116+
};
117+
118+
/**
119+
* 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`).
120+
* @method interpolate
121+
* @param {Point | Object} pt1 The first point as a Point or generic object.
122+
* @param {Point | Object} pt2 The second point as a Point or generic object.
123+
* @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.
124+
* @param {Point | Object} [pt] An object to copy the result into. If omitted a generic object with x/y properties will be returned.
125+
* @return {Point} The new, interpolated point.
126+
* @chainable
127+
*/
128+
Point.interpolate = function(pt1, pt2, f, pt) {
129+
pt = pt||{};
130+
pt.x = pt2.x + (f * (pt1.x - pt2.x));
131+
pt.y = pt2.y + (f * (pt1.y - pt2.y));
132+
return pt;
133+
};
134+
88135
/**
89136
* Copies all properties from the specified point to this point.
90137
* @method copy

0 commit comments

Comments
 (0)