Using Basic Geometry Types

The following examples show some of the most commonly used functions and operators in the point, vector, and matrix classes. These examples use the 3D classes, but most of them also apply to the 2D classes as well.

The default constructor for points and vectors initializes all coordinates to 0. Points and vectors may also be constructed by specifying their coordinates as follows:

AcGePoint3d    p1(2.0,5.0,-7.5), p2, p3(1.0,2.0,3.0);
AcGeVector3d   v1(3.0,4.0,5.0), v2(0.0,1.0,-1.0), v3;

The point and vector classes provide +, +=, -, and -= operators. These operators allow points and vectors to be used in much the same way as built-in types, such as doubles and integers. The following are examples of adding and subtracting points and vectors:

p2 = p1 + v1;       // Set p2 to sum of p1 and v1.
p1 += v1;           // Add v1 to p1.
p3 -= v1;           // Subtract v1 from p3.
v3 = v1 + v2;       // Set v3 to sum of v1 and v2.
v1 += v2;           // Add v2 to v1.
v3 = v1 - v2;       // Set v3 to difference of v1 and v2.

There is no + operator for adding two points; however, a point can be converted to a vector, which can then be added to another point:

p1 += p2.asVector();

The following are examples of how to obtain the negative of a vector:

v2 = -v1;           // Set v2 to negative of v1.
v1.negate();        // This is equivalent to v1 = -v1.

The following are examples of different ways to scale a vector:

v1 *= 2.0;          // Doubles the length of v1.
v3 = v1 / 2.0;      // Set v3 to half the length of v1.
v1.normalize();     // Make v1 a unit vector.

The point and vector classes contain a number of query functions for computing distances and lengths:

double len = v2.length();  // Length of v2.
len = p1.distanceTo(p2);   // Distance from p1 to p2.

The following function is very useful for computing the angle between two 3D vectors. The following returns the angle between v1 and v2 where the angle is taken to be counterclockwise about v3 (v3 is assumed to be perpendicular to v1 and v2):

angle = v1.angleTo(v2,v3);

The following functions return a Boolean value (true or false) and may be used inside if statements:

if (v1.isZeroLength())   
if (v1.isParallelTo(v2))
if (v1.isPerpendicularTo(v2)) 

The vector class contains functions for the usual vector operations:

len = v1.dotProduct(v2);
v3 = v1.crossProduct(v2);

The default constructor for a matrix initializes the matrix to the identity matrix:

AcGeMatrix3d   mat1, mat2, mat3;

The following rotates p3 90 degrees about the line defined by p1 and v1:

mat1.setToRotation ( kPi/2.0, v1, p1 );
p3 = mat1 * p2;

A matrix can be inverted if it is not singular:

if (!mat2.isSingular())
    mat2.invert();

The * operator is defined for concatenating matrices:

mat3 = mat1 * mat2;

The following tests whether a matrix contains equal scaling in all three coordinates (that is, it does not change the shape of any entity to which it is applied):

if (mat.isUniScaledOrtho())