title: Area intersection in Python
body: <p>I have a code that takes a condition C as an input, and computes the solution to  my problem as an 'allowed area' A on the (x,y) space. This area is made of several 'tubes', which are defined by 2 lines that can never cross.</p>
  
  <p>The final result I'm looking for must satisfy k conditions {C1, .., Ck}, and is therefore an intersection S between k areas {A1, .. , Ak}. </p>
  
  <p>Here is an example with 2 conditions (A1: green, 3 tubes. A2: purple, 1 tube); the solution S is in red. </p>
  
  <p><a href="https://i.stack.imgur.com/LgKo0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LgKo0.png" alt="Output of the code for 2 conditions"></a></p>
  
  <p>How can I find S when I'm dealing with 4 areas of around 10 tubes each? (The final plot is awful!)</p>
  
  <p>I would need to be able to plot it, and to find the mean coordinate and the variance of the points in S (variance of each coordinate). [If there is an efficient way of knowing whether a point P belongs to S or not, I’ll just use a Monte Carlo method].</p>
  
  <p>Ideally, I’d also like to be able to implement “forbidden tubes” that I would remove from S [it might be a bit more complicated than intersecting S with the outside of my forbidden area, since two tubes from the same area can cross (even if the lines defining a tube never cross)].</p>
  
  <hr>
  
  <p>Note: </p>
  
  <ul>
  <li><p>The code also stores the arc length of the lines.</p></li>
  <li><p>The lines are stored as arrays of points (around 1000 points per line). The two lines defining a tube do not necessarily have the same number of points, but Python can interpolate ALL of them as a function of their arc length in 1 second.</p></li>
  <li><p>The lines are parametric functions (i.e. we cannot write y = f(x), since the lines are allowed to be vertical). </p></li>
  <li><p>The plot was edited with paint to get the result on the right... Not very efficient!</p></li>
  </ul>
  
  <hr>
  
  <p>Edit:</p>
  
  <ul>
  <li><p>I don't know how I can use plt.fill_between for a multiple intersection (I can do it here for 2 conditions, but I need the code to do it automatically when there are too many lines for eye judgement).</p></li>
  <li><p>For now I just generate the lines. I didn’t write anything for finding the final solution since I absolutely don’t know which structure is the most adapted for this. [However, a previous version of the code was able to find the intersection points between the lines of 2 different tubes, and I was planning to pass them as polygons to shapely, but this implied several other problems..]</p></li>
  <li><p>I don't think I can do it with <code>sets</code>: scanning the whole (x,y) area at required precision represents around 6e8 points... [The lines have only 1e3 points thanks to a variable step size (adapts to the curvature), but the whole problem is quite large]</p></li>
  </ul>