Thursday, 2 February 2012

Perp dot product

I have used the perp dot product in a couple of posts but not explained it. It has a page at Mathworld but no Wikipedia article so I should perhaps explain it here.

The perp dot product is a product between two vectors in two-dimensions, and is obtained by taking the dot product of one vector with the perpendicular of the other. The perpendicular is simply a vector at right angles to the vector it is based on with the same magnitude. It is obtained by rotating through 90° or π/2 radians, or by multiplying by the complex number i in the complex plane.

This can be illustrated with a simple vector class (although I would not use a class myself  for performance reasons):

class Vec2D {
    var fX:Number;
    var fY:Number;

    function Vec2D(_x:Number, _y:Number) {fX = _x; fY = _y;}

    function Perp():Vec2D {
        return Vec2D(-fY, fX);

Note that the Perp function exchanges the x and y members and negates one of them. It is easy to confirm that this is a rotation through a quarter turn.

The perp dot product is then the dot product of one vector with this perp vector. That is, as member functions

    function Dot(v2:Vec2D):Number {
        return fX * v2.fX + fY * v2.fY;
    function PerpDot(v2:Vec2D):Number {
        return Perp().Dot(v2);
    // version with the operations inlined
    function PerpDot(v2:Vec2D):Number {
        return -fY * v2.fX + fX * v2.fY;

It has a number of useful properties. It is a Number with value v1v2 sin θv1 and v2 are the vector lengths and θ is the angle between the vectors, and is the area of the parallelogram with the vectors as two sides, and so twice the area of the triangle formed by the two vectors.

It is closely related to the dot product which has value v1v2 cos θ. Together they form a complex number which if normalised can be used as the rotation between the vector directions. And as already noted they can be passed to atan2 to accurately get the signed angle between the vectors.

The perp dot product is the two-dimensional equivalent of the cross product in three dimensions, although in many ways it is more useful as it more immediately relates to the dot product and rotations. It is something everyone doing two-dimensional geometry should be aware of.

No comments:

Post a Comment