Reputation: 21
I have a curve generated by random and a line which runs through it. I have found the intersection coordinates of the curve with the line using interpolation. But now I have to find the areas of the curves between these points. My code is a s follows:
import numpy as np
import matplotlib.pylab as pl
from matplotlib import mlab
def find_inter_coord(a,x):
y = a-x
index = mlab.find((y[1:] >= 0) & (y[:-1] < 0)| (y[1:] < 0) & (y[:-1] >= 0))
crossing_index = [i - y[i] / (y[i+1] - y[i]) for i in index]
return crossing_index
data = np.random.uniform(low=-1000, high=-200, size=(100,))
pt = -750.5
pt_array = (pt) * 100
x = find_inter_coord(data, pt)
pl.figure(figsize = (10,5))
pl.plot(data)
pl.plot(pt_array)
pl.scatter(x, [pt for p in x], color='red')
The graph is as follows:
Now I need to find the areas of all the curves below the line pt_array.. How do I do this? Any help would be aprreciated. thanks
Upvotes: 1
Views: 128
Reputation: 114230
To find the areas of the curves, you can implement the following steps:
The area of a triangle between intersection point (xi, pt)
and sub-line point (xj, yj)
(areas marked A) is just 0.5 * (xj - xi) * (pt - yj)
. For areas marked C, just reverse the order of the x
coordinates.
The area of a trapezoid between two sub-line points (xi, yi)
and (xj, yj)
(areas marked B) is 0.5 * (xj - xi) * (yi + yj)
The areas A, C on the right show a corner case you may or may not need to handle differently, where there are no trapezoidal regions between the triangular ones.
Upvotes: 1