designcoding
About Table of Contents Keywords Monthly Archive
Support designcoding!

Vector Arithmetics in Rhino Python

August 20, 2021 | Algorithms
#linear-algebra #rhino-python #vector

Today, I am going to advance the Vector class a bit more. Firstly, I will improve the display method I introduced recently. Then, I will add two new methods which handle the fundamental vector arithmetics in Rhino Python.

Improving the Display Method

In the previous attempt, I displayed vectors on the origin of the Rhino viewport. The coordinates of the tail of a vector are not stored within the object attributes. Normally, I can store two coordinates, one for the tail, one for the tip. That seems logical. But in the future, we are going to see that, the actual length of the vector (the amounts of displacement in x, y, and z) will be the core elements of many calculations. This is why I stored only the tip point information. However, while displaying vectors, an origin point can be used. I am adding this optional functionality in the below code:

	def display(self, origin=[0,0,0]):
		v = self.components
		tip = [v[0]+origin[0], v[1]+origin[1], v[2]+origin[2]]
		line = rs.AddLine(origin, tip)
		rs.CurveArrows(line, 2)
LineExplanation
1Start the definition of the “display” method. This time, the method will take one optional input. This input, “origin” is an optional one, with a default value of [0,0,0].
2This time, I am assigning self.components in a new, temporary variable named “v”. Because I am going to use self.components several times. This makes those lines shorter.
3We define “tip” as a new variable. This variable is a list of three elements (square brackets and elements are separated by a comma). Each element of this list is an addition of the components of the vector, and the elements of the origin (which is another list of three numbers).
4A new variable named “line” is defined. This is the line drawn by rhinoscriptsyntax’s AddLine method. The line is drawn from the optional origin point to the tipping point calculated in the above line.
5Create an arrowhead on the line created in the above line.

The below figure shows the calculation of the vector’s tip point according to the varying origin.

Vector Arithmetics in Rhino Python linear-algebra, rhino-python

Vector Arithmetics: Vector Addition Method

This method will take two vectors, and output their summation as a new vector. This is one of the fundamental operations we will be using in the future. The below figure explains the vector addition procedure. Thus, it is adding one vector’s tail to the other’s tipping point. Basically, it is the addition of the x, y, and z components of the two vectors.

Vector Arithmetics in Rhino Python vector, linear-algebra
	def add(vA, vB):
		v1 = vA.components
		v2 = vB.components
		addition = [v1[0]+v2[0], v1[1]+v2[1], v1[2]+v2[2]]
		return Vector(addition)
LineExplanation
1Start the definition of the “add” method of the Vector class. Here, the method will take two vector objects as inputs. Notice that the method does not take “self” as an input. This makes the add method a “class method”. We are going to call this method, not from an object, (such as object.add()) but from the name of the whole class (Vector.add()).
2A temporary variable v1 is defined, taking the components of the first input vector.
3A temporary variable v2 is defined, taking the components of the second input vector.
4A new variable named “addition” is defined as a list of three numbers. Therefore, each element of this list includes the addition the corresponding elements from v1 and v2.
5The return statement in Python determines which object or data will be transferred to the code that fires this method. For example, the AddLine method in rs returns the ID number of the line object.

Vector Arithmetics: Vector-Scalar Multiplication Method

We will need another important operation: vector-scalar multiplication. When we multiply a vector with a scalar (number), we multiply the components of the vector (coordinates, in our case) by that number. As you can see, the below figure explains this.

Vector Arithmetics in Rhino Python rhino-python, vector
	def multiply(self, s):
		v = self.components
		return Vector([v[0]*s, v[1]*s, v[2]*s])
LineExplanation
1Start the definition of the “multiply” method. The method takes one input, s, a number to be multiplied with the vector (self).
2Define a temporary variable, “v” that keeps the components attribute of the vector.
3Return a new vector object (notice the Vector() constructor) by giving a list of three numbers as an input. This input is calculated by multiplying each coordinate by the s.

Conclusion: Vector Arithmetics

Today, I started studying vector arithmetics and developed new methods for it. Below is the latest code I have been coding since the beginning.

import rhinoscriptsyntax as rs
class Vector:
	def __init__(self, point):
		self.components = point
	def display(self, origin=[0,0,0]):
		v = self.components
		tip = [v[0]+origin[0], v[1]+origin[1], v[2]+origin[2]]
		line = rs.AddLine(origin, tip)
		rs.CurveArrows(line, 2)
	def add(vA, vB):
		v1 = vA.components
		v2 = vB.components
		addition = [v1[0]+v2[0], v1[1]+v2[1], v1[2]+v2[2]]
		return Vector(addition)
	def multiply(self, s):
		v = self.components
		return Vector([v[0]*s, v[1]*s, v[2]*s])

Now, we can test the above code by creating new vector objects and doing arithmetic operations with them.

myvector1 = Vector([3,5,0])
myvector2 = Vector([4,1,0])
myvector3 = Vector.add(myvector1, myvector2)
myvector1.display([5,5,0])
myvector2.display([5,5,0])
myvector3.display([5,5,0])

Cite this post

Yazar, T. (2021, August 20). Vector Arithmetics in Rhino Python. designcoding. Retrieved August 24, 2026, from https://www.designcoding.net/vector-arithmetics-in-rhino-python/

Related Posts

Vector Projection and Angle

December 21, 2021

In this session, I will add two new methods to the Vector class. I think this will finish the basics for the vectors. In the future, we are going to need several new methods like adding multiple vectors and interpolation. But for now, I think this would be sufficient to further advance into parametric curves and surfaces. These new methods will be based on the dot product method we created in the last post. I will add the vector projection…

Vector Normalization and Dot Product

October 29, 2021

This is the continuation of the Vector class we started here, and further advanced here, here, and here. This new Rhino Python implementation is mostly educational and partially a hobby. Before this session, we have developed display, magnitude, add, multiply, reverse, and subtract methods. This time, I am adding the vector normalization and dot product methods and seeing the utilizations of the dot product. Line Explanation 1-26 Already explained in the previous posts. 27 Define a new method called “normalize”….

Vector Magnitude Method in Rhino Python

October 12, 2021

Today, I am going to make only one addition to the Vector class we recently started in Rhino Python. The magnitude of a vector can be easily calculated by assuming that the axes (2 or 3 axes) of it are perpendicular to each other. This gives us an opportunity to assume a right triangle visually, and calculate the magnitude (length) of a vector by using the Pythagorean Theorem. In short, if you square and add the components of a vector,…

More Vector Operations in Rhino Python

September 1, 2021

This is the continuation of my new project of re-creating the parametric curve and surface methods of Rhino via Python scripting. If you remember, I started with the building block of vector operations, here and here. Then, I defined vector addition and multiplication, before going deeper into the geometric calculations. In fact, they are using the previously defined addition and multiplication methods. New Vector Operations: Subtraction and Reversing In the future, we are going to need to reverse a vector,…

Display Method for Vector Class

August 17, 2021

Let’s continue from the Vector class that started yesterday. Previously, I defined this class to store three numbers (coordinates), named as “components”. I defined a method named __init__ for this. Similarly, I am adding a display method to the Vector class today. Note that I am using Rhino 6 in this code, but it should also work in Rhino 5 or 7. The code Below is the line-by-line explanation of the method: Line Explanation 1-4 Already explained before. 5 Start…

  • Chapters

    • Algorithms
    • Discourses
    • Fabrications
    • Studios
  • Explore

    • All Keywords
    • Table of Contents
    • Monthly Archive
    • #grasshopper
    • #parametric-curve
    • #tutorial
    • #parametric-surface
    • #polyhedra
    • #bezier-curve
    • #terrain
    • #design-education
    • #simulation
    • #fourier
    • #dual
    • #art
    • #design-object
    • #image-sampler
    • #growth
    • #vector-field
    • #circle
    • #goldberg-polyhedra
    • #calculus
    • #music
  • Search

  • Support designcoding!

  • Enjoying designcoding? Support me on Patreon to keep it growing. Thank you!

  • copyright 2026 designcoding.net | about | privacy policy | end user license agreement