The function below gives out all the points of intersection below. It is related to my previous issues post: prev post. The function has 2 arrays b, c
that are dependent on a main array a
.When either b
or c
crosses the a
it has to wait for the other array to cross it before it starts calculating the lines of cross once again. So if b
crosses a
first, b
will have to wait till c
crosses a before it looks out for crosses again. This is further on explained better with a expected result on my prev post link. I am trying to add a bit where it will show the order of the crosses. The list would be like b,c,b,c,b,c...
or c,b,c,b,c...
. So essentially I want to add labels of the crossings to the function below.
Variables:
a = np.array([9887.89 9902.99 9902.99 9910.23 9920.79 9911.34 9920.01 9927.51 9932.3
9932.33 9928.87 9929.22 9929.22 9935.24 9935.24 9935.26 9935.26 9935.68
9935.68 9940.5 ])
b = np.array([9935.26 9935.26 9935.68 9935.68 9940.5 9925.19 9925.19 9929.62 9929.65
9929.93 9932.55 9936.81 9936.84 9937.26 9932.55 9932.55 9932.55 9932.6
9932.6 9932.6])
c = np.array([9928.87 9929.22 9929.22 9935.24 9935.24 9935.26 9935.26 9935.68 9935.68
9940.5 9925.19 9925.19 9929.62 9929.65 9929.93 9932.55 9936.81 9936.84
9937.26 9932.55])
Function:
def intersection_points(a, *others):
others = np.array(others)
indices = np.argwhere(
((a[:-1] < others[..., :-1]) & (a[1:] > others[..., 1:])) |
((a[:-1] > others[..., :-1]) & (a[1:] < others[..., 1:])) |
(a[:-1] == others[..., :-1]))
return indices[indices[:, 1].argsort()] # sort by i
ip = intersection_points(a, b, c)
idxx = np.concatenate(([ip[0]], ip[1:][ip[:-1, 0] != ip[1:, 0]]))
idxx = idxx[:,1]
question from:
https://stackoverflow.com/questions/65949418/adding-labels-to-line-crossings-in-numpy-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…