Vectors


Image 1: A 2D vector positioned at 5 units on the x and 2 units on the y.

A vector is a coordinate that represents a position in space. In MAXScript, vectors are implemented through point2 and point3 objects. Most frequently, you will be dealing with point3 objects.

Components

A vector is composed of 1 component for each dimension in their respective space. Each component represents a specific point in that respective dimension. For example, the x component in a vector indicates the position of the vector in the x dimension. Image 1 above shows a 2-dimensional (2D) vector that lies at the coordinate (5, 2) (that is, 5 on the x and 2 on the y).

Constructors

In MAXScript, vectors are implemented through the point2 and point3 objects. Point2 objects represent a 2D vector that has 2 components, an x and a y. In MAXScript, these are constructible via the square braces syntax.

Eg:

2d_vector = [50, 75]

The code segment above constructs a point2 object with x value 50 and y value 75.

Similarly, we can construct point3 objects. Eg:

3d_vector = [25, 50, 75]

The code segment above constructs a point3 object with x value 25, y value 50, and z value 75.

Point2 and point3 objects share most operations. For the remainder of this document, the focus will be on point3 objects.

Component Access

We can access the components of a vector via their component names. Eg:

a = [10, 20, 30]
a.x = 15 /* Set x value to 15 */
b = a.y /* Read y value and assign it to b which is now set to 20 */

Operators

Vectors can perform basic mathematical operations, eg:

a = [10, 20, 30]
b = [5, 10, 15]
c = a + b /* c is [15, 30, 45] */
c = b - a /* c is [-5, -10, -15] */
a += b /* a is [15, 30, 45] */
a -= b /* a is [10, 20, 30] */

Vectors can also be scaled by a float, eg:

a = [3, 6, 9]
b = a * 3 /* b is [9, 18, 27] */
b = a / 3 /* b is [1, 2, 3] */
a *= 3 /* a is [9, 18, 27] */
a /= 3 /* a is [1, 2, 3] */

Functions

length

The length of a vector is the distance from the origin (the point zero on all dimensions) to the tip. We can find the length of a vector by calling the length function. Eg:

a = length [30, 0, 0] /* a is 30 */
a = length [0, 1, 0] /* a is 1 */
a = length [1, 0, 1] /* a is 1.41421 */
a = length [1, 1, 1] /* a is 1.73205 */

normalize

The direction of a vector is the direction from the point of origin to the tip of the vector. A pure direction vector is a vector whose length is 1; we call this a normalized vector or a unit length vector. For example, the vectors [10, 0, 0] and [1, 0, 0] share the same direction but have different lengths; [1, 0, 0] is a unit length vector while [10, 0, 0] is not. To get a normalized vector from any non-zero length vector, we call the normalize function.

Eg:

a = normalize [10, 0, 0] /* a is [1, 0, 0] */
a = normalize [1, 0, 0] /* a is [1, 0, 0] since source vector is already unit length */
a = normalize [1, 0, 1] /* a is [0.707107,0,0.707107] */
a = normalize [1, 1, 1] /* a is [0.57735,0.57735,0.57735] */

Reference

For more information, see the point3 maxscript api.