*This article is about the vector used in algebra. For the type of graphics, see Vector Graphics.*

**For more information, see Euclidean vector on Wikipedia.**
**Vectors** are geometric objects with a direction and a magnitude. They are used to represent various physical quantities such as forces and object displacement. This article will explain how to use vectors within Scratch.

## Definition of Vector

Vectors are essentially lists, with each "item" (components) being a number. Only 2-dimensional vectors (which have 2 components) will be considered, however, vectors can be in any number of dimensions. For example, raytracing requires 3-dimensional vectors for calculations. They can be graphically represented with an arrow from one point to another.

Vectors are used in physics and math to represent many things, such as velocity, force, displacement, etc. They can be used to describe an object's motion in order to calculate things such as collisions.

## Notation

The following notation will be used in this article:

- is a vector, with vectors being denoted in bold.
- is the vector with components and .
- and are the first and second components of respectively.
- is a dot product.
- is a cross product.
- is the unit vector .
- is the length of .

## Vector Operations

A lot of the standard operations of natural numbers can be performed on vectors. Vector addition consists of simply adding the respective components of two vectors: . This can be represented geometrically by placing a vector at the tip of the other's.

Subtraction can be derived from addition, and only requires subtracting the vectors' respective components:

Vector multiplication (known as scalar multiplication) will be defined only between a scalar (a real number) and a vector, where each of the components is multiplied by the scalar. The same holds for division, with each of the components being divided by the scalar: *
*

A vector can be divided by a scalar, but a scalar cannot be divided by a vector:

The magnitude of a vector is its length, and can be calculated using the Pythagorean theorem:

The direction of an n-dimensional vector is defined by (n-1) angles; for a 2D vector, only one angle is necessary to calculate its direction, which can be calculated using right-angle trigonometry: (where is the angle)

One common quantity we need is the *unit vector*, which is a vector in the same direction as another, but of unit length. A vector's corresponding *unit vector* can be calculated by dividing the vector by its length: *.
*

The dot product and cross product are the two main methods of multiplying two vectors together:

- The dot product of two vectors produces a scalar, which can be calculated with two different equations:
*.*- , with being the angle between the two vectors.

- The cross product:
*of two vectors is only defined in 3-dimensional space*^{[note 1]}and produces a vector. The cross product is*non-commutative*, meaning that .

Let *a × b = c*.

- The Three-Dimensional cross-product is defined by these two equations:
- , where is the angle between the two vectors and is the unit vector perpendicular two the vectors and .
- The individual components of are defined by these equations:

Note:

- ↑ There exists a seven-dimensional cross product, although it is rarely used in Scratch projects.

## Scratch Representation

To represent a vector in Scratch, we use a pair of variables, normally called <name>.x and <name>.y. We can then perform addition, subtraction, multiplication, and division on each of those variables independently.

Create a pair of variables position.x and position.y, and write a script to make a sprite continually go to the coordinates given by the position vector. Now, if you set those variable watchers to sliders, you can change the position with sliders.

when gf clicked forever set x to (position.x) set y to (position.y)

For something more interesting, create another variable pair called "velocity" (i.e. create velocity.x and velocity.y). Add a new script which changes the respective components of the position vector by the components of the velocity vector. Now, by changing the value of velocity, you should have a much smoother motion.

when gf clicked forever change [position.x v] by (velocity.x) change [position.y v] by (velocity.y) set x to (position.x) set y to (position.y)

Finally, we can create an effect of gravity by also changing the velocity by some vector:

when gf clicked set [position.x v] to (0) set [position.y v] to (0) set [velocity.x v] to (10) set [velocity.y v] to (10) forever change [velocity.y v] by (-1) change [position.x v] by (velocity.x) change [position.y v] by (velocity.y) set x to (position.x) set y to (position.y)

## A common use: bouncing

A common use of vectors is to simulate a body bouncing off an arbitrarily angled surface. To make a bouncing script, we need to calculate the perpendicular vector to the surface, then project the velocity vector of the body on that to find the component of the vector that is reflected.

To find the perpendicular, we switch the X and Y components, and negate any one. Projection is a bit tougher, and requires the **dot product**. The dot product (a • b).