Vectors


Geometric definition


A vector is an abstract object (It doesn’t represent a “physical” object such as a line or a square), that has:


Think of a force: it is not something you see (we can only see the EFFECTS of a force), yet it has a direction, and a magnitude.

Vectors are usually represented by the coordinates of the end of the vector, the origin of the vector being at (0, 0, 0).

In Rhino, vectors are used essentially to construct and move objects, or to represent the normal to a surface, circle…

How to add


RhinoCommons


As is the case with a lot of objects in RhinoCommon, there are different ways we can create a vector. The most useful are:

import Rhino

# Coordinates of the end of the vector
x = 1
y = 2
z = 3

# Will create a vector with origin at (0, 0, 0) and end at (x, y, z)
my_vector = Rhino.Geometry.Vector3d(x, y, z)


import Rhino

#Point to use as end of the vector
point_A = Rhino.Geometry.Point3d(1, 1, 1)

# Will create a vector with (0, 0, 0) as origin and the point "point_A" as end
my_vector = Rhino.Geometry.Vector3d(point_A)


RhinoScriptSyntax


import rhinoscriptsyntax as rs

# Points to use as origin and end of the vector
point_A = rs.AddPoint(0, 0, 0)
point_B = rs.AddPoint(10, 10, 10)

# Will create a vector with origin at "point_A" and end at "point_B"
vector = rs.VectorCreate(point_A, point_B)

print(vector)

Main methods, properties and operators


Methods


Among the main method we have:

vector_A = Rhino.Geometry.Vector3d(1, 0, 0)
vector_B = Rhino.Geometry.Vector3d(0, 1, 1)

# Will create a vector by adding the coordinates of the two vectors p1 and p2
vector_C = Rhino.Geometry.Vector3d.Add(vector_A, vector_B)

# Will calculate the cross-product between v1 and v2
vector_C = Rhino.Geometry.Vector3d.CrossProduct(vector_A, vector_B)

# Will verify if a vector is parallel to another, within a given angular tolerance
is_parallel = vector_B.IsParallelTo(vector_A, 0.01)
print(is_parallel)

Properties


vector_A = Rhino.Geometry.Vector3d(1, 0, 0)

# Will calculate the length (or magnitude) of a vector:
length = vector_A.Length

# Will retrieve the X coordinate of the vector:
x = vector_A.X

# Will retrieve the Y coordinate of the vector:
y = vector_A.Y

# Will retrieve the Z coordinate of the vector:
z = vector_A.Z

# Will check if a vector is of length = 1
is_unit = vector_A.IsUnitVector
print(is_unit)

Operators:


Vector objects also have some very useful operators. Here are a few of them:

vector_A = Rhino.Geometry.Vector3d(1, 0, 0)
vector_B = Rhino.Geometry.Vector3d(0, 1, 1)

# Will add the coordinates of v1 and v2 respectively, to create the vector v3
vector_C = vector_A + vector_B

# Will substract the coordinates of v1 and v2 respectively, to create the vector v3
vector_C = vector_A - vector_B

# Will compute the dot product of v1 and v2, then print the result
dot_product = vector_A * vector_B
print(dot_product)

# Will multiply the coorinates of v1 by 2
vector_C = vector_B * 2

# Will divide the coorinates of v1 by 2
vector_C = vector_B / 2

# Will compute the opposite vector to v1
vector_C = -vector_A


🛠 Exercises


01: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍

Solutions:

01: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍