Vector Arithmetics in Rhino Python

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 components

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 arithmetic: addition
	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: multiplication
	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])


Design Mathematics | Linear Algebra | Rhino Python || vector addition | vector multiplication
Print this post
August 20, 2021
Views: 2864


« Display Method for Vector Class
On the Digital ReConstruction of Muqarnas »



       
       
  • Search

  • Categories

    • Education
      • Basic Design
      • Design Geometry
      • Design Mathematics
      • Digital Fabrication
      • Parametric Modeling
      • Tutorials
    • Philosophy
      • Phenomenology
      • Philosophy of Language
    • Practice
      • 3D Models
      • Projects
      • Publications
      • Workshops
    • Research
      • 3D Printing
      • Building Facade
      • Calculus
      • Climate Analysis
      • Compass Constructions
      • Computational Geometry
      • Curves
      • Decorative Arts
      • Digital Fabrication
      • Evolutionary Solvers
      • Folding Structures
      • Fractals
      • Graph Theory
      • Interlocking Structures
      • Islamic Patterns
      • Linear Algebra
      • Minimal Surfaces
      • Muqarnas
      • Non-Euclidean Geometry
      • Paneling
      • Parametric Curves
      • Parametric Objects
      • Parametric Surfaces
      • Pattern Deformations
      • Patterns
      • Pavilions
      • Polyhedra
      • Rammed Earth Structures
      • Robotic Fabrication
      • Shape Grammars
      • Simulation
      • Space Syntax
      • Surface Constructions
      • Tessellations
      • Tools
      • Vector Fields
      • Virtual Reality
    • Tools and Languages
      • 3DS Max
      • 3DS Max Script
      • Grasshopper
      • Photoshop
      • Physical Prototyping
      • Revit
      • Rhino
      • Rhino Macro
      • Rhino Python
      • Rhino Script
      • Unity
  • Monthly Archive

    • May 2025 (2)
    • April 2025 (5)
    • December 2024 (40)
    • August 2024 (5)
    • July 2024 (6)
    • April 2024 (4)
    • March 2024 (10)
    • February 2024 (10)
    • January 2024 (8)
    • December 2023 (10)
    • August 2023 (3)
    • July 2023 (3)
    • June 2023 (7)
    • May 2023 (8)
    • April 2023 (7)
    • March 2023 (2)
    • February 2023 (2)
    • January 2023 (3)
    • December 2022 (6)
    • November 2022 (7)
    • January 2022 (1)
    • December 2021 (1)
    • October 2021 (3)
    • September 2021 (4)
    • August 2021 (4)
    • May 2019 (2)
    • April 2019 (1)
    • March 2019 (5)
    • January 2019 (2)
    • December 2018 (1)
    • November 2018 (4)
    • October 2018 (9)
    • July 2018 (1)
    • June 2018 (4)
    • May 2018 (1)
    • April 2018 (4)
    • February 2018 (2)
    • January 2018 (7)
    • August 2017 (9)
    • July 2017 (6)
    • October 2016 (1)
    • May 2015 (5)
    • April 2015 (8)
    • March 2015 (12)
    • February 2015 (4)
    • January 2015 (11)
    • November 2014 (1)
    • August 2014 (1)
    • June 2014 (2)
    • May 2014 (12)
    • April 2014 (5)
    • March 2014 (3)
    • February 2014 (6)
    • January 2014 (4)
    • December 2013 (5)
    • November 2013 (11)
    • October 2013 (2)
    • September 2013 (9)
    • August 2013 (4)
    • July 2013 (2)
    • June 2013 (14)
    • May 2013 (4)
    • April 2013 (10)
    • March 2013 (11)
    • February 2013 (11)
    • January 2013 (10)
    • December 2012 (10)
    • November 2012 (6)
    • October 2012 (13)
    • September 2012 (2)
    • August 2012 (5)
    • July 2012 (14)
    • June 2012 (6)
    • May 2012 (17)
    • April 2012 (15)
    • March 2012 (9)
    • February 2012 (16)
    • January 2012 (18)
    • December 2011 (20)
    • November 2011 (2)
  • Keywords

      3d printing . accuracy . add-on development . aluminium mesh . aluminium wire . anemone . angle . animate form . animation . apartment . aperiodic . approximation . archimedean . archimedean solid . archimedean spiral . architecture . arduino . area . array . ascii . attractor . award . b-spline . baklava . baldaquin . bambu . basic design . basis spline . basketball . Beginner . bend . bezier . bim . bitmap . blob . boolean . brick . bspline . buckminster fuller . buckminsterfuller . buckyball . building regulations . cage-edit . cairopentagonal . calatrava . calculus . canopy . cardboard . card design . cartesian house . casting . catalan solid . cellular . ceramic . cesaro . chamfer . chaos . chopsticks . circle . circle packing . closed . clusters . cnc cutting . color . column . compass . complex number . component . computation . computational design . computational geometry . computerization . concepts . constructivism . contouring . control points . convex hull . cost analysis . crane . crossover . cube . cura . curvature . curve . cycloid . dataflow . dataflow diagram . dataflow management . data list . data recorder . data tree . deboor . decasteljau . deformation . delaunay . deleuze . derivative . descartes . design competition . design contest . designcontest . design education . design exercises . design studio . diagram . digital design . digital fabrication . digital studio . dijkstra . display . divide . dodecahedron . dome . dot product . doyle . doyle spiral . dragon curve . dual . dwg . dymaxion . dynamic . dürer . edge bundling . education . egg-crate . ellipsoid . elongated . emergency . emergent . enneahedron . enneper surface . entrance . epicycles . equation . escher . euclid . euclidean construction . evolution door . excavated dodecahedron . excel . exhibition . fabrication . fabrik . facade . fermat . fibonacci . field . field lines . firefly . flange . flaps . flocking . flow . folding . font . force field . fourier . fractal . function . function curves . galapagos . game engine . gaudi . gaussian curvature . generative components . genetic algorithms . geodesic . geometry . gestalt . girih . goldberg . golden ratio . gosper . graph . graphic design . graph mapper . Grasshopper . grasshopper python . grid . growth . guitar . gyroid . hatch . helix . hendecahedron . herringbone . herschelsenneahedron . hexagon . hilbert . holomorphic . hoopsnake . hose . hotwire cutter . hypar . hyperbolic . hyperbolic space . hyperboloid . ice-ray . icosahedron . icosidodecahedron . image . image sampler . imagesampler . image sampling . interior design . interlocking . inverse kinematics . iqlight . islamic pattern . isovist . istanbul . iteration . ivy . julia . julia set . kagome . kangaroo . kinetic . kirigami . koch . kuka . kündekari . l-systems . ladybug . lamp . lanterns . laser . laser cutting . lattice . layout . leap motion . le corbusier . lecorbusier . leveling . lissajous . lissajous curve . lituus . lokma . loop . lowpoly . macro . mandelbrot . mantı . map . material . mathematics . maxscript . mecon . mesh . metaball . metamorphosis . mihrimahsultan . minimal surface . minimum spanning tree . mirror . miura ori . modeling . modulardesign . moebius . molding . monkey saddle . morph . motion . mug . muqarnas . musicxml . möbius . natural stone . nature . nesting . nexus . ngrid . noise . non-euclidean . normal . normalization . nurbs . nuts and bolts . object classes . occlusion . octahedron . ontology . opennest . origami . packing . paradigm shift . parametric . parametric design . parametric modeling . parametric object . parametric roof . parametric surface . parametric wall . parquet deformation . patch . pattern . pavilion . pedagogy . pendentive . penrose . pentagon . perception . performance . perlin . perlin noise . permaculture . philosophy . photoshop . phyllotaxis . pipe . planar . plane . planter . plaster . platonic solid . point . polygon . polyhedra . polyline . porous . poster . potplus . precast concrete . precision . printing . processing . projection . prototile . prototiling . prototypes . puzzle . pvc hose . pvc pipe . pyramid . python . qshaper . rammed earth . random . raytrace . record history . region . reptile . responsive . reverse vector . reversing vector . revit . revit family . rhino . rhinonest . rhinopython . rhinoscript . rhombicosidodecahedron . rhombus . riemann . risingchair . rivet . robot . robotic arm . robotic fabrication . roof . rubber band . rule-based design . ruled surface . rumi . savoye . science . section . seljuk muqarnas . semi regular . shape grammars . shapeshifting . shortestpath . sierpinski . signal . sinan . sine . sketch . skin . slope . snowflake . snub . snubsquare . socolar . sofa . software development . solar position . solid . sound . space-filling . spacechase . spacefilling . space syntax . spatial allocation . spec . sphenoidhendecahedron . sphere . spiral . spline . square . star . stellated . stellated icosahedron . stellation . string . stripe . structure . student works . subdivision . subsurface . surface . surface paneling . survey . sweep . symbiosis . süleymaniye . table . taenia . tangent . tattoo . technology . tensegrity . terrain . tessellation . tetrahedron . tetrakaidecahedron . text . textile . the primitive hut . tiling . timer . toolbar . tool calibration . topography . topology . transformation . tree . triangle . triangulation . truchet . truncated cuboctahedron . truncatedicosahedron . truncated icosidodecahedron . truncated octahedron . truncated tetrahedron . truss . tube . twisted tower . unit vector . unity . unroll . variation . vasari . vb.net . vbnet . vector . vector addition . vectorfield . vector magnitude . vector multiplication . vector normalization . vectors . vector subtraction . villasavoye . virtual reality . visualization . visual programming . void . voronoi . waffle . waterbomb . water cube . wave . weaire-phelan . webcam . william huff . wind . window . wood . wood stick . wood sticks . Workshop . zumthor

               
copyright 2025 designcoding.net | about designcoding | privacy policy | sitemap | end-user license agreement