Colinearity test for three points

def subtract(p1, p2): if len(p1) == len(p2): return [p1i - p2i for p1i, p2i in zip(p1, p2)] def cross(a,b): if len(a) == len(b) == 3: ax, ay, az = a bx, by, bz = b return [ay*bz - az*by, az*bx - ax*bz, ax*by - ay*bx] if len(a) == len(b) == 2: ax, ay = a bx, by = b return [ax*by - bx*ay] def colinear(p1, p2, p3): res = [0]*(len(p1) - (1 if len(p1) == 2 else 0)) return cross(subtract(p3, p1), subtract(p2, p1)) == res # 2D p1 = [4, 5] p2 = [6, 7] p3 = [7, 9] print(colinear(p1,p2,p3)) # False p1 = [4, 5] p2 = [6, 7] p3 = [7, 8] print(colinear(p1,p2,p3)) # True # 3D p1 = [1, 4, 6] p2 = [5, 8, 12] p3 = [9, 5, 7] print(colinear(p1,p2,p3)) # False p1 = [10, 0, 5] p2 = [0, 10, 5] p3 = [5, 5, 5] print(colinear(p1,p2,p3)) # True