0

I want to ask something about how to calculate the intersected area of the irregular shape that has been made by 2 lines. I've been using shapely to calculate the irregular shape it can work but it can't give the area if my shape is intersected.enter image description here

That's the example of the image I need to calculate the area with value somewhat like this :

x1 = [636.021, 636.357, 636.692, 637.028, 637.364, 637.699, 638.035, 638.37, 638.706, 639.041, 639.377, 639.712, 640.047, 640.382, 640.717, 641.053, 641.388, 641.723, 642.058, 642.393, 642.727, 643.062, 643.397, 643.732, 644.066, 644.401, 644.736, 645.07, 645.405, 645.739, 646.073, 646.408, 646.742, 647.076, 647.41, 647.745, 648.079, 648.413, 648.747, 649.081, 649.414, 649.748, 650.082, 650.416, 650.75, 651.083, 651.417, 651.75, 652.084, 652.417, 652.751, 653.084, 653.417, 653.751, 654.084, 654.417, 654.75, 655.083, 655.416, 655.749, 656.021]
y1 = [34.19, 34.58, 34.58, 34.48, 35.8, 35.76, 35.35, 35.92, 36.44, 36.92, 36.73, 37.48, 37.98, 37.17, 39.01, 39.73, 39.7, 39.05, 38.72, 39.44, 39.81, 38.79, 38.56, 38.67, 38.62, 39.85, 39.11, 39.58, 39.38, 40.58, 39.71, 43.05, 42.68, 42.7, 42.48, 44.52, 45.36, 45.36, 46.82, 46.2, 46.48, 46.05, 46.92, 47.43, 47.74, 45.59, 46.83, 47.6, 47.04, 47.11, 47.63, 49.27, 49.19, 49.39, 49.57, 49.85, 51.45, 51.43, 51.26, 53.04, 51.96180180180196]
x2 = [636.021, 636.357, 636.692, 637.028, 637.364, 637.699, 638.035, 638.37, 638.706, 639.041, 639.377, 639.712, 640.047, 640.382, 640.717, 641.053, 641.388, 641.723, 642.058, 642.393, 642.727, 643.062, 643.397, 643.732, 644.066, 644.401, 644.736, 645.07, 645.405, 645.739, 646.073, 646.408, 646.742, 647.076, 647.41, 647.745, 648.079, 648.413, 648.747, 649.081, 649.414, 649.748, 650.082, 650.416, 650.75, 651.083, 651.417, 651.75, 652.084, 652.417, 652.751, 653.084, 653.417, 653.751, 654.084, 654.417, 654.75, 655.083, 655.416, 655.749, 656.021]
y2 = [40.84, 41.75, 41.43, 41.55, 42.19, 40.75, 41.51, 41.6, 40.68, 41.04, 41.16, 41.38, 41.41, 40.86, 41.51, 41.87, 41.53, 41.42, 40.82, 40.18, 39.8, 39.7, 38.75, 37.97, 38.69, 39.06, 37.39, 38.17, 38.69, 38.72, 37.75, 39.73, 39.0, 39.24, 40.28, 40.52, 40.27, 40.76, 41.15, 42.15, 41.7, 41.56, 40.79, 41.13, 42.76, 40.63, 40.71, 40.95, 41.24, 40.9, 40.77, 42.71, 42.05, 42.36, 41.91, 43.73, 43.64, 43.42, 43.2, 44.39, 43.4506606606608]

thank you !

NexusRay
  • 43
  • 5
  • 1
    Is it the case that all of your x values are common between the 2 lines? It appears so from your data. If true, you can easily numerically integrate this... I think. Also, you show the lines crossing... do you want absolute area (any enclosed area) or integral of the difference, where they would offset? – AirSquid Mar 22 '20 at 18:59
  • @Jeff H yes its coming from 2 lines from the experimental data and the x values is same. Yess i want the absolute area of any encloses area – NexusRay Mar 22 '20 at 19:06
  • Why do you use Shapely for this? – Georgy Mar 22 '20 at 20:20
  • Does this answer your question? [Find the area between two curves plotted in matplotlib (fill\_between area)](https://stackoverflow.com/questions/25439243/find-the-area-between-two-curves-plotted-in-matplotlib-fill-between-area) – Georgy Mar 22 '20 at 20:23
  • @Georgy I'm using shapely to find the enclosed area of 2 lines.. But if the polygon area is intersected the polygon.area function return to me a false value of the area – NexusRay Mar 23 '20 at 08:36

1 Answers1

0

* CORRECTION: This only works for integrated area, not "absolute area" *

OK. I think your answer lies in scipy.integrate which can do integration by trapezoids. This will work because your x values are common, so that will enable you to just use a composite function such as f(x) = y3 = y2 - y1.

#integrate trapezoids

from scipy import integrate

x1 = [636.021, 636.357, 636.692, 637.028, 637.364, 637.699, 638.035, 638.37, 638.706, 639.041, 639.377, 639.712, 640.047, 640.382, 640.717, 641.053, 641.388, 641.723, 642.058, 642.393, 642.727, 643.062, 643.397, 643.732, 644.066, 644.401, 644.736, 645.07, 645.405, 645.739, 646.073, 646.408, 646.742, 647.076, 647.41, 647.745, 648.079, 648.413, 648.747, 649.081, 649.414, 649.748, 650.082, 650.416, 650.75, 651.083, 651.417, 651.75, 652.084, 652.417, 652.751, 653.084, 653.417, 653.751, 654.084, 654.417, 654.75, 655.083, 655.416, 655.749, 656.021]
y1 = [34.19, 34.58, 34.58, 34.48, 35.8, 35.76, 35.35, 35.92, 36.44, 36.92, 36.73, 37.48, 37.98, 37.17, 39.01, 39.73, 39.7, 39.05, 38.72, 39.44, 39.81, 38.79, 38.56, 38.67, 38.62, 39.85, 39.11, 39.58, 39.38, 40.58, 39.71, 43.05, 42.68, 42.7, 42.48, 44.52, 45.36, 45.36, 46.82, 46.2, 46.48, 46.05, 46.92, 47.43, 47.74, 45.59, 46.83, 47.6, 47.04, 47.11, 47.63, 49.27, 49.19, 49.39, 49.57, 49.85, 51.45, 51.43, 51.26, 53.04, 51.96180180180196]
x2 = [636.021, 636.357, 636.692, 637.028, 637.364, 637.699, 638.035, 638.37, 638.706, 639.041, 639.377, 639.712, 640.047, 640.382, 640.717, 641.053, 641.388, 641.723, 642.058, 642.393, 642.727, 643.062, 643.397, 643.732, 644.066, 644.401, 644.736, 645.07, 645.405, 645.739, 646.073, 646.408, 646.742, 647.076, 647.41, 647.745, 648.079, 648.413, 648.747, 649.081, 649.414, 649.748, 650.082, 650.416, 650.75, 651.083, 651.417, 651.75, 652.084, 652.417, 652.751, 653.084, 653.417, 653.751, 654.084, 654.417, 654.75, 655.083, 655.416, 655.749, 656.021]
y2 = [40.84, 41.75, 41.43, 41.55, 42.19, 40.75, 41.51, 41.6, 40.68, 41.04, 41.16, 41.38, 41.41, 40.86, 41.51, 41.87, 41.53, 41.42, 40.82, 40.18, 39.8, 39.7, 38.75, 37.97, 38.69, 39.06, 37.39, 38.17, 38.69, 38.72, 37.75, 39.73, 39.0, 39.24, 40.28, 40.52, 40.27, 40.76, 41.15, 42.15, 41.7, 41.56, 40.79, 41.13, 42.76, 40.63, 40.71, 40.95, 41.24, 40.9, 40.77, 42.71, 42.05, 42.36, 41.91, 43.73, 43.64, 43.42, 43.2, 44.39, 43.4506606606608]

# compute y3 which is the difference between y2 and y1.

y3 = [t1 - t2 for (t1, t2) in zip(y2, y1)]

# integrate

trap_areas = integrate.cumtrapz(y3, x1)

# let's check the first one...

print(trap_areas[:3])

# area of first trapezoid
actual_area_first = (x1[1] - x1[0]) * 1/2 * ((y2[0]-y1[0]) + (y2[1]-y1[1]))

print(actual_area_first)

integrated_area = sum(trap_areas)

print(f'integrated: {integrated_area : 0.3f}')

Yields:

[2.32176 4.67011 7.00867]
2.321760000000089
integrated:  654.699
AirSquid
  • 10,214
  • 2
  • 7
  • 31