# Coordinates and Vectors
This notebook will take you through the basics of the cartesian co-ordinates system, and the way we can use vectors to represent positions in space.

## Introduction: Examples of Coordinates and Changes
Coordinates you have covered before, these are pairs of numbers which represent the location of things on a grid. 

Below is an example of a variety of coordinates with objects placed at them, stolen shamelessly from a Bing search.


This part is familitar, but when it comes to calculating the steps that any one object would need to cover to get to any other, things can get long winded. 

There is a standardised formula. This is simply the co-ordinates subtracted, in the form of: Destination - Origin

A simple conceptual derivation is below

When you want to go from one location to another, there is a journey involved. This journey takes the same form as the two locations. Our journey represented mathematically would look like:
\begin{equation}
\begin{pmatrix}x_{origin}  & y_{origin} \end{pmatrix} + \vec{journey} =  \begin{pmatrix} x_{target}  & y_{target} \end{pmatrix}
\end{equation}

When we rearrange to find the journey then we'd have something which looked like:
\begin{equation}
\begin{pmatrix} x_{target}  & y_{target} \end{pmatrix} - \begin{pmatrix}x_{origin}  & y_{origin} \end{pmatrix}  =  \vec{journey} 
\end{equation}

The prescriptive equation then (using what we know about co-ordinates) is:
\begin{equation}
 \vec{journey}  =\begin{pmatrix} x_{target} - x_{origin} & y_{target} - y_{origin} \end{pmatrix}
\end{equation}

Path for worm to heart is 
\begin{equation}
 \begin{pmatrix} 3  & 1 \end{pmatrix} - \begin{pmatrix}5  & 2 \end{pmatrix}  =  \begin{pmatrix} -2  & -1 \end{pmatrix}
\end{equation}
Or in the other direction , heart to worm is


\begin{equation}
\begin{pmatrix}5  & 2 \end{pmatrix}  -  \begin{pmatrix} 3  & 1 \end{pmatrix} =  \begin{pmatrix} 2  & 1 \end{pmatrix}
\end{equation}



Path between the worm and the heart is (2, 1) or (-2,-1) depending on your direction. 




## Python Exposure: Numpy Arrays
These can be represented in python too, and made a lot simpler to work with for us. What we need is a structure which understands that it has two parts, and when adding or subtracting one from the other, it needs to add the two first parts together and the two second parts together, not just add the lot. For example


```python
(1, 2) + (1, 1)
```




    (1, 2, 1, 1)



Using () brackets, the + sign just stuck them together into one big list. the same will occur if you use square brackets []

Thankfully, someone has already though of us needing this, and created something which will do the heavy lifting for us, along with lots of other features that we don't need right now. This is gathered into a library called numpy (Number Python). The structure we are looking for is called an array.


```python
import numpy as np # abbreviated 'cause we're lazy 
worm = np.array([5, 2])
heart = np.array([3, 1]) 

print(worm - heart)
print(heart - worm)
```

    [2 1]
    [-2 -1]


Note: In python taking an object, like `np` and using the `.` operator, pulls out a particular part of it. Here we are extracting the definition for the array structure and using it to make one.

As you can see, when using two numpy arrays, python understands that its supposed to add the relevant parts together, not just sum the lot. 

### Exercise 1.1: Calculating Journeys
Now, Using numpy, calculate the following journeys:

1. Cricket -> Dog
1. Sun -> Strawberry
1. Cricket -> Sun

We've already imported numpy above, so you will be able to access the array generators with the np.array([x,y]) syntax as before


```python
## Put your Exercise attempt in here. 



### Below is an empty list, which you can use to store your answers,
### remember: if you dont use the output directly you must wrap the numbers in np.array
answers = [ , , ]
## This should check your results
sum(answers) == np.array([9,-5])
```

### Excercise 1.2: Multi Stage Journeys
There is a useful feature of complex journeys with co-ordinates. Which will hopefully become clear when doing the 2 tasks below. 

1. Calculate the overall movement to make the following journey:: <font color=red> sun -> worm -> cricket -> strawberry </font>

1. And the movement to take the simpler journey ::::::::::::::::::::::::: <font color=blue> sun -> strawberry </font>




## Extension: 3 Dimensional Co-ordinates. 

We are familiar with using 2 dimensional co-ordinates to describe flat surfaces. This works because they have two directions they can travel in. Left-Right and Up-Down. In reality though, there is another dimension, In-Out of the page. Since it's very difficult to show a 3D image on a 2D surface, we have to make do with some side-on views to try to make sense of it all. 



### Exercise 2.1: Reading 3d Co-ordinates
Using the diagram above, define the position of the 3rd point (R). First two are included for reference.


```python
## Generate the Coordinates for R
pos_P = np.array([-3,0,0]) # Note the array is 3 elements long now
pos_Q = np.array([6,-2,0])

## Define Position for R here
```

### Exercise 2.2: 3-d Journeys
We can define the journey between two points in 3 dimensions the same as before. 

What is the journey from point P to point Q below


```python
## Define your calculation here
sum(pos_P - pos_Q)

## This should check the answer
sum(answer) == -7
```




    -7




```python

```

ad6a1606bc3d24fd3d17eabece56d075d84b9c76
ipynb
Jupyter Notebook
tutorials/01-Coordinates_and_Vectors.ipynb
JDOsborne1/python-orrery-lesson
edc3f538fcb33eb57dddf1eb87e9106db18eb028
MIT
tutorials/01-Coordinates_and_Vectors.ipynb
JDOsborne1/python-orrery-lesson
edc3f538fcb33eb57dddf1eb87e9106db18eb028
MIT
2020-04-30T17:47:41.000Z
2020-04-30T19:11:31.000Z
tutorials/01-Coordinates_and_Vectors.ipynb
JDOsborne1/python-orrery-lesson
edc3f538fcb33eb57dddf1eb87e9106db18eb028
MIT
Qwen/Qwen-72B
1. YES
2. YES