# 3D Vector OpenSCAD Library

## by thingiverse, published

## Thing Info

# Summary

This is a library for manipulating and visualizing 3D vectors in OpenSCAD. It has basic functions like crossProduct_3DVector() and magnatude_3DVector() as well as some more complex functions to test vector angles and rotate one vector towards another vector.

Check out the instructions tab to see more detail.

# Instructions

To use it in your own project, download the .scad and put it in your openscad library folder or in the same folder as the project you are using it in. Then at the top of your script just type :

`use <3dvector.scad>`

We've also included this in Customizer! Include it in your customizers by typing:

`use <utils/3dvector.scad>`

Here'Âs a full list of the functions and modules included:

`function magnatude_3DVector(v)`

give this function a 3D vector in [x,y,z] format and it will return the length of that vector

`function angleBetween_3DVector(u, v)`

give this function two 3D vectors in [x,y,z] format and it will return the angle, in degrees, between them

`function normalize_3DVector(v)`

give this function a 3D vector in [x,y,z] format and it will return a vector that points in the same direction but with a length of 1

`function dotProduct_3DVector(u, v)`

give this function two 3D vectors in [x,y,z] format and it will return the dot product of those vectors

`function crossProduct_3DVector(u, v)`

give this function two 3D vectors in [x,y,z] format and it will return the cross product of those vectors

`function rotMatrix_3DVector(axis, angle)`

give this function a 3D vectors in [x,y,z] format for axis and an angle in degrees and it will return a rotation matrix for the axis-angle rotation

`function rotAxisAngle_3DVector(vec, axis, angle)`

give this function an initial 3D vector and a 3D vector for the axis in [x,y,z] format, and an angle in degrees and it will return the rotated vector

`function trackTo_3DVector(cur, tar, influence)`

give this function an initial 3D vector and a target 3D vector in [x,y,z] format, then give it an influence between 0 and 1 and it will return an interpolated vector between the two vectors according to the influence

`function random_3DVector(seed = undef)`

calling this function without any parameters will return a random 3D vector. pass it a seed value to get the same "random" vector each time

```
module drawArrow_3DVector
(
vector = [10, 10, 10],
root = [0, 0, 0],
color_from_magnatude = true,
magnatude_range = 100,
radius_from_magnatude = true,
manual_radius = 1,
res = 16
)
```

this module will draw a colored arrow to represent a 3D Vector.

root controls where the vector starts from.

magnatude_range defines the vector length that will result in a purple arrow. Vectors approaching 0 length will be red.

radius_from_magnatude controls if the arrow gets fatter when it is longer.

manual_radius controls the radius if radius_from_magnatude is set to false.

res is the number of facets around the arrow.

```
module drawArrowChain_3DVector
(
my_vectors = [[10,10,10],[-10,10,10]],
root = [0, 0, 0],
iterations_left
)
```

this module will recursively draw a chain of colored arrows end to end to represent a list of 3D Vectors.

root controls where the vector chain starts from.

iterations_left controls how many more recursions will occur, this parameter will self populate and only needs to be exposed because of how recursion works in OpenSCAD. Don'Ât set this unless you have a good reason to.

## Thing Info

## Liked By

View All## Tags

## License

## Give a Shout Out

# All Apps

No results.

the following code does not seem to line up:

```
offset_length = 0.71;
offset_height = 0.43;
offset_angle = angleBetween_3DVector([0, 0, 0], [offset_length, 0, offset_height]);
cube(size = [1, 1, 1], center = false);
translate([offset_length, 0, offset_height]) {
cube(size = [1, 1, 1], center = false);
}
translate([0, 0.125, 1])
{
rotate(a = offset_angle, v = [0, 1, 0])
{
color("red")
{
cube(size = [1, 1, 1], center = false);
}
}
}
```

btw I needed to update angleBetween_3DVector() since it was throwing an error when starting with [0, 0, 0] so I added some infinitesimal value to both vectors thus lowering the chances of a divide-by-zero error somewhere.

` function angleBetween_3DVector(u, v) = acos(((u+[0.000001, 0.000001, 0.000001]) * (v+[0.000001, 0.000001, 0.000001])) / (magnatude_3DVector(u+[0.000001, 0.000001, 0.000001]) * magnatude_3DVector(v+[0.000001, 0.000001, 0.000001])));`

the following lines up correctly though:

` offset_angle = atan2(offset_length, offset_height)`

in module drawArrow_3DVector there is no option to set the color and color_from_magnitude variable is unused. suggest you change the declaration and color line to the following:

module drawArrow_3DVector

(

vector = [10, 10, 10],

root = [0, 0, 0],

color_from_magnatude = true,

color="black",

magnatude_range = 100,

radius_from_magnatude = true,

manual_radius = 1,

res = 16

)

`color((color_from_magnatude?(hsvToRGB(magnatude / magnatude_range * 360,1,1,1)):color))`

also it should be spelled magnitude and not magnatude.

Please Login to Comment