Cross and Dot Product

Calculate the cross and dot product of two vectors. Here is the web calculator to do it for you.

/* returns string representation of the cross product vector to the vector parameters
   parameters are the components of the two vectors (x1 is x component of first vector, x2 is x component of second vector, etc.) */
function crossProduct(x1, y1, z1, x2, y2, z2) {
    // components of cross product vectors
    var x = y1 * z2 - z1 * y2;
    var y = z1 * x2 - x1 * z2;
    var z = x1 * y2 - y1 * x2;
    return "(" + x + ", " + y + ", " + z + ")";
}

/* returns dot product of vector parameters
   parameters are the components of the two vectors (x1 is x component of first vector, x2 is x component of second vector, etc.) */
function dotProduct3D(x1, y1, z1, x2, y2, z2) {
    // sum of the product of each of the components
    return x1 * x2 + y1 * y2 + z1 * z2;
}

/* returns dot product of vector parameters
   parameters are the components of the two vectors (x1 is x component of first vector, x2 is x component of second vector, etc.) */
function dotProduct2D(x1, y1, x2, y2) {
    // sum of the product of each of the components
    return x1 * x2 + y1 * y2;
}

// examples
alert("Cross Product: " + crossProduct(1, 2, 3, 2, -1, 1)); // Cross Product: (5, 5, -5)
alert("Dot Product: " + dotProduct3D(1, 2, 3, 2, -1, 1)); // Dot Product: 3
alert("Dot Product: " + dotProduct2D(1, 2, 2, -1)); // Dot Product: 0
DOWNLOAD

         Created: May 23, 2014
Completed in full by: Michael Yaworski