CalcBot

Searchβ¦

Vector2

Function reference for c-vector2 calculate

Heads up!

This is the documentation for the

`c-vector2`

command. If you mean to read the information on `c-vector3`

, go here.The term **vector** in this document will describe a **2D vector**.

2D vectors

A 2D vector is a representation of a point in two-dimensional space. You can express vectors in CalcBot with 2 or 4 components, which collectively express the magnitude and direction of the vector.

General

c-v2 c <expression>

Shorthand syntax for

`c-vector2 calculate`

.1

> c-v2 c i + j

2

(1, 1)

Copied!

c-v2 m [r | d]

Shorthand syntax for switching trigonometric modes.

`c-vector2`

and `c-vector3`

both share the same trigonometric mode.1

> c-v2 m r

2

Set vector mode to radians

3

β

4

> c-v2 m d

5

Set vector mode to degrees

Copied!

Operators

All operators from

`c-calculate`

are available for use in `c-vector2 calculate`

:not n

Negates **truthy** value, false (0) is returned. Otherwise, true (1) is returned.

`n`

. If `n`

is a 1

> c-v2 c not true

2

0

3

β

4

> c-v2 c not false is true

5

1

Copied!

n!

Take the factorial of

`n`

. If `n`

is a vector, this operation will throw an error.1

> c-v2 c 6!

2

720

3

β

4

> c-v2 c (1, 2)!

5

Cannot take factorials of vectors.

Copied!

a ^ b

Raise **not** return a complex number in any situation, unlike in

`a`

to the power of `b`

. If `b`

is a vector, this operation will immediately throw an error. If `a`

is a vector, but `b`

is not an integer, this operation will also immediately throw an error. This operation will `c-calculate`

.1

> c-c 2 ^ 3

2

8

3

β

4

> c-v2 c (3i + j) ^ 3

5

(30, 10)

6

β

7

> c-v2 c -4 ^ (1/2)

8

NaN

9

β

10

> c-v2 c (3i + j) ^ (1/2)

11

Vectors cannot be raised to decimal powers.

12

β

13

> c-v2 c 2 ^ j

14

Vectors cannot be on the right side of the '^' operator.

Copied!

a * b

Multiply

`a`

and `b`

. When operating on two vectors, this operator returns the dot product of the two vectors.1

> c-v2 c 2 * 4

2

8

3

β

4

> c-v2 c (1, 2) * (5, 4, 1, 2)

5

-8

Copied!

a / b

Divide

`a`

by `b`

. If `b`

is a vector, this operation will immediately throw an error.1

> c-v2 c 15 / 5

2

3

3

β

4

> c-v2 c (1, 2) / 2

5

(0.5, 1)

6

β

7

> c-v2 c (1, 2) / (5, 4, 1, 2)

8

Vectors cannot be on the right side of the '/' operator.

Copied!

a % b

Divide

`a`

by `b`

and return the remainder of the result. This is also known as modulus division, or remainder division. If either `a`

or `b`

is a vector, this operation will immediately throw an error.1

> c-v2 c 8 % 2

2

0

3

β

4

> c-v2 c (1, 2) % 2

5

Vectors cannot be used with the '%' operator.

Copied!

a + b

Add

`a`

and `b`

.1

> c-v2 c 1 + 1

2

2

3

β

4

> c-v2 c (3, 4) + 2

5

(3, 4) + 2

6

β

7

> c-v2 c (3, 4) + (1, 2)

8

(4, 6)

Copied!

a - b

Subtract

`b`

from `a`

.1

> c-v2 c 1 - 1

2

0

3

β

4

> c-v2 c (3, 4) - 2

5

(3, 4) - 2

6

β

7

> c-v2 c (3, 4) - (1, 2)

8

(2, 2)

Copied!

a is b

Returns true (1) if

`a`

is equal to `b`

.1

> c-v2 c 3 is 1 + 2

2

1

3

β

4

> c-v2 c (1, 1) is (2 - 1, 1)

5

1

Copied!

a nis b

Returns true (1) if **not** equal to

`a`

is `b`

.1

> c-v2 c 3 nis 1 + 2

2

0

3

β

4

> c-v2 c (3, 1) nis (2, 1)

5

1

Copied!

a ais b

Returns true (1) if **approximately** equal to

`a`

is `b`

. The difference between them must be less than `1 * 10 ^ -6`

. For vectors, this operator will compare the x and y components separately.This operator is intended to be used when comparing the results of certain mathematical operations that produce slightly imprecise results (like prime notation).

1

> c-v2 c 3.0000002 ais 3

2

1

3

β

4

> c-v2 c (3, 2) ais (2.9999999, 2)

5

1

Copied!

a anis b

Negates the behavior of the

`ais`

operator.1

> c-v2 c 3 anis 3

2

0

3

β

4

> c-v2 c (5, 2) anis (1, 0)

5

1

Copied!

a > b

Returns true (1) if

`a`

is greater than `b`

.1

> c-v2 c 3 > 2

2

1

Copied!

a < b

Returns true (1) if

`a`

is less than `b`

.1

> c-v2 c 3 < 2

2

0

Copied!

a >= b

Returns true (1) if

`a`

is greater than or equal to `b`

.1

> c-v2 c 3 >= 2

2

1

3

β

4

> c-v2 c 4 >= 4

5

1

Copied!

a <= b

Returns true (1) if

`a`

is less than or equal to `b`

.1

> c-v2 c 3 <= 2

2

0

3

β

4

> c-v2 c 4 <= 4

5

1

Copied!

a and b

Returns true if **both** **truthy** values.

`a`

and `b`

are 1

> c-v2 c 3 and 4

2

1

3

β

4

> c-v2 c 3 and 0

5

0

Copied!

a or b

Returns true if **either** **truthy** values.

`a`

or `b`

are 1

> c-v2 c 3 or 4

2

1

3

β

4

> c-v2 c 3 or 0

5

1

6

β

7

> c-v2 c 0 or 0

8

0

Copied!

Vector literals

(a, b)

Syntax for a two-dimensional two-component vector.

1

> c-v2 c (1, 2)

2

(1, 2)

Copied!

(a, b, c, d)

Syntax for a two-dimensional four-component vector. Vectors of this kind are implicitly converted to their component form when used with other operations during evaluation.

1

> c-v2 c (1, 2, 5, 3)

2

(1, 2, 5, 3)

Copied!

Vector constants

These constants will always be available everywhere in all of

`c-vector2`

's children commands.`i`

`j`

`zero`

All

`c-calculate`

functions as described here are available to use in `c-vector calculate`

, and most of them will behave the same. Some functions have been modified for use with `c-vector calculate`

, however:Trigonometric functions

All trigonometric functions will **not** operate with vector arguments.

sqrt(n)

This function will **not** return a complex number in any situation, unlike in

`c-calculate`

.1

> c-v2 c sqrt(4)

2

2

3

β

4

> c-v2 c sqrt(-4)

5

NaN

Copied!

abs(n)

This function will behave as expected. If

`n`

is a vector, this function will return the vector with the absolute value of each of its components (equivalent to `(abs(x(n)), abs(y(n)))`

).1

> c-v2 c abs(-4)

2

4

3

β

4

> c-v2 c abs((-2, -pi))

5

(2, 3.141592653589793)

Copied!

pow(n, p)

If **not** return a complex number in any situation, unlike in

`p`

is a vector, this function will immediately throw an error. If `n`

is a vector, but `p`

is not an integer, this function will also immediately throw an error. Otherwise, this function behaves as expected, except that it will `c-calculate`

. This function is implicitly called when using the alternative syntax: `n ^ p`

.1

> c-v2 c pow(2, 4)

2

16

3

β

4

> c-v2 c pow(3i + j, 3)

5

(30, 10)

6

β

7

> c-v2 c pow(3i + j, 1/2)

8

Vectors cannot be raised to decimal powers.

9

β

10

> c-v2 c pow(2, j)

11

Vectors cannot be on the right side of the '^' operator.

Copied!

Functions that return a number

Most of the functions below available in

`c-vector calculate`

are also exposed as children commands of `c-vector`

.x(v)

Returns the

`x`

component of vector `v`

.1

> c-v2 c x(2i + j)

2

2

Copied!

y(v)

Returns the

`y`

component of vector `v`

.1

> c-v2 c y(2i + j)

2

1

Copied!

comp(v)

Returns the component of vector

`v`

.1

> c-v2 c comp((2, 2, 3, 3))

2

(1, 1)

Copied!

quad(v)

Returns the quadrant that vector

`v`

's component's head lies in. If the head is on the x-axis, 5 is returned. If the head is on the y-axis, 6 is returned.1

> c-v2 c quad(2i + j)

2

1

3

β

4

> c-v2 c quad(j)

5

6

Copied!

dir(v)

Returns the direction angle of vector

`v`

, where the unit vector `i`

(1, 0) is 0 degrees. For example, unit vector `j`

(0, 1)'s direction angle is 90 degrees.1

> c-v2 c dir(2i + j)

2

26.56505117707799

Copied!

mag(v)

Returns the magnitude of vector

`v`

.1

> c-v2 c mag(2i + j)

2

2.23606797749979

Copied!

sqrmag(v)

Returns the squared magnitude of vector

`v`

. If you need to compare the magnitudes of two vectors, it will usually be more efficient to compare their squared magnitudes.1

> c-v2 c sqrmag(2i + j)

2

5

Copied!

angle(v1, v2)

Returns the smallest angle between vectors

`v1`

and `v2`

. The returned value will always be between 0 and 180 degrees.1

> c-v2 c angle(2i + j, -4i + 6j)

2

97.12501634890181

Copied!

dot(v1, v2)

Returns the dot product of vectors

`v1`

and `v2`

.1

> c-v2 c dot(2i + j, -4i + 6j)

2

-2

Copied!

dist(v1, v2)

Returns the distance between the heads of vectors

`v1`

and `v2`

.1

> c-v2 c dist(2i + j, -4i + 6j)

2

7.810249675906654

Copied!

Functions that return a vector

polar(m, a)

Returns a vector given its magnitude (

`m`

) and direction angle (`a`

). This is equivalent to a vector defined as `(m * cos(a), m * sin(a))`

.1

> c-v2 c polar(2, 90)

2

(0, 2)

Copied!

unit(v)

Returns the unit vector of vector

`v`

, that is, the vector with the same direction angle as `v`

but with a magnitude of 1.1

> c-v2 c unit(2i + j)

2

(0.8944271909999159, 0.4472135954999579)

Copied!

lerp(v1, v2, t)

Returns a new vector linearly interpolated from vector

`v1`

to `v2`

by a constant `t`

. For example, `lerp(2i+j, -4i+6j, 0.5)`

returns the midpoint between vectors `2i+j`

and `-4i+6j`

.1

> c-v2 c lerp(2i + j, -4i + 6j, 0.5)

2

(-1, 3.5)

Copied!

mid(v1, v2)

Returns the midpoint between vectors

`v1`

and `v2`

. This will always perform more quickly than `lerp(v1, v2, 0.5)`

.1

> c-v2 c mid(2i + j, -4i + 6j)

2

(-1, 3.5)

Copied!

Last modified 4d ago

Copy link

Contents

Heads up!

2D vectors

General

c-v2 c <expression>

c-v2 m [r | d]

Operators

not n

n!

a ^ b

a * b

a / b

a % b

a + b

a - b

a is b

a nis b

a ais b

a anis b

a > b

a < b

a >= b

a <= b

a and b

a or b

Vector literals

(a, b)

(a, b, c, d)

Vector constants

Modified Calculate functions

Trigonometric functions

sqrt(n)

abs(n)

pow(n, p)

Functions that return a number

x(v)

y(v)

comp(v)

quad(v)

dir(v)

mag(v)

sqrmag(v)

angle(v1, v2)

dot(v1, v2)

dist(v1, v2)

Functions that return a vector

polar(m, a)

unit(v)

lerp(v1, v2, t)

mid(v1, v2)