Utilities
Diagramatics provides some utility functions that can be used to create diagrams.
// creating a vector object
let va = new Vector2(1,2);
// you can also use the shorthand `V2`
let vb = V2(1,2);
// you can create a unit vector with a given angle using `Vdir`
let vc = Vdir(Math.PI/2); // the same as `V2(0,1)`
let va = V2(1,2);
let vb = V2(3,4);
let vc, x;
// addition
vc = va.add(vb); // V2(4,6);
// subtraction
vc = va.sub(vb); // V2(-2,-2);
// scaling
vc = va.scale(2); // V2(2,4);
// multiplication
vc = va.mul(vb); // V2(3,8);
// rotation from the origin
vc = va.rotate(Math.PI/2); // V2(-2,1);
// dot product
x = va.dot(vb); // 1*3 + 2*4 = 11;
// cross product
// * the cross product in 2D is a scalar
x = va.cross(vb); // 1*4 - 2*3 = -2;
// length
x = va.length(); // sqrt(1*1 + 2*2) = sqrt(5);
// length squared
x = va.length_sq(); // 1*1 + 2*2 = 5;
// angle from the positive x-axis
x = va.angle(); // atan2(2,1) = 1.1071487177940904;
// normalization
vc = va.normalize(); // V2(1/sqrt(5), 2/sqrt(5));
// checking for equality
x = va.equals(vb); // false;
// apply a function
let f = (v) => v.add(V2(1,1));
vc = va.apply(f); // V2(2,3);
// the same as `vc = f(va)`
to_radian (
angle
:
number )
// helper function to convert from degrees to radians
let angle = to_radian(30); // 0.5235987755982988
let ang2 = to_radian(90); // 1.5707963267948966 = Math.PI/2
to_degree (
angle
:
number )
// helper function to convert from radians to degrees
let angle = to_degree(Math.PI/2); // 90
let ang2 = to_degree(Math.PI/6); // 30
array_repeat (
arr
:
T[] , len
:
number )
:
T[]
// repeat an array until the length is `len`
let arr = [1,2,3];
let new_arr = array_repeat(arr, 5); // [1,2,3,1,2]
linspace (
start
:
number , end
:
number , n
:
number = 100 )
:
number[]
// create an equivalently spaced array of numbers from start to end (inclusive)
// [start, end]
let arr1 = linspace(0, 1, 5); // [0, 0.25, 0.5, 0.75, 1]
let arr2 = linspace(0, 1, 4); // [0, 0.333, 0.666, 1]
linspace_exc (
start
:
number , end
:
number , n
:
number = 100 )
:
number[]
// create an equivalently spaced array of numbers from start to end (exclusice)
// [start, end)
let arr1 = linspace_exc(0, 1, 5); // [ 0, 0.2, 0.4, 0.6, 0.8 ]
let arr2 = linspace_exc(0, 1, 4); // [ 0, 0.25, 0.5, 0.75 ]
range (
start
:
number , end
:
number , step
:
number = 1 )
:
number[]
// create an equivalently spaced array of numbers from start to end (exclusive)
// [start, end)
let arr1 = range(0, 5); // [0, 1, 2, 3, 4]
let arr2 = range(0, 5, 1); // [0, 1, 2, 3, 4]
let arr3 = range(0, 5, 2); // [0, 2, 4]
let arr4 = range(0, 5, 0.5); // [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]
range_inc (
start
:
number , end
:
number , step
:
number = 1 )
:
number[]
// create an equivalently spaced array of numbers from start to end (inclusive)
// [start, end]
let arr1 = range_inc(0, 5); // [0, 1, 2, 3, 4, 5]
let arr2 = range_inc(0, 5, 1); // [0, 1, 2, 3, 4, 5]
let arr3 = range_inc(0, 5, 2); // [0, 2, 4]
let arr4 = range_inc(0, 5, 0.5); // [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]