<repo_name>ccoley/js3DGraphics<file_sep>/Tuple3.js
/**
 * Created with IntelliJ IDEA.
 * User: Chris
 * Date: 1/21/14
 * Time: 5:38 PM
 */

function Tuple3(x, y, z) {
    this.x = x !== undefined ? x : 0;
    this.y = y !== undefined ? y : 0;
    this.z = z !== undefined ? z : 0;
}

Tuple3.prototype.set = function(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
};

//Tuple3.prototype.copy = function(tuple) {
//    this.x = tuple.x;
//    this.y = tuple.y;
//    this.z = tuple.z;
//};

Tuple3.prototype.scale = function(scaleFactor) {
    this.x *= scaleFactor;
    this.y *= scaleFactor;
    this.z *= scaleFactor;
};

Tuple3.prototype.toString = function() {
    return "[" + this.x + "," + this.y + "," + this.z + "]";
};<file_sep>/README.md
js3DGraphics
================
This is a 3D graphics engine written in JavaScript.

I am following along with the tutorial located [here](http://gamedevelopment.tutsplus.com/series/lets-build-a-3d-graphics-software-engine--gamedev-12718).
<file_sep>/Point.js
/**
 * Created with IntelliJ IDEA.
 * User: Chris
 * Date: 1/21/14
 * Time: 5:01 PM
 */

// Point extends Tuple3
function Point(x, y, z) {
    Tuple3.call(this, x, y, z);
}

// inherit Tuple3
Point.prototype = new Tuple3();

// correct the constructor pointer because it points to Tuple3
Point.prototype.constructor = Point;

/** Point Operations */
Point.prototype.addVector = function(vector) {
    this.x += vector.x;
    this.y += vector.y;
    this.z += vector.z;
};

Point.prototype.subtractVector = function(vector) {
    this.x -= vector.x;
    this.y -= vector.y;
    this.z -= vector.z;
};

Point.prototype.addScaledVector = function(scale, vector) {
    this.x += scale * vector.x;
    this.y += scale * vector.y;
    this.z += scale * vector.z;
};

/** Point Functions */
Point.prototype.distance = function(point) {
    return Math.sqrt((this.x - point.x)*(this.x - point.x) + (this.y - point.y)*(this.y - point.y) + (this.z - point.z)*(this.z - point.z));
};

Point.prototype.distanceSquared = function(point) {
    return (this.x - point.x)*(this.x - point.x) + (this.y - point.y)*(this.y - point.y) + (this.z - point.z)*(this.z - point.z);
};

Point.prototype.drawPoint = function() {
    console.log("Point: " + this.toString());
};<file_sep>/Vector.js
/**
 * Created with IntelliJ IDEA.
 * User: Chris
 * Date: 1/21/14
 * Time: 5:01 PM
 */

// Vector extends Tuple3
function Vector(x, y, z) {
    Tuple3.call(this, x, y, z);
}

// inherit Tuple3
Vector.prototype = new Tuple3();

// correct the constructor pointer because it points to Tuple3
Vector.prototype.constructor = Vector;

/** Vector Operations */
Vector.prototype.addVector = function(vector) {
    this.x += vector.x;
    this.y += vector.y;
    this.z += vector.z;
};

Vector.prototype.subtractVector = function(vector) {
    this.x -= vector.x;
    this.y -= vector.y;
    this.z -= vector.z;
};

Vector.prototype.addScaledVector = function(scale, vector) {
    this.x += scale * vector.x;
    this.y += scale * vector.y;
    this.z += scale * vector.z;
};

Vector.prototype.makeFromPointSubtraction = function(point1, point2) {
    this.x = point1.x - point2.x;
    this.y = point1.y - point2.y;
    this.z = point1.z - point2.z;
};

Vector.prototype.cross = function(vector1, vector2) {
    this.x = vector1.y * vector2.z - vector1.z * vector2.y;
    this.y = vector1.z * vector2.x - vector1.x * vector2.z;
    this.x = vector1.x * vector2.y - vector1.y * vector2.x;
};

/*
 * This method will normalize this Vector so that its length is 1.0. If the
 * length of the Vector is 0 or 1, no action is taken.
 */
Vector.prototype.normalize = function() {
    var lsqr = this.x * this.x + this.y * this.y + this.z * this.z;
    if (lsqr != 1 && lsqr != 0) {
        var dist = Math.sqrt(lsqr);
        this.x /= dist;
        this.y /= dist;
        this.z /= dist;
    }
};

/** Vector Functions */
Vector.prototype.dot = function(vector) {
    return this.x * vector.x + this.y * vector.y + this.z * vector.z;
};

Vector.prototype.magnitude = function() {
    return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
};

Vector.prototype.magnitudeSquared = function() {
    return this.x * this.x + this.y * this.y + this.z * this.z;
};<file_sep>/main.js
/**
 * Created with IntelliJ IDEA.
 * User: Chris
 * Date: 1/21/14
 * Time: 8:24 PM
 */

function main() {
    var point1 = new Point(1,2,1);
    var point2 = new Point(0,4,4);
    var vector1 = new Vector(2,0,0);
    var vector2;

    point1.drawPoint(); // should display (1,2,1)
    point2.drawPoint(); // should display (0,4,4)

    vector2 = new Vector();
    vector2.makeFromPointSubtraction(point1, point2);

    vector1.addVector(vector2);

    point1.addVector(vector1);
    point1.drawPoint(); // should display (4,0,-2)

    point2.subtractVector(vector2);
    point2.drawPoint(); // should display (-1,6,7)
}