MathWorx

point2D.js

Summary

This defines the point2D class which is a special case of a Vector.


Class Summary
point2D The point2D class defines a point in 2-dimensional Euclidean space via homogeneous coordinates (i.e.

/**MathWorx Copyright (c) 2008 Shane Steinert-Threlkeld
Dual-licensed under the MIT and GNU GPL Licenses.
For more information, see LICENSE file */

/**@fileoverview This defines the point2D class which is a special case of a Vector. */

dependencies = ['Vector', 'AffineTransform'];
require(dependencies);

/**Constructor function.  Makes a new 2D point (x,y,1).
@class The point2D class defines a point in 2-dimensional Euclidean space via homogeneous coordinates (i.e. as a 3-vector (x,y,1)).  This allows affine transformations to be applied to 2-D points via matrix multiplication.
@param {Number} x the x component; if x is a point2D object, will create a duplicate of the point
@param {Number} y the y component
@requires Vector
@requires Matrix
@constructor */
function point2D(x, y) {

	if(x instanceof point2D) {
		return new point2D(x.get(1), x.get(2));
	}

	/**The x value of the 2D point.
	@type Number */
	this.x = x;
	/**The y value of the 2D point.
	@type Number */
	this.y = y;
	/**The "dimension" of the point; ALWAYS 2. Important that this.n != 3; gives "illusion" of 2-D vector while allowing multiplication by matrices with 3 columns for transformations
	@type Integer
	@final */
	this.n = 2;
	/**The components of the 2D point.  This allows multiplication by affine transformation matrices and other useful Vector operations.
	@type Array */
	this.components = [this.x, this.y, 1];

}

point2D.prototype = new Vector;

/**Maps this point to a new 2D point according to a given function.  Overwrites the map function of the Vector class.
Used internally for things like addition, multiplication, dot products, etc.
@param func {Object} the mapping function
@return {point2D} the new, mapped point */
point2D.prototype.map = function(func) {
	var x = func(this.x, 0, 0);
	var y = func(this.y, 1, 0);
	return new point2D(x, y);
}

/**Rotates a point through a degree theta and returns a new point.
@param {Number} theta the degree (in radians) by which to rotate the point
@return {point2D} a new point that is the old rotated through an angle theta */
point2D.prototype.rotate = function(theta) {
	var T = new AffineTransform.rotate2D(theta);
	return T.applyTo(this);
}

/**Translates a point (x,y,1) to (x+dx, y+dy, 1).
@param {Number} dx the x translation
@param {Number} dy the y translation
@return {point2D} a new point that is the old one translated */
point2D.prototype.translate = function(dx, dy) {
	var T = new AffineTransform(2).translate2D(dx, dy);
	return T.applyTo(this);
}

/**Converts a 2D point to a 3D point with a 0 z-component.
@returns {point3D} the 3D point */
point2D.prototype.make3D = function() {
	return new point3D(this.x,this.y,0);
}

/**Converts point2D object back to a Vector.  Will no longer be homogeneous coordinates.
@return {Vector} the vector [x,y,z] */
point2D.prototype.toVector = function() {
	return new Vector([this.x,this.y]);
}

/**Produces a string representation of the point.  Overwrites the toString function of the Vector class.
@return {String} the string (x,y) */
point2D.prototype.toString = function() {
	return "("+this.x+","+this.y+")";
}

point2D.zero = new point2D(0,0);

MathWorx

Documentation generated by JSDoc on Mon Aug 11 13:58:31 2008