27 May, 2019

## Working With Vectors

Working with mathematical vectors in code is valuable. It requires only math calculation, and a code executing time is faster than a code dependent on a geometrical object attributes.

Maher showed, in a previous post how to extract direction components from an AxisSystem matrix using EKL. In this post, I'll dig deeper into how Vectors work with examples using EKL within a Knowledge Pattern; and within a script node in xGenerative Design.

### Vectors

A Vector is an element that has both magnitude and direction. In EKL, there are two Vector orientations ( column and row).

let v (Vector)

//vector in column
v = [ 0; 0; 1 ]

//vector in row
v = [ 0, 0, 1 ]



It is important to know these types when you get x, y, z attributes of vectors

//column vector
let x, y, z (Real)
x = v.Get(1,1) //x value of v
y = v.Get(2,1) //y value of v
z = v.Get(3,1) //z value of v

//row vector
x = v.Get(1,1) //x value of v
y = v.Get(1,2) //y value of v
z = v.Get(1,3) //z value of v



Also, a column vector will be created by using the method that transforms Direction into a vector.

let oPlane (Plane)

v = direction(oPlane).Vector()

let oLine (Line)

v = direction(oLine).Vector()



Note: a vector create by 3 Real numbers in xGen will be a row vector

Here are some examples of vector functions.

let v1, v2 (Vector) //input (column vector)
let ReturnVec (Vector) //output (column vector)

let x, y, z (Real)
x = v1.Get(1,1) + v2.Get(1,1)
y = v1.Get(2,1) + v2.Get(2,1)
z = v1.Get(3,1) + v2.Get(3,1)

ReturnVec = [ x; y; z ]



Vector Subtract

x = v1.Get(1,1) - v2.Get(1,1)
y = v1.Get(2,1) - v2.Get(2,1)
z = v1.Get(3,1) - v2.Get(3,1)

ReturnVec = [ x; y; z ]



Vector Scale

x = v1.Get(1,1)*R
y = v1.Get(2,1)*R
z = v1.Get(3,1)*R

ReturnVec = [ x; y; z ]



CrossProduct (already exist in EKL, but just in case)

let CrossProduct (Vector) //output
x = v1.Get(2,1)*v2.Get(3,1) - v1.Get(3,1)*v2.Get(2,1)
y = v1.Get(3,1)*v2.Get(1,1) - v1.Get(1,1)*v2.Get(3,1)
z = v1.Get(1,1)*v2.Get(2,1) - v1.Get(2,1)*v2.Get(1,1)

CrossProduct = [ x; y; z ]



DotProduct

let DotProduct (Vector) //output
set DotProduct = v1.Get(1,1)*v2.Get(1,1) + v1.Get(2,1)*v2.Get(2,1) + v1.Get(3,1)*v2.Get(3,1)



Magnitude

let Magnitude (Real) //output
set Magnitude = sqrt(DotProduct)



Vector Normalize (already exist in EKL, but just in case)

let nv (Vector) //output

let Len (Real)
Len = abs( sqrt(Magnitude) )

let x, y, z(Real)
x = v1.Get(1,1)/Len
y = v1.Get(2,1)/Len
z = v1.Get(3,1)/Len

nv = [ x; y; z ]



By using these function, an angle between vectors is easily found. (There will be another post for calculating rotation angle)

Vector Angle

let vAngle (ANGLE) //output

//step 1: create unit vectors of v1 and v2
let nv1, nv2 (Vector)

//step 2: dotproduct of nv1 and nv2
let dot (Real)

vAngle = acos(dot)



Rotating vector is also easily created by following steps.

Vector Rotate

//step 1: create unit vector of original vector and rotation axis vector
let nv, nAxis (Vector)

//step 2: extract x, y, and z attributes of Axis unit vector
let x, y, z (Real)
x = nAxis.Get(1,1)
y = nAxis.Get(2,1)
z = nAxis.Get(3,1)

//step 3: get real number of rotation angle(convert ANGLE to String to Real)
let rAngle (Real)

//step 5: find cosin, sin, and tangent
let c, s, t (Real)

//step 6: get transformed(ratated) value of x, y, and z attributes
let vx, vy, vz (Real)
vx = nv.Get(1,1)*(t*x*x + c) + nv.Get(2,1)*(t*x*y - s*z) + nv.Get(3,1)*(t*x*z + s*y)
vy = nv.Get(1,1)*(t*x*y + s*z) + nv.Get(2,1)*(t*y*y + c) + nv.Get(3,1)*(t*y*z - s*x)
vz = nv.Get(1,1)*(t*x*z - s*y) + nv.Get(2,1)*(t*y*z + s*x) + nv.Get(3,1)*(t*z*z + c)

let vReturn (Vector) //output
vReturn = [vx; vy; vz]



### 3D BI-ARC Example

Here is an example of creation by using vectors via Knowledge Patterns and xGen's script node.

#### Knowledge Patterns

BI-ARC using Knowledge Pattern with custom control.

#### xGenerative Design Script Node

BI-ARC using Script Editor in xGen with custom control.

### Source File

Here is the file used for the above example

Tags: , , ,