Understanding/converting vector maths code

Thread Starter

AlbertHall

Joined Jun 4, 2014
12,625
I have the following code from a 'C' program:
C:
  public class Vector
  {
  public double X;
  public double Y;

public double Cross(Vector v)
  {
  return X * v.Y - Y * v.X;
  }
}
Although this is called 'Cross' it is not, as far as I can tell, a cross product.
It returns a scalar quantity.

Can someone please tell what this is called and what the result represents, please?
 

Papabravo

Joined Feb 24, 2006
22,082
I have the following code from a 'C' program:
C:
  public class Vector
  {
  public double X;
  public double Y;

public double Cross(Vector v)
  {
  return X * v.Y - Y * v.X;
  }
}
Although this is called 'Cross' it is not, as far as I can tell, a cross product.
It returns a scalar quantity.

Can someone please tell what this is called and what the result represents, please?
It looks like the z-component of the Cross Product. More correctly it is the magnitude of the z-component of the Cross Product. The Corss Product of two vectors is a vector that is perpendicular to both of the original vectors. It cannot be represented in 2-dimensional space.
 

bogosort

Joined Sep 24, 2011
696
I have the following code from a 'C' program:
C:
  public class Vector
  {
  public double X;
  public double Y;

public double Cross(Vector v)
  {
  return X * v.Y - Y * v.X;
  }
}
Although this is called 'Cross' it is not, as far as I can tell, a cross product.
It returns a scalar quantity.

Can someone please tell what this is called and what the result represents, please?
That code is definitely not C, nor is it very meaningful on its own. But if I had to guess, I'd say that it is calculating the determinant of a 2x2 matrix comprised of two row vectors U = [u_x u_y] and V = [v_x v_y]:

\(det( \begin{bmatrix}u_x u_y \\ v_x v_y\end{bmatrix} ) = u_x v_y - u_y v_x\)

This can arguably be called a 2D cross product, which is indeed a scalar, but it would have been far less confusing if they had called it det() instead of Cross(). (And they should have used uppercase for vector names, and lowercase for component names.)
 

Thread Starter

AlbertHall

Joined Jun 4, 2014
12,625
Yes, thank you. Dot product or scalar product.

That code is definitely not C, nor is it very meaningful on its own.
That may be because I have chopped out the bit I didn't understand from the whole thing which does go on a bit.
In my searches I did come across a comment that 'object oriented code is a way of making a simple program look big and complicated'.
 
Top