Code Example: parallel_lines.py

#!/usr/bin/python3
# ===================================================================
# are two line segments parallel?
# -------------------------------------------------------------------
# line segments have 4 values (p0_x, P0_y, p1_x, p1_y)
# -------------------------------------------------------------------
# can I do this with the sin or cos of the slope angle to remove
# the pesky divide by zero?
# ===================================================================

def parallel_lines(seg0,seg1):

    accuracy = 0.001           # calculation accuracy

    # ---- get segment data

    s0x0 = seg0[0]             # segment 0, point 0, X coord
    s0y0 = seg0[1]             # segment 0, point 0, Y coord
    s0x1 = seg0[2]             # segment 0, point 1, X coord
    s0y1 = seg0[3]             # segment 0, point 1, Y coord

    s1x0 = seg1[0]             # segment 1, point 0, X coord
    s1y0 = seg1[1]             # segment 1, point 0, Y coord
    s1x1 = seg1[2]             # segment 1, point 1, X coord
    s1y1 = seg1[3]             # segment 1, point 1, Y coord

    s0dx = s0x1 - s0x0         # segment 0 dx
    s0dy = s0y1 - s0y0         # segment 0 dy
    s1dx = s1x1 - s1x0         # segment 1 dx
    s1dy = s1y1 - s1y0         # segment 1 dy

    ##print(f's0dx = {s0dx}')
    ##print(f's0dy = {s0dy}')
    ##print(f's1dx = {s1dx}')
    ##print(f's1dy = {s1dy}')

    # ---- two vertical or horizontal parallel lines?

    if abs(s0dx) < accuracy and abs(s1dx) < accuracy:
        return True
    if abs(s0dy) < accuracy and abs(s1dy) < accuracy:
        return True

    # ---- one line is vertical?

    if abs(s0dx) < accuracy or abs(s1dx) < accuracy:
        return False

    # ---- parallel lines - same slope?

    m0 = s0dy/s0dx             # slope of segment 0
    m1 = s1dy/s1dx             # slope of segment 1

    ##print(f'm0         = {m0}   (slope)')
    ##print(f'm1         = {m1}   (slope)')
    ##print(f'm0-m1      = {m0-m1}   (dm)')
    ##print(f'abs(m0-m1) = {abs(m0-m1)}')
  
    # ---- line segments have the same slope?

    if abs(m0-m1) < accuracy:
        return True

    return False

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

if __name__ == '__main__':

    tests = [
        [ (-100.0,200.0,100.0,200.0),
          (-50.0,-50.0,250.0,350.0), 'test #1' ],

        [ (50.0,300.0,350.0,300.0),
          (0.0,-50.0,300.0,350.0), 'test #2' ],

        [ (-200.0,-200.0,-200.0,100.0),
          (-40.0,-100.0,200.0,-200.0), 'test #3' ],

        [ (-325.0,300.0,-325.0,200.0),
          (-350.0,300.0,-350.0,200.0), 'test #4 - vertical parallel lines' ],

        [ (-200.0,325.0,0.0,325.0),
          (-200.0,350.0,0.0,350.0), 'test #5 - horizontal parallel lines' ],

        [ (-200.0,150.0,-150.0,250.0),
          (-225.0,150.0,-175.0,250.0), 'test 6 - same slope parallel lines' ],

        [ (-300.0,-300.0,-300.0,100.0),
          (-350.0,-200.0,-50.0,-300.0), 'test #7' ],

        [ (250.0,0.0,350.0,0.0),
          (300.0,-100.0,300,100.0), 'test #8' ],
        ]

    for t in tests:
        print('--------------------------------------------')
        print(t[2])
 
        tf = parallel_lines(t[0],t[1])

        if tf:
            print('lines are parallel')
        else:
            print('lines are not parallel')

    print('--------------------------------------------')
    print()