2

I have a .txt file from which I find ordered pairs, and then draw a graphic using numpy and matplotlib. For example these are my ordered pairs:

[[(4.0, 0), (0, 6.0)], [(6.0, 0), (0, 3.0)]]

(Each sub-list represents a line in the final graphic)

The graphic looks like this:

grafik1

But I want to find the intersection between the two lines. And if there were more lines, how can I find the common area between them all? e.g.

grafik2

7
  • 1
    Hi Schoolboy, in your second question are you referring to the area in the first quadrant under all the lines? If you only have two lines, there would be no "common area between all the intersections", since there only would be one. Commented May 18, 2012 at 6:08
  • @user Yes, maybe you meant enclosed with some other lines like the axes? Commented May 18, 2012 at 6:12
  • It looks like the URLs to your graphics are no longer valid (they expired). Commented May 18, 2012 at 15:23
  • @user1305311, the links you posted are giving us an "access denied" error. Commented May 18, 2012 at 16:28
  • @JonasByström I can have maybe 2 or more lines. All depends on what is written in the .txt file. This is an example with 3 lines. And here I have to find intersection points. Just like this: img820.imageshack.us/img820/6471/ejem4.png Commented May 19, 2012 at 0:38

2 Answers 2

0

completed

I've made a number of assumptions, that are based on the OP's description of the problem but maybe their real problem is more complex and the question describes a simplification of it.

My assumptions in the code below are the following: the lines are described by two positive integer numbers, the graph is limited to the first quadrant, and the graph limits are strict, no additional blank space around the lines.

This is the code that produces the graph above:

import matplotlib.pyplot as plt
import numpy as np
from itertools import combinations
from fractions import Fraction

# print fraction
pf = lambda num, den: str(Fraction(num, den)).rjust(7)

lines = ((4,3), (5,5), (3,5), (7,6), (8, 6), (4,7), (6,4))
#lines = sorted(lines)
xmx, ymx = 0,0
for y, x in lines:
    xmx = x if x>xmx else xmx
    ymx = y if y>ymx else ymx
    plt.plot((0, x), (y,0), label=repr((y,x)))
for (y1, x1), (y2, x2) in combinations(lines, r=2):
    if x1*y2 == x2*y1:
        print((y1,x1), (y2, x2), ' '*34,  '(parallel lines)')
        continue # parallel lines
    x0n, x0d = x1*x2*(y2-y1), x1*y2-x2*y1
    y0n, y0d = y1*y2*(x2-x1), y1*x2-y2*x1
    inside = 0 <= x0n/x0d <= xmx and 0 <= y0n/y0d <=ymx
    print((y1,x1), (y2, x2), 
        '   Xo =', pf(x0n, x0d),
        '   Yo =', pf(y0n, y0d),
        '   (inside)' if inside else '   (outside)'
        )
    if inside:
        plt.scatter(x0n/x0d, y0n/y0d, color='w', ec='k')
# fill area below lines
N = 201
x = np.linspace(0, xmx, N)
y = np.ones(N)*ymx
z = np.zeros(N)
for y0, x0 in lines: 
    y = np.min((y, np.max((y0-y0*x/x0, z), axis=0)), axis=0)
plt.fill_between(x, y, color='yellow')

# Axes' minutiae
plt.xlim((0, xmx))
plt.ylim((0, ymx))
plt.legend()
plt.grid(1)
plt.show()

and this is the output produced on the console

(4, 3) (5, 5)    Xo =      -3    Yo =       8    (outside)
(4, 3) (3, 5)    Xo =   15/11    Yo =   24/11    (inside)
(4, 3) (7, 6)    Xo =     -18    Yo =      28    (outside)
(4, 3) (8, 6)                                    (parallel lines)
(4, 3) (4, 7)    Xo =       0    Yo =       4    (inside)
(4, 3) (6, 4)    Xo =      12    Yo =     -12    (outside)
(5, 5) (3, 5)    Xo =       5    Yo =       0    (inside)
(5, 5) (7, 6)    Xo =      12    Yo =      -7    (outside)
(5, 5) (8, 6)    Xo =       9    Yo =      -4    (outside)
(5, 5) (4, 7)    Xo =     7/3    Yo =     8/3    (inside)
(5, 5) (6, 4)    Xo =       2    Yo =       3    (inside)
(3, 5) (7, 6)    Xo =  120/17    Yo =  -21/17    (outside)
(3, 5) (8, 6)    Xo =   75/11    Yo =  -12/11    (outside)
(3, 5) (4, 7)    Xo =     -35    Yo =      24    (outside)
(3, 5) (6, 4)    Xo =    10/3    Yo =       1    (inside)
(7, 6) (8, 6)    Xo =       6    Yo =       0    (inside)
(7, 6) (4, 7)    Xo =  126/25    Yo =   28/25    (inside)
(7, 6) (6, 4)    Xo =      -3    Yo =    21/2    (outside)
(8, 6) (4, 7)    Xo =    21/4    Yo =       1    (inside)
(8, 6) (6, 4)    Xo =     -12    Yo =      24    (outside)
(4, 7) (6, 4)    Xo =   28/13    Yo =   36/13    (inside)
Sign up to request clarification or add additional context in comments.

Comments

0

To find intersections, you could use the shapely library. For the common area, if it is the area that's under all curves, I'd use something like:

numpy.trapz(x, np.minimum(*[y1, y2, ..., yn]))

Where the x y arrays contain the intersections. You could also loop over the pairs and find all intersections analytically since they are straight lines.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.