0

I am trying to apply four formulas to a set of coordinates I have in an array, to generate two additional arrays that I can then plot.

The two new arrays called 'internal_edge' and 'external_edge'.

In the code below, I have printed the four equations I need to apply to the X and Y of both 'internal_edge' and 'external_edge'.

import numpy as np
import math as m
from matplotlib import pyplot as plt

track_width = 0.25

centre_line = np.array([
    [5.2838386568469105, 0.5533114231405133],[5.366471208948866, 0.5138588293370626],[5.449968630730311, 0.47627584038467463],[5.534812288152224, 0.4418872118882958],[5.621950766713397, 0.4139674411266211],[5.711536576482786, 0.3955402988007556],[5.80273779150609, 0.38945006681201744],[5.893859166442468, 0.3968403768909704],[5.983006824666859, 0.41711137467093196],
])

x1, y1 = centre_line.T
#Internal X[0] Value:
print(track_width*m.cos(m.radians(m.degrees(m.atan2(y1[1]-y1[0],x1[1]-x1[0]))+90))+x1[1])
#Internal Y[0] Value:
print(track_width*m.sin(m.radians(m.degrees(m.atan2(y1[1]-y1[0],x1[1]-x1[0]))+90))+y1[1])
#External X[0] Value:
print(track_width*m.cos(m.radians(m.degrees(m.atan2(y1[1]-y1[0],x1[1]-x1[0]))-90))+x1[1])
#External Y[0] Value:
print(track_width*m.sin(m.radians(m.degrees(m.atan2(y1[1]-y1[0],x1[1]-x1[0]))-90))+y1[1])

internal_edge = np.array([
])
external_edge = np.array([
])

x2, y2 = internal_edge.T
x3, y3 = external_edge.T

plt.scatter(x1,y1,color='blue')
plt.scatter(x2,y2,color='green')
plt.scatter(x3,y3,color='red')
plt.show()

I now need to work out how to apply these to every single data point in my array to generate my new arrays.

Perfect Output Imperfect Output

2
  • Hi, welcome to StackOverflow. Just to clarify, are you trying to work out how to apply the formulas to [[(x1[0], y1[0]), (x1[1], y1[1])], [(x1[1], y1[1]), (x1[2], y1[2])], [(x1[2], y1[2]), (x1[3], y1[3])], ..., [(x1[i], y1[i]), (x1[i+1], y1[i+1])]]? Commented May 24, 2019 at 8:50
  • That is correct! Commented May 24, 2019 at 8:59

1 Answer 1

1

Here's how you can do it.

import numpy as np
from matplotlib import pyplot as plt

track_width = 0.25

centre_line = np.array([
    [5.2838386568469105, 0.5533114231405133],[5.366471208948866, 0.5138588293370626],[5.449968630730311, 0.47627584038467463],[5.534812288152224, 0.4418872118882958],[5.621950766713397, 0.4139674411266211],[5.711536576482786, 0.3955402988007556],[5.80273779150609, 0.38945006681201744],[5.893859166442468, 0.3968403768909704],[5.983006824666859, 0.41711137467093196],
])

x1, y1 = centre_line.T

# adjust for full dataset maybe?
x1 = np.append(x1, x1[0])
y1 = np.append(y1, y1[0])

angle = np.arctan2(y1[1:] - y1[:-1], x1[1:] - x1[:-1])


internal_edge = np.array([
    track_width*np.cos(angle + np.deg2rad(90)) + x1[1:],
    track_width*np.sin(angle + np.deg2rad(90)) + y1[1:],
])

external_edge = np.array([
    track_width*np.cos(angle - np.deg2rad(90)) + x1[1:],
    track_width*np.sin(angle - np.deg2rad(90)) + y1[1:],
])

x2, y2 = internal_edge
x3, y3 = external_edge

plt.scatter(x1,y1,color='blue')
plt.scatter(x2,y2,color='green')
plt.scatter(x3,y3,color='red')
plt.show()

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! That looks great. Running this with the full dataset is not quite perfect however. I've attached two images to the original post, one of how it should look (generated when I was testing by manually inputting the desired arrays), and the one generated by your code. Would you be able to take a look and let me know what you think? This is frying my brain at this point!
how about now? don't have the dataset to test on
That solves the missing datapoint problem, but we still have those two wrong values in the top left.
angle[121] == 0 which coincides with weird anomaly on the graph. Idk how 2 fix it something math related
There was a duplicate datapoint that was causing it. I've removed the duplicate (121) and now works fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.