/** * The Point object represents a location in a two-dimensional coordinate * system, where x represents the horizontal axis and y * represents the vertical axis. * *
The following code creates a point at(0,0):
* *Methods and properties of the following classes use Point objects:
* *You can use the new Point() constructor to create a Point
* object.
true if the object is equal to this Point
* object; false if it is not equal.
*/
public equals(toCompare: Point): boolean {
return (this._rawData[0] === toCompare._rawData[0] && this._rawData[1] == toCompare._rawData[1]);
}
/**
* Scales the line segment between(0,0) and the current point to a set
* length.
*
* @param 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).
*/
public normalize(thickness: number = 1): void {
const len: number = this.length;
if ((this.x !== 0 || this.y !== 0) && len) {
const relativeThickness: number = thickness / len;
this._rawData[0] *= relativeThickness;
this._rawData[1] *= relativeThickness;
}
}
/**
* 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.
*
* @param dx The amount by which to offset the horizontal coordinate,
* x.
* @param dy The amount by which to offset the vertical coordinate, y.
*/
public offset(dx: number, dy: number): void {
this._rawData[0] += dx;
this._rawData[1] += dy;
}
public setTo(xa: number, ya: number): void {
this._rawData[0] = xa;
this._rawData[1] = ya;
}
/**
* Subtracts the coordinates of another point from the coordinates of this
* point to create a new point.
*
* @param v The point to be subtracted.
* @return The new point.
*/
public subtract(v: Point): Point {
return new Point(this.x - v.x, this.y - v.y);
}
/**
* Returns a string that contains the values of the x and y
* coordinates. The string has the form "(x=x,
* y=y)", so calling the toString() method for a
* point at 23,17 would return "(x=23, y=17)".
*
* @return The string representation of the coordinates.
*/
public toString(): string {
return '[Point] (x=' + this.x + ', y=' + this.y + ')';
}
/**
* Returns the distance between pt1 and pt2.
*
* @param pt1 The first point.
* @param pt2 The second point.
* @return The distance between the first and second points.
*/
public static distance(pt1: Point, pt2: Point): number {
const dx: number = pt2.x - pt1.x;
const dy: number = pt2.y - pt1.y;
return (dx === 0) ? Math.abs(dy) : (dy === 0) ? Math.abs(dx) : Math.sqrt(dx * dx + dy * dy);
}
/**
* 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).
*
* @param pt1 The first point.
* @param pt2 The second point.
* @param 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.
* @return The new, interpolated point.
*/
public static interpolate(pt1: Point, pt2: Point, f: number): Point {
const f1: number = 1 - f;
const raw1: Float32Array = pt1._rawData;
const raw2: Float32Array = pt2._rawData;
return new Point(raw1[0] * f + raw2[0] * f1, raw1[1] * f + raw2[1] * f1);
}
/**
* Converts a pair of polar coordinates to a Cartesian point coordinate.
*
* @param len The length coordinate of the polar pair.
* @param angle The angle, in radians, of the polar pair.
* @return The Cartesian point.
*/
public static polar(len: number, angle: number): Point {
return new Point(len * Math.cos(angle), len * Math.sin(angle));
}
}