Calculate the cross product: C# code

A method written in C# for calculating the cross product between two vectors:

  Vector3d CrossProduct(Vector3d v1, Vector3d v2)
  {
    double x, y, z;
    x = v1.Y * v2.Z - v2.Y * v1.Z;
    y = (v1.X * v2.Z - v2.X * v1.Z) * -1;
    z = v1.X * v2.Y - v2.X * v1.Y;

    var rtnvector = new Vector3d(x, y, z);
    rtnvector.Unitize(); //optional
    return rtnvector;
  }

The cross product enables you to find the vector that is ‘perpendicular’ to two other vectors in 3D space. The magnitude of the resultant vector is a function of the ‘perpendicularness’ of the input vectors.

Read more about the cross product here.

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.