Category Archives: Geometry

Basis geometry

Get a offset (or point) on a vector perpendicular to a point

 

To find the offset on a vector perpendicular to a point use the dot product

Assume the vector is defined by P1 and P2  and the point by P

in 2d

            (P.x - P1.x) * (P2.x - P1.x) + (P.y - P1.y) * (P2.y - P1.y)
offset =    -----------------------------------------------------------------
            (P2.x - P1.x) * (P2.x - P1.x) + (P2.y - P1.y) * (P2.y - P1.y)

or 3d

            (P.x - P1.x) * (P2.x - P1.x) + (P.y - P1.y) * (P2.y - P1.y) + (P.z - P1.z) * (P2.z- P1.z)
offset =    ---------------------------------------------------------------------------------------------------
            (P2.x - P1.x) * (P2.x - P1.x) + (P2.y - P1.y) * (P2.y - P1.y) + (P2.z - P1.z) * (P2.z - P1.z)

notes:

If vector P1,P2 has been normalized (length 1) the division by the length is obviously not necessary. If not the result is divided the square length. Although divided by the length (square root) the result will equal that of a normalized vector. Keep in mind that the dot project is a projection on the vector thus requiring a additional division by the length!

 

Calculating the point on the vector can be done using

P.x =  P1.x + (P2.x - P1.x) * o;

P.y =  P1.y + (P2.y - P1.y) * o;

P.z =  P1.z + (P2.z - P1.z) * o;  (3d)
Lastest update in June 2011, inital post in June 2011

The cross product

Formula:

Nx = ( dz1 * dy2 ) – (dy1 * dz2 );
Ny = ( dx1 * dz2 ) – (dz1 * dx2 );
Nz = ( dy1 * dx2 ) – (dx1 * dy2 );

dx1 is the delta between the two x coordinates (last – first) of vector V1.
dx2 is the delta between the two x coordinates (last – first) of vector V2.
dy1 is the delta between the two y coordinates (last – first) of vector V1.
dy2 is the delta between the two y coordinates (last – first) of vector V2.
dz1 is the delta between the two z coordinates (last – first) of vector V1.
dz2 is the delta between the two z coordinates (last – first) of vector V2.

The cross product calculated between two vectors results in a vector perpendicular to both vectors. The length of this vector is the surface of the parallelogram.

If the angle between the two vectors is 0 or 180 degrees the lengths is zero.  This can be used for vector overlap checks.

In 2D only the Nz value is used, it can be used for overlaps or parallel vectors etc.

Uses:

Are two 2D vectors parallel

Are two 2D vectors  coincident

Get the intersection point of two 2D vectors

Lastest update in May 2011, inital post in May 2011

The dot product

Formula:  dx1 * dx2 + dy1 * dy2 + dz1 * dz1

dx1 is the delta between the two x coordinates (last – first) of vector V1.
dx2 is the delta between the two x coordinates (last – first) of vector V2.
dy1 is the delta between the two y coordinates (last – first) of vector V1.
dy2 is the delta between the two y coordinates (last – first) of vector V2.
dz1 is the delta between the two z coordinates (last – first) of vector V1.
dz2 is the delta between the two z coordinates (last – first) of vector V2.

Check of 90 angles

The dot product is actually a representation of the cosine. When the internal vector of two vectors angle is 90 degrees the dot product is always zero.

Project point on vector

Project P on vector V1

Projection can be done using the dot product.

You can chose to normalize the vector, this is how I learned it.

Normalizing a vector requires you to make its length 1 but keeping its direction.

length = sqrt(  ((vx2 - vx1) * (vx2 - vx1)) + ((vy2 - vy1) * (vy2 - vy1))  +( (vz2 - vz1) * (vz2 - vz1)) );

vx2 = vx1 + (vx2 - vx1) / length;

vy2 = vy1 + (vy2 - vy1) / length;

vz2 = vz1 + (vz2 - vz1) / length;

Note: It is often more efficient to calculate the delta’s up front.

The dot vector can be used to project a point on a vector (line).

The result will be 3 * 1 + 2 * 0  + 0 * 0 = 3

Now vector |V1| multiplied with 3 will point to the projection point.

Alternative

Alternatively do not normalize the vector. In that case the result will be 3 * 4 + 2 * 0  + 0 * 0 = 12.

When you look at the original formula you can divide it by the length of V1 (4) to get the offset relatively to the |V1| vector.

To get the offset to the original vector divide by the length again. Which will give offset 1.0. Useful thing is you will save the square.

For a 2d vector and a point the formula becomes:

(vx2 – vx1) * (px -vx1) + (vy2 – vy1) * (py – vy1)

——————————————————-

((vx2 – vx1) *  (vx2 – vx1)) +  ( (vy2 – vy1) * (vy2 – vy1))

Lastest update in May 2011, inital post in May 2011