0

Very new to this so thank you for your patience.

This is my situation, I have a set of start co-ordinates (3, 12 = the first being an x axis value and the second y axis) and a list of directions i.e. S, S, N, E, W. With every iteration of the directions I need to add or subtract them from the start co-ordinates to plot a route and eventually lead me to the final co-ordinates.

I have converted the N,E,S,W into numerical values i.e. (N becomes [0, 1] and S becomes [0, -1]. As the N and S directions would not affect the X axis hence the first value being 0. E would be [1, 0] and W [-1, 0].

My question is, if the first direction is N [0, 1] how to I add this to the start co-ordinates of [3, 12] so the answer is [3, 13] and continue in this manner with every subsequent direction?

I would also like to print the new co-ordinates after every direction has been added.

This is how i have begun, however i think i am getting mixed up with list, tuple and dictionary formatting.

start = [3, 12]
coordinates = dict({'N':[0,1], 'E':[1:0], 'S':[0:-1]. 'W':[-1:0]})
directions = ('S', 'S', 'W', 'S', 'S', 'S', 'E', 'E', 'E')

Any help would be much appreciated.

robbo500
  • 45
  • 6
  • 1
    What have you tried? Please add your code and/or errors in the question – rdas Jun 09 '22 at 09:51
  • `[0,1]` is a list, `[1:0]` is not. Note the difference between the numbers ;-) – André Jun 09 '22 at 09:51
  • the code in my question is where I have got to so far. I managed to essentially append a direction to the start i.e. I added N to the start and got ([3,12], [0,1]) when I actually want [3.13] – robbo500 Jun 09 '22 at 09:57

3 Answers3

0

you have some typos in your example but if I uderstand it correctly you wanted something like this:

start = [3, 12]
coordinates = {'N': [0, 1], 'E': [1, 0], 'S': [0, -1], 'W': [-1, 0]}
directions = ('S', 'S', 'W', 'S', 'S', 'S', 'E', 'E', 'E')

for d in directions:
    dx, dy = coordinates[d]
    start[0] += dx
    start[1] += dy
    print(start)
Marcin Cuprjak
  • 674
  • 5
  • 6
0

I advise you to use numpy to manipulate vectors in an easier way.

import numpy as np

start = np.array([3, 12])
coordinates = {
    'N': np.array([0, 1]), 
    'E': np.array([1, 0]), 
    'S': np.array([0, -1]),
    'W': np.array([-1, 0])
}
directions = ('S', 'S', 'W', 'S', 'S', 'S', 'E', 'E', 'E')

for direction in directions:
    start += coordinates[direction]
Mateo Vial
  • 658
  • 4
  • 13
0

Make a list of all the vectors (start and each direction), unzip it into its x and y components, and then sum these.

direction_vectors = {'N':[0,1], 'E':[1,0], 'S':[0,-1], 'W':[-1,0]}
directions = ['S', 'S', 'W', 'S', 'S', 'S', 'E', 'E', 'E']
start = [3, 12]
dx, dy = zip(*[start] + [direction_vectors[d] for d in directions])
end = [sum(dx), sum(dy)]

Alternatively, use numpy to sum the vectors directly.

import numpy as np
end = start + np.sum([direction_vectors[d] for d in directions], axis=0)
Stuart
  • 9,597
  • 1
  • 21
  • 30