# About Math in Vectorscript

Jump to: navigation, search This vcor page needs language cleanup. If you wish to help, clean up the page, then remove me deleting the line "{{Language cleanup}}". In the category:Language cleanup you'll find a list of all pages marked for cleanup.

Very basic introduction about Math in Vectorscript, for the VS beginner. Unorthodox article by Orso B. Schmid

Of all things in Vectorscript the one that gives me more troubles is math. I simply don't understand anything of it, and if struggling and sweating I get to understand a tiny bit of it, I forget instantly. Need to start always from scratch again and again. So, supposing there are others in the same position, I think it won't be bad to collect some notes. For us, the math-impaired ones or those approaching scripting from far off disciplines.

Should the math-skilled stumble upon this article, please: a) have mercy upon us, b) feel free to add some real explanations of the things I can't explain.

## Vectors

Let's face the worst immediately: vectors. I don't understand vectors in the least, but I get them to work in my scripts visualizing them as follows: triangles in a 3D space. The main jump in abstract thinking was to stop thinking them as simple points.

Triangles are good, there is a lot of things you can do with them using really basic trig, so basic that even me can master it. Here are some images that should bring you to see vectors as triangles. This might help you as it helps me.

Vectors can be visualized by a triplet of coordinates {x, y, z}, defining a triangle whose start is always the user origin and whose base lies on the x-axis. If you have a vector "v" whose v.z=0 then your vector can be represented as a triangle in a 2D [x, y] coordinate system. But if your vector has z<>0 then your vector can be seen as triangle in the 3D space where its diagonal starts from the user origin and ends at the given point in 3D space. The base of the triangle is in both cases at the bottom of the x-axis. See screenshots below. Please observe that a vector whose z<>0 can also be seen as splitting a rectangular extrude (in orange in screenshot 2) by the diagonal.

1- with z=0: 2- with z<>0:  A vector is far more comfortable to use than a 3D point, because it has some properties which allow you to manipulate the data much faster. The simplest property is that it's defined by a distance and an angle. The angle is always measured between the x-axis and the diagonal of the triangle. Math people call the length or distance of the vector "magnitude".

• Norm(v): Measures the distance between user origin and the pt {x, y, z} in the 3D space. Its value is obviously influenced by the z-value of the vector as well as its angle. The higher the z-value by the same angle, the longest the vector length will be. The shortest distance is when at v.z = 0, whereupon the triangle is flat on a 2D plane.
```{ distance in 3D from the origin to the pt }
lenOfVector := Norm(v);```
• Vec2Ang(v): Returns the angle formed by the x-axis and the diagonal of the triangle.
• if z=0 then the angle is exactly like the angle of a line in a 2D space ("a" in screenshot 1).
• if z<>0 then the angle is that formed by the slanted blue triangle above. It's not a very comfortable triangle to work with. ("A" in screenshot 2).
```{ create a new vector "w": rotating a vector "v" by 45 degrees around the user origin }
w := Ang2Vec(Vec2Ang(v) +45, Norm(v));
{ remember that if v.z <> zero, the angle is slanted and shaping a vector in the fashion above described won't output the expected result. Try }```
• Ang2Vec(ang, length):Plug an angle, plug a distance and you have a well formed vector starting from the user origin, this vector will always have v.z = 0.
```{ define a new vector }
v := Ang2Vec(ang, leng); { The z is here always zero }
v := Ang2Vec(45, 5m);```

### Addition and subtraction

Vectors can be summed in a visual fashion grabbing them by their tail and putting them on the head of the previous. Analogically they can be subtracted putting them by the head on the head of the previous. This is easily comprehensible in 2D, but I have difficulties figuring the slanted triangles in 3D, so I rather prefer imagining them as rectangular extrudes. Piling up the extrudes will give you the needed resulting vector. Is easy then to recognize the resulting 3D bounding box: the farmost corner from the user origin is always the searched pt. Addition and subtraction in 3D

Given two vectors "v1" and "v2", adding to form a vector v3 and subtracting them to a vector v4 means:

```v3 := v1 + v2;
{ alternatively }
v3.x := v1.x +v2.x;
v3.y := v1.y +v2.y;
v3.z := v1.z +v2.z;

v4 := v1 - v2;
{ alternatively }
v4.x := v1.x -v2.x;
v4.y := v1.y -v2.y;
v4.z := v1.z -v2.z;```

It is evident that the vector operation is faster.

### Multiplication and division

A vector can be multiplied for a scalar (roughly: a number). This operation corresponds to adding the same vector by the multiplying factor (the number). For example if a vector v is 2 units long (Norm(v) =2), then v * 3 will be three times longer: 6. Division works analogically: it corresponds to subtracting.

```w := v * 3;
w := v + v + v; { same as above }```

This known, you can use a vector whose length is 1 - a Unit Vector- for reshaping your vectors to other distances or angles.

• UnitVec(v): Given a vector v, returns the same vector but with a length =1 unit: Norm(UnitVec(v)) will invariably output 1, in whatever units you are:
```{ reshape a vector "v" to a new distance }
{ supposing that "v" has z <> 0 }
w := UnitVec(v) * (Norm(v) +1m); { prolong the vector "v" by one meter keeping its orientation in 3D }
w := -Norm(v) * UnitVec(v); { flip "v" vertically and horizontally: around the x-axis and around the y-axis }
w := Ang2Vec(Vec2Ang(UnitVec(v)) -90, Norm(v)); { rotate "v" by -90 degrees, look at the routine "Perp" }```

### Make relative

In the majority of cases you don't need vectors to be related to the user origin {0,0}, but to some other point. Once known how to sum and subtract you can make your vectors relative to another vector which then acts as common origin.

```PROCEDURE Example;
VAR
o, v, w :VECTOR;
BEGIN
{ WARNING: in VW 2011 GetPt3D won't load a z value different than zero if the active plane is screen. Be sure that the active plane is "Layer" }
GetPt3D(o.x, o.y, o.z, FALSE); { get the new origin pt for the calculation }
GetPt3D(v.x, v.y, v.z, FALSE); { get a pt }
GetPt3D(w.x, w.y, w.z, FALSE); { get another pt }

u := v+w -o; { sum of v+w relative to a common pt "o" }

Locus3D(o.x, o.y, o.z);
Locus3D(v.x, v.y, v.z);
Locus3D(w.x, w.y, w.z);
Locus3D(u.x, u.y, u.z);
END;
RUN(Example);```
1. I have Dyscalculia