Nicole.Andy
Nicole.Andy

Reputation: 21

Area of a curve between intersection points on a plane

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:

Output Graph 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

Answers (1)

Mad Physicist
Mad Physicist

Reputation: 114230

To find the areas of the curves, you can implement the following steps:

  1. Find all the points that are below the line (green dots).
  2. For each pair of intersection points, if there are points that are below the line between them, do the following:
    1. Compute the area of the triangle up to the first data point under the line (Areas marked A)
    2. Compute the areas of all the trapezoids between successive points (Areas marked B)
    3. Compute the area of the triangle between the last point under the line and the next intersection (Areas marked C)

enter image description here

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

Related Questions