crossProduct method
Calculates the cross product for two 3-dimensional vectors.
Throws MatrixUnsupportedOperation for vectors with other dimensions. Throws MatrixInvalidDimensions if both vectors do not have the same number of elements.
final Vector a = Vector.column([2.0,3.0,4.0]);
final Vector b = Vector.column([5.0,6.0,7.0]);
print(a.crossProduct(b));
prints
[-3.0, 6.0, -3.0]
Implementation
Vector crossProduct(Vector other) {
if (this.elements != 3) {
throw MatrixUnsupportedOperation("Cross product only implemented for 3 dimensional vectors.");
}
if (this.elements != other.elements) {
throw MatrixInvalidDimensions("Cross product requires both vectors to have 3 elements.");
}
List<double> values = [];
values.add(this[1] * other[2] - this[2] * other[1]);
values.add(this[2] * other[0] - this[0] * other[2]);
values.add(this[0] * other[1] - this[1] * other[0]);
if (this.type == VectorType.row) {
return Vector.row(values);
} else {
return Vector.column(values);
}
}