1 Vector Mathematics

This guide discusses vector math including vector representation, vector operation, and line and plane equations.

A vector indicates a quantity, such as velocity or force, that has direction and length. Vectors in 3D coordinate systems are represented with an ordered set of three real numbers and look like:

1.1 Vector representation

In this document, lower case bold letters with arrow on top will notate vectors. Vector components are also enclosed in angle brackets. Upper case letters will notate points. Point coordinates will always be enclosed by parentheses.

Using a coordinate system and any set of anchor points in that system, we can represent or visualize these vectors using a line-segment representation. An arrowhead shows the vector direction.

For example, if we have a vector that has a direction parallel to the x-axis of a given 3D coordinate system and a length of 5 units, we can write the vector as follows:

To represent that vector, we need an anchor point in the coordinate system. For example, all of the arrows in the following figure are equal representations of the same vector despite the fact that they are anchored at different locations.

Figure (1): Vector representation in the 3-D coordinate system.

Given a 3D vector , all vector components , , are real numbers. Also all line segments from a point to point are equivalent representations of vector .

So, how do we define the end points of a line segment that represents a given vector? Let us define an anchor point (A) so that:

And a vector:

The tip point of the vector is calculated by adding the corresponding components from anchor point and vector :



Figure (2): The relationship between a vector, the vector anchor point, and the point coinciding with the vector tip location.

Position vector

One special vector representation uses the as the vector anchor point. The position vector is represented with a line segment between two points, the origin and the tip point B, so that:


Figure (1): Vector representation in the 3-D coordinate system.

A position vector for a given vector is a special line segment representation from the origin point to point .

Vectors vs. points

Do not confuse vectors and points. They are very different concepts. Vectors, as we mentioned, represent a quantity that has direction and length, while points indicate a location. For example, the North direction is a vector, while the North Pole is a location (point). If we have a vector and a point that have the same components, such as:


We can draw the vector and the point as follows:

Figure (4): A vector defines a direction and length. A point defines a location.

Vector length

As mentioned before, vectors have length. We will use to notate the length of a given vector . For example:



In general, the length of a vector is calculated as follows:

Figure (5): Vector length.

Unit vector

A unit vector is a vector with a length equal to one unit. Unit vectors are commonly used to compare the directions of vectors.

A unit vector is a vector whose length is equal to one unit.

To calculate a unit vector, we need to find the length of the given vector, and then divide the vector components by the length. For example:


  
  

If of , then:
  
  
  
  
  

In general:

The unit vector of

Figure (6): Unit vector equals one-unit length of the vector.

1.2 Vector operations

Vector scalar operation

Vector scalar operation involves multiplying a vector by a number. For example:



Figure (7): Vector scalar operation

In general, given vector , and a real number

Vector addition

Vector addition takes two vectors and produces a third vector. We add vectors by adding their components.

Vectors are added by adding their components.

For example, if we have two vectors:




Figure (8): Vector addition.

In general, vector addition of the two vectors a and b is calculated as follows:



Vector addition is useful for finding the average direction of two or more vectors. In this case, we usually use same-length vectors. Here is an example that shows the difference between using same-length vectors and different-length vectors on the resulting vector addition:

Figure (9): Adding various length vectors (left). Adding same length vectors (right) to get the average direction.

Input vectors are not likely to be same length. In order to find the average direction, you need to use the unit vector of input vectors. As mentioned before, the unit vector is a vector of that has a length equal to 1.

Vector subtraction

Vector subtraction takes two vectors and produces a third vector. We subtract two vectors by subtracting corresponding components. For example, if we have two vectors and and we subtract from , then:




If we subtract from , we get a different result:


Note that the vector has the same length as the vector , but goes in the opposite direction.

Figure (10): Vector subtraction.

In general, if we have two vectors, and , then is a vector that is calculated as follows:



Vector subtraction is commonly used to find vectors between points. So if we need to find a vector that goes from the tip point of the position vector to the tip point of the position vector , then we use vector subtraction as shown in Figure (11).

Figure (11): Use vector subtraction to find a vector between two points.

Vector properties

There are eight properties of vectors. If a, b, and c are vectors, and s and t are numbers, then:








Vector dot product

The dot product takes two vectors and produces a number. For example, if we have the two vectors a and b so that:


Then the dot product is the sum of multiplying the components as follows:


In general, given the two vectors a and b:



We always get a positive number for the dot product between two vectors when they go in the same general direction. A negative dot product between two vectors means that the two vectors go in the opposite general direction.

Figure (12): When the two vectors go in the same direction (left), the result is a positive dot product. When the two vectors go in the opposite direction (right), the result is a negative dot product.

When calculating the dot product of two unit vectors, the result is always between 1 and +1. For example:



In addition, the dot product of a vector with itself is equal to that vector’s length to the power of two. For example:



Calculating the square length of vector :



Vector dot product, lengths, and angles

There is a relationship between the dot product of two vectors and the angle between them.

The dot product of two non-zero unit vectors equals the cosine of the angle between them.

In general:

, or

Where:

is the angle included between the vectors.

If vectors a and b are unit vectors, we can simply say:

And since the cosine of a 90-degree angle is equal to 0, we can say:

Vectors and are orthogonal if, and only if, .

For example, if we calculate the dot product of the two orthogonal vectors, World xaxis and yaxis, the result will equal zero.




There is also a relationship between the dot product and the projection length of one vector onto another. For example:





Figure (13): The dot product equals the projection length of one vector onto a non-zero unit vector.

In general, given a vector a and a non-zero vector b, we can calculate the projection length pL of vector a onto vector b using the dot product.


Dot product properties

If , , and are vectors and s is a number, then:





Vector cross product

The cross product takes two vectors and produces a third vector that is orthogonal to both.

Figure (14): Calculating the cross product of two vectors.

For example, if you have two vectors lying on the World xy-plane, then their cross product is a vector perpendicular to the xy-plane going either in the positive or negative World z-axis direction. For example:




The vector is orthogonal to both and .

You will probably never need to calculate a cross product of two vectors by hand, but if you are curious about how it is done, continue reading; otherwise you can safely skip this section. The cross product is defined using determinants. Here is a simple illustration of how to calculate a determinant using the standard basis vectors:



The cross product of the two vectors and is calculated as follows using the above diagram:



Cross product and angle between vectors

There is a relationship between the angle between two vectors and the length of their cross product vector. The smaller the angle (smaller sine); the shorter the cross product vector will be. The order of operands is important in vectors cross product. For example:




Figure (15): The relationship between the sine of the angle between two vectors and the length of their cross product vector.

In Rhino’s right-handed system, the direction of is given by the right-hand rule (where = index finger, = middle finger, and = thumb).

In general, for any pair of 3-D vectors and :

Where:

is the angle included between the position vectors of and

If a and b are unit vectors, then we can simply say that the length of their cross product equals the sine of the angle between them. In other words:

The cross product between two vectors helps us determine if two vectors are parallel. This is because the result is always a zero vector.

Vectors and are parallel if, and only if, .

Cross product properties

If , , and are vectors, and is a number, then:





1.3 Vector equation of line

The vector line equation is used in 3D modeling applications and computer graphics.

Figure (16): Vector equation of a line.

For example, if we know the direction of a line and a point on that line, then we can find any other point on the line using vectors, as in the following:


line direction unit vector
line position point
any point on the line

We know that:

(2)
(1)

From 1 and 2:

(3)

However, we can write (3) as follows:


Therefore:



Which is the same as:

Given a point and a direction on a line, any point on that line can be calculated using the vector equation of a line where is a number.

Another common example is to find the midpoint between two points. The following shows how to find the midpoint using the vector equation of a line:

is the position vector for point
is the position vector for point
is the vector going from

From vector subtraction, we know that:

From the line equation, we know that:

And since we need to find midpoint, then:

Hence we can say:

Figure (17): Find the midpoint between two input points.

In general, you can find any point between and by changing the value between 0 and 1 using the general equation:

Given two points and , any point between the two points is calculated using the equation where t is a number between 0 and 1.

1.4 Vector equation of a plane

One way to define a plane is when you have a point and a vector that is perpendicular to the plane. That vector is usually referred to as normal to the plane. The normal points in the direction above the plane.

One example of how to calculate a plane normal is when we know three non-linear points on the plane.

In Figure (16), given:

= the first point on the plane
= the second point on the plane
= the third point on the plane

And:

= a position vector of point
= a position vector of point
= a position vector of point

We can find the normal vector as follows:

Figure (18): Vectors and planes

We can also derive the scalar equation of the plane using the vector dot product:

If:



Then we can expand the above:

Solving the dot product gives the general scalar equation of a plane:

1.5 Tutorials

All the concepts we reviewed in this chapter have a direct application to solving common geometry problems encountered when modeling. The following are stepbystep tutorials that use the concepts learned in this chapter using Rhinoceros and Grasshopper (GH).

1.5.1 Face direction

Given a point and a surface, how can we determine whether the point is facing the front or back side of that surface?

Input:

  1. a surface
  2. a point

Parameters:

The face direction is defined by the surface normal direction. We will need the following information:

  • The surface normal direction at a surface location closest to the input point.
  • A vector direction from the closest point to the input point.

Compare the above two directions, if going the same direction, the point is facing the front side, otherwise it is facing the back.

Solution:

1. Find the closest point location on the surface relative to the input point using the Pull component. This will give us the uv location of the closest point, which we can then use to evaluate the surface and find its normal direction.

2. We can now use the closest point to draw a vector going towards the input point. We can also draw:

3. We can compare the two vectors using the dot product. If the result is positive, the point is in front of the surface. If the result is negative, the point is behind the surface.

The above steps can also be solved using other scripting languages. Using the Grasshopper VB component:

Private Sub RunScript(ByVal pt As Point3d, ByVal srf As Surface, ByRef A As Object)

  'Declare variables
  Dim u, v As Double
  Dim closest_pt As Point3d

  'get closest point u, v
  srf.ClosestPoint(pt, u, v)

  'get closest point
  closest_pt = srf.PointAt(u, v)

  'calculate direction from closest point to test point
  Dim dir As New Vector3d(pt - closest_pt)

  'calculate surface normal
  Dim normal = srf.NormalAt(u, v)

  'compare the two directions using the dot product
  A = dir * normal
End Sub

Using the Grasshopper Python component with RhinoScriptSyntax:

import rhinoscriptsyntax as rs #import RhinoScript library

#find the closest point
u, v = rs.SurfaceClosestPoint(srf, pt)

#get closest point
closest_pt = rs.EvaluateSurface(srf, u, v)

#calculate direction from closest point to test point
dir = rs.PointCoordinates(pt) - closest_pt

#calculate surface normal
normal = rs.SurfaceNormal(srf, [u, v])

#compare the two directions using the dot product
A = dir * normal

Using the Grasshopper Python component with RhinoCommon only:

#find the closest point
found, u, v = srf.ClosestPoint(pt)

if found:

    #get closest point
    closest_pt = srf.PointAt(u, v)

    #calculate direction from closest point to test point
    dir = pt - closest_pt

    #calculate surface normal
    normal = srf.NormalAt(u, v)

    #compare the two directions using the dot product
    A = dir * normal

Using the Grasshopper C# component:

private void RunScript(Point3d pt, Surface srf, ref object A)
{
  //Declare variables
  double u, v;
  Point3d closest_pt;

  //get closest point u, v
  srf.ClosestPoint(pt, out u, out v);

  //get closest point
  closest_pt = srf.PointAt(u, v);

  //calculate direction from closest point to test point
  Vector3d dir = pt - closest_pt;

  //calculate surface normal
  Vector3d normal = srf.NormalAt(u, v);

  //compare the two directions using the dot product
  A = dir * normal;
}

1.5.2 Exploded box

The following tutorial shows how to explode a polysurface. This is what the final exploded box looks like:

Input:

Identify the input, which is a box. We will use the Box parameter in GH:

Parameters:

  • Think of all the parameters we need to know in order to solve this tutorial.
  • The center of explosion.
  • The box faces we are exploding.
  • The direction in which each face is moving.

Once we have identified the parameters, it is a matter of putting it together in a solution by piecing together the logical steps to reach an answer.

Solution:

1. Find the center of the box using the Box Properties component in GH:

2. Extract the box faces with the Deconstruct Brep component:

3. The direction we move the faces is the tricky part. We need to first find the center of each face, and then define the direction from the center of the box towards the center of each face as follows:

4. Once we have all the parameters scripted, we can use the Move component to move the faces in the appropriate direction. Just make sure to set the vectors to the desired amplitude, and you will be good to go.

The above steps can also be solved using VB script, C# or Python. Following is the solution using these scripting languages.

Using the Grasshopper VB component:

Private Sub RunScript(ByVal box As Brep, ByVal dis As Double, ByRef A As Object)

    'get the brep center
    Dim area As Rhino.Geometry.AreaMassProperties
    area = Rhino.Geometry.AreaMassProperties.Compute(box)

    Dim box_center As Point3d
    box_center = area.Centroid

    'get a list of faces
    Dim faces As Rhino.Geometry.Collections.BrepFaceList = box.Faces

    'decalre variables
    Dim center As Point3d
    Dim dir As Vector3d
    Dim exploded_faces As New List( Of Rhino.Geometry.Brep )
    Dim i As Int32
    'loop through all faces

    For i = 0 To faces.Count() - 1
      'extract each of the face
      Dim extracted_face As Rhino.Geometry.Brep = box.Faces.ExtractFace(i)

      'get the center of each face
      area = Rhino.Geometry.AreaMassProperties.Compute(extracted_face)
      center = area.Centroid

      'calculate move direction (from box centroid to face center)
      dir = center - box_center
      dir.Unitize()
      dir *= dis

      'move the extracted face
      extracted_face.Transform(Transform.Translation(dir))

      'add to exploded_faces list
      exploded_faces.Add(extracted_face)
    Next

    'assign exploded list of faces to output
    A = exploded_faces
  End Sub

Using the Grasshopper Python component with RhinoCommon:

import Rhino #import RhinoCommon module

#find a point between A and B
D = A + t * (B - A)

#find mid point between A and D
C1 = A + 0.5 * (D - A)

#find mid point between D and B
C2 = D + 0.5 * (B - D)

#find spheres radius
r1 = A.DistanceTo(C1)
r2 = B.DistanceTo(C2)

#create spheres and assign to output
S1 = Rhino.Geometry.Sphere(C1, r1)
S2 = Rhino.Geometry.Sphere(C2, r2)

Using the Grasshopper C# component:

private void RunScript(Brep box, double dis, ref object A)
{

    //get the brep center
  Rhino.Geometry.AreaMassProperties area =        Rhino.Geometry.AreaMassProperties.Compute(box);
  Point3d box_center = area.Centroid;

  //get a list of faces
  Rhino.Geometry.Collections.BrepFaceList faces = box.Faces;

  //decalre variables
  Point3d center;   Vector3d dir;
  List<Rhino.Geometry.Brep> exploded_faces = new List<Rhino.Geometry.Brep>();

  //loop through all faces   for( int i = 0; i < faces.Count(); i++ )
  {
    //extract each of the face
    Rhino.Geometry.Brep extracted_face = box.Faces.ExtractFace(i);

    //get the center of each face
    area = Rhino.Geometry.AreaMassProperties.Compute(extracted_face);
    center = area.Centroid;

    //calculate move direction (from box centroid to face center)
    dir = center - box_center;
    dir.Unitize();
    dir *= dis;

    //move the extracted face
    extracted_face.Transform(Transform.Translation(dir));

    //add to exploded_faces list
    exploded_faces.Add(extracted_face);
  }

  //assign exploded list of faces to output
  A = exploded_faces;
}

1.5.3 Tangent spheres

This tutorial will show how to create two tangent spheres between two input points. This is what the result looks like:

Input:
Two points ( and ) in the 3-D coordinate system.

Parameters: The following is a diagram of the parameters that we will need in order to solve the problem: tangent point  between the two spheres, at some  parameter (0-1) between points  and .

  • The center of the first sphere or the midpoint  between  and .
  • The center of the second sphere or the midpoint  between  and .
  • The radius of the first sphere or the distance between  and .
  • The radius of the second sphere or the distance between  and .

Solution:

1. Use the Expression component to definepoint between and atsome parameter . The expression we will use is based onthe vector equation of a line:

$$B-ABA (\vec{BA}) using the vector subtraction operation.

$$t*(B-A)t$$ is between 0 and 1 to get us a location on the vector.

$$A+t*(B-A)$$ : gets apoint on the vector between A and B.

2. Use the Expression component to also define the mid points  and .

3. The first sphere radius and the second sphere radius can be calculated using the Distance component.

4. The final step involves creating the sphere from a base plane and radius. We need to make sure the origins are hooked to  and  and the radius from  and .

Using the Grasshopper VB component:

Private Sub RunScript(ByVal A As Point3d, ByVal B As Point3d, ByVal t As Double, ByRef S1 As Object, ByRef S2 As Object)

  'declare variables
  Dim D, C1, C2 As Rhino.Geometry.Point3d
  Dim r1, r2 As Double

  'find a point between A and B
  D = A + t * (B - A)

  'find mid point between A and D
  C1 = A + 0.5 * (D - A)

  'find mid point between D and B
  C2 = D + 0.5 * (B - D)
  'find spheres radius
  r1 = A.DistanceTo(C1)
  r2 = B.DistanceTo(C2)

  'create spheres and assign to output
  S1 = New Rhino.Geometry.Sphere(C1, r1)
  S2 = New Rhino.Geometry.Sphere(C2, r2)

End Sub

Using Python component:

import Rhino

#find a point between A and B
D = A + t * (B - A)

#find mid point between A and D
C1 = A + 0.5 * (D - A)

#find mid point between D and B
C2 = D + 0.5 * (B - D)

#find spheres radius
r1 = A.DistanceTo(C1)
r2 = B.DistanceTo(C2)

#create spheres and assign to output
S1 = Rhino.Geometry.Sphere(C1, r1)
S2 = Rhino.Geometry.Sphere(C2, r2)

Using the Grasshopper C# component:

private void RunScript(Point3d A, Point3d B, double t, ref object S1, ref object S2)
{
  //declare variables
  Rhino.Geometry.Point3d D, C1, C2;
  double r1, r2;

  //find a point between A and B
  D = A + t * (B - A);

  //find mid point between A and D
  C1 = A + 0.5 * (D - A);

  //find mid point between D and B
  C2 = D + 0.5 * (B - D);

  //find spheres radius
  r1 = A.DistanceTo(C1);
  r2 = B.DistanceTo(C2);

  //create spheres and assign to output
  S1 = new Rhino.Geometry.Sphere(C1, r1);
  S2 = new Rhino.Geometry.Sphere(C2, r2);
}

Download Sample Files

Download the math-samplesandtutorials.zip archive, containing all the example Grasshopper and code files in this guide.


Next Steps

Now that you know vector math, check out the Matrices and Transformations guide to learn more about the moving, rotating and scaling objects..