# 3D Coordinates and Vectors

### <mark style="color:blue;">1. Understanding 3D Coordinates</mark>

In 3D space, each point **P(x, y, z)** is represented by three values where:

* `x` is the position on the X-axis (left-right)
* `y` is the position on the Y-axis (front-back)
* `z` is the position on the Z-axis (up-down)

#### Example:

A point A(2, 3, 4) means:

* `2` units along X-axis
* `3` units along Y-axis
* `4` units along Z-axis

### <mark style="color:blue;">2. What is a Vector in 3D?</mark>

A vector in 3D space has:

* **Direction**
* **Magnitude (length)**

A 3D vector is written as:

$$
\overrightarrow{v}=\left( a, b, c \right)
$$

Where `a`, `b`, and `c` are the components along x, y, and z.

### <mark style="color:blue;">3. Vector from Two Points</mark>

To find a vector from point A to point B:

$$
\overrightarrow{AB}=\left( x\_{b}-x\_{a} ; , y\_{b}-y\_{a} ; , z\_{b}-z\_{a} \right)
$$

#### Example:

Let A = (1, 2, 3) and B = (4, 6, 9). Then

$$
\overrightarrow{AB} =\left( 4−1,6−2,9−3 \right)=\left( 3,4,6 \right)
$$

#### Python Code:

```python
import numpy as np

A = np.array([1, 2, 3])
B = np.array([4, 6, 9])
vector_AB = B - A
print("Vector AB:", vector_AB)
```

#### Output:

```python
Vector AB: [3 4 6]
```

### <mark style="color:blue;">2. Vector Magnitude (Length)</mark>

$$
\left| \overrightarrow{v} \right|=\sqrt{x^{2}+y^{2}+z^{2}}
$$

#### Example:

$$
\overrightarrow{v}=(3,4,6)\Rightarrow \left| \overrightarrow{v} \right|=\sqrt{3^{2}+4^{2}+6^{2}}=\sqrt{61}
$$

#### Python Code:

```python
magnitude = np.linalg.norm(vector_AB)
print("Magnitude of Vector AB:", magnitude)
```

### <mark style="color:blue;">5. Unit Vector</mark>

A unit vector has magnitude 1 and points in the same direction:

$$
\hat{v}=\frac{\overrightarrow{v}}{\left| \overrightarrow{v} \right|}
$$

#### Python Code:

```python
unit_vector = vector_AB / magnitude
print("Unit vector in direction of AB:", unit_vector)
```

#### Output:

```python
Unit vector in direction of AB: [0.38411064 0.51214752 0.76822128]
```

### <mark style="color:blue;">6. Dot Product of Two Vectors</mark>

$$
\overrightarrow{A}.\overrightarrow{B}=x\_{a}x\_{b}+y\_{a}y\_{b}+z\_{a}z\_{b}
$$

Used to find the <mark style="color:red;">**angle**</mark> between vectors:

$$
cos\theta=\frac{\overrightarrow{A}.\overrightarrow{B}}{|\overrightarrow{A}||\overrightarrow{B}|}
$$

#### Python Code:

```python
import numpy as np

A = np.array([1, 2, 3])
B = np.array([4, 5, 6])
dot_product = np.dot(A, B)
angle = np.arccos(dot_product / (np.linalg.norm(a) * np.linalg.norm(b)))
print("Dot product:", dot_product)
print("Angle (radians):", angle)
```

#### Output:

```python
Dot product: 32
Angle (radians): 0.2257261285527342
```

### <mark style="color:blue;">7. Cross Product</mark>

$$
\overrightarrow{A}\times \overrightarrow{B}=\begin{vmatrix}
i & j & k \\
x\_{a} & y\_{a} & z\_{a} \\
x\_{b} & y\_{b} & z\_{b}
\end{vmatrix}
$$

#### Python Code:

```python
import numpy as np

A = np.array([1, 2, 3])
B = np.array([4, 5, 6])

cross_product = np.cross(A, B)
print("Cross product (perpendicular vector):", cross_product)
```

#### Output:

```python
Cross product (perpendicular vector): [-3  6 -3]
```

### <mark style="color:blue;">8. Distance Between Two Points in 3D</mark>

$$
D=\sqrt{(x\_{2}-x\_{1})^{2}+(y\_{2}-y\_{1})^{2}+(z\_{2}-z\_{1})^{2}}
$$

#### Python Code:

```python
import numpy as np

A = np.array([1, 2, 3])
B = np.array([4, 5, 6])

distance = np.linalg.norm(B - A)
print("Distance between A and B:", distance)
```

#### Output:

```python
Distance between A and B: 5.196152422706632
```

### <mark style="color:blue;">9. Visualizing 3D Vectors</mark>

```python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# origin
ax.quiver(0, 0, 0, a[0], a[1], a[2], color='blue', label='a')
ax.quiver(0, 0, 0, b[0], b[1], b[2], color='green', label='b')

ax.set_xlim([0, 5])
ax.set_ylim([0, 5])
ax.set_zlim([0, 5])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()
plt.show()
```

#### Output:

<figure><img src="https://3561236956-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5YU9oQgnizEAfDKK4w2y%2Fuploads%2FJqEmjJ0nDek3sb25LQsw%2F00017.png?alt=media&#x26;token=c82feaf8-5408-4f46-868a-0299a1d259a0" alt=""><figcaption></figcaption></figure>

### <mark style="color:blue;">Keywords</mark>

`3D coordinates`, `vectors`, `vector magnitude`, `unit vector`, `dot product`, `cross product`, `vector projection`, `distance in 3D`, `3D space`, `vector operations`, `python vectors`, `numpy vectors`, `angle between vectors`, `3D visualization`, `vector direction`, `vector length`, `3D math`, `linear algebra`, `vector geometry`, `vector representation`, `nerd cafe`
