Intersection of Two Line Segments

image_missing
image_missing

Code

#!/usr/bin/python3
# ===================================================================
# line segments define infinity lines. where do they intersect?
# ===================================================================
# From: stackoverflow.com/questions/20677795/
#       how-do-i-compute-the-intersection-point-of-two-lines
# ===================================================================
# The challenge: This code's error testing and reporting is not
# structured very well. See if you can do better?
# ===================================================================

import numpy as np
import matplotlib.pyplot as plt

# -------------------------------------------------------------------
# ---- find the intersection of two lines
# -------------------------------------------------------------------

def find_intersection(l1,l2,title=''):
    
        p1,p2 = l1
        p3,p4 = l2
        
        x1,y1 = p1
        x2,y2 = p2
        x3,y3 = p3
        x4,y4 = p4

        try:

            px = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4)) / \
                 ((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4) ) 
            py = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4)) / \
                 ((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4))
            return (px, py)
        
        except Exception as e:
            print(f'{title} {e}')
            
        return None


# --------------------------------------------------------------------
# ---- plot lines
# --------------------------------------------------------------------

def plot_lines(l1,l2,i,title=None):

    # ---- plot x,y axes and grid

    plt.axhline(0,color='black')      # x axis
    plt.axvline(0,color='black')      # y axis
    plt.xlim(-20,20)
    plt.ylim(-20,20)
    plt.grid()

    # ---- display title

    if title is not None:
        plt.title(label=title,fontsize=20,color='black')

    # ---- plot line 1

    x = np.array([ l1[0][0], l1[1][0] ])
    y = np.array([ l1[0][1], l1[1][1] ])

    plt.plot(x,y,color='black')
    
    plt.plot(x,y,'o',color='black')

    # ---- plot line 2

    x = np.array([ l2[0][0], l2[1][0] ])
    y = np.array([ l2[0][1], l2[1][1] ])
    
    plt.plot(x,y,color='black')
    
    plt.plot(x,y,'o',color='black')

    # ---- plot intersection

    if i is not None:
        x = np.array([ i[0], ])
        y = np.array([ i[1], ])
        plt.plot(x,y,'o',color='red')

    # render plot
    
    plt.show()

# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

if __name__ == '__main__':

    def results(i,title=None):

        if title is not None:
            print(title + ' ',end='')

        if i is None:
            print('lines do not intersect')
        else:
            print(f'lines intersect at {i}')
            
    # ---- test 1

    t  = 'Test 1'     
    l1 = ((0,0), (10,10))
    l2 = ((0,10),(10,0))
    i = find_intersection(l1,l2)
    results(i,t)
    plot_lines(l1,l2,i,t)

    # ---- test 2

    t  = 'Test 2'     
    l1 = ((-10,-10),(10,10))
    l2 = ((-10,10),(10,-10))
    i = find_intersection(l1,l2)
    results(i,t)
    ##plot_lines(l1,l2,i,t)

    # ---- test 3

    t  = 'Test 3'    
    l1 = ((0,0),(10,10))
    l2 = ((0,-5),(10,-10))
    i = find_intersection(l1,l2,t)
    results(i,t)
    ##plot_lines(l1,l2,i,t)

    # ---- test 4

    t  = 'test 4'     
    l1 = ((0,4),(10,4))
    l2 = ((0,8),(10,8))
    i = find_intersection(l1,l2,t)
    results(i,t)
    ##plot_lines(l1,l2,i,t)

    # ---- test 5

    t  = 'test 5'     
    l1 = ((-5,-5),(-5,5))
    l2 = ((5,-5),(5,5))
    i = find_intersection(l1,l2,t)
    results(i,t)
    ##plot_lines(l1,l2,i,t)

    # ---- test 6

    t  = 'test 6'     
    l1 = ((-5,0),(5,10))
    l2 = ((5,0),(-5,-10))
    i = find_intersection(l1,l2,t)
    results(i,t)
    plot_lines(l1,l2,i,t)