check if three points are collinear in 3d in short the idea is that we are able to create a triangle using three points and the area of that triangle can determine if the three points are collinear or not first we create two vectors with the same initial point from the three points then we will calculate the crossproduct of them the length of the cross vector is numerically equal to the area of a parallelogram finally the area of the triangle is equal to half of the area of the parallelogram since we are only differentiating between zero and anything else we can get rid of the square root when calculating the length of the vector and also the division by two at the end from a second perspective if the two vectors are parallel and overlapping we can t get a nonzero perpendicular vector since there will be an infinite number of orthogonal vectors to simplify the solution we will not calculate the length but we will decide directly from the vector whether it is equal to 0 0 0 or not read more https math stackexchange coma1951650 pass two points to get the vector from them in the form x y z createvector0 0 0 1 1 1 1 1 1 createvector45 70 24 47 32 1 2 38 23 createvector14 1 8 7 6 4 7 7 12 get the cross of the two vectors ab and ac i used determinant of 2x2 to get the determinant of the 3x3 matrix in the process read more https en wikipedia orgwikicrossproduct https en wikipedia orgwikideterminant get3dvectorscross3 4 7 4 9 2 55 22 11 get3dvectorscross1 1 1 1 1 1 0 0 0 get3dvectorscross4 3 0 3 9 12 36 48 27 get3dvectorscross17 67 4 7 6 78 9 5 4 78 19 33 123 2594 277 15110000000004 129 11260000000001 check if vector is equal to 0 0 0 of not sine the algorithm is very accurate we will never get a zero vector so we need to round the vector axis because we want a result that is either true or false in other applications we can return a float that represents the collinearity ratio iszerovector0 0 0 accuracy10 true iszerovector15 74 32 accuracy10 false iszerovector15 74 32 accuracy10 false check if three points are collinear or not 1 create tow vectors ab and ac 2 get the cross vector of the tow vectors 3 calcolate the length of the cross vector 4 if the length is zero then the points are collinear else they are not the use of the accuracy parameter is explained in iszerovector docstring arecollinear4 802293498137402 3 536233125455244 0 2 186788107953106 9 24561398001649 7 141509524846482 1 530169574640268 2 447927606600034 3 343487096469054 true arecollinear6 2 6 6 200213806439997 4 930157614926678 4 482371908289856 4 085171149525941 2 459889509029438 4 354787180795383 true arecollinear2 399001826862445 2 452009976680793 4 464656666157666 3 682816335934376 5 753788986533145 9 490993909044244 1 962903518985307 3 741415730125627 7 false arecollinear1 875375340689544 7 268426006071538 7 358196269835993 3 546599383667157 4 630005261513976 3 208784032924246 2 564606140206386 3 937845170672183 7 false pass two points to get the vector from them in the form x y z create_vector 0 0 0 1 1 1 1 1 1 create_vector 45 70 24 47 32 1 2 38 23 create_vector 14 1 8 7 6 4 7 7 12 get the cross of the two vectors ab and ac i used determinant of 2x2 to get the determinant of the 3x3 matrix in the process read more https en wikipedia org wiki cross_product https en wikipedia org wiki determinant get_3d_vectors_cross 3 4 7 4 9 2 55 22 11 get_3d_vectors_cross 1 1 1 1 1 1 0 0 0 get_3d_vectors_cross 4 3 0 3 9 12 36 48 27 get_3d_vectors_cross 17 67 4 7 6 78 9 5 4 78 19 33 123 2594 277 15110000000004 129 11260000000001 i j k check if vector is equal to 0 0 0 of not sine the algorithm is very accurate we will never get a zero vector so we need to round the vector axis because we want a result that is either true or false in other applications we can return a float that represents the collinearity ratio is_zero_vector 0 0 0 accuracy 10 true is_zero_vector 15 74 32 accuracy 10 false is_zero_vector 15 74 32 accuracy 10 false check if three points are collinear or not 1 create tow vectors ab and ac 2 get the cross vector of the tow vectors 3 calcolate the length of the cross vector 4 if the length is zero then the points are collinear else they are not the use of the accuracy parameter is explained in is_zero_vector docstring are_collinear 4 802293498137402 3 536233125455244 0 2 186788107953106 9 24561398001649 7 141509524846482 1 530169574640268 2 447927606600034 3 343487096469054 true are_collinear 6 2 6 6 200213806439997 4 930157614926678 4 482371908289856 4 085171149525941 2 459889509029438 4 354787180795383 true are_collinear 2 399001826862445 2 452009976680793 4 464656666157666 3 682816335934376 5 753788986533145 9 490993909044244 1 962903518985307 3 741415730125627 7 false are_collinear 1 875375340689544 7 268426006071538 7 358196269835993 3 546599383667157 4 630005261513976 3 208784032924246 2 564606140206386 3 937845170672183 7 false
Vector3d = tuple[float, float, float]
Point3d = tuple[float, float, float]


def create_vector(end_point1: Point3d, end_point2: Point3d) -> Vector3d:
    
    x = end_point2[0] - end_point1[0]
    y = end_point2[1] - end_point1[1]
    z = end_point2[2] - end_point1[2]
    return (x, y, z)


def get_3d_vectors_cross(ab: Vector3d, ac: Vector3d) -> Vector3d:
    
    x = ab[1] * ac[2] - ab[2] * ac[1]  
    y = (ab[0] * ac[2] - ab[2] * ac[0]) * -1  
    z = ab[0] * ac[1] - ab[1] * ac[0]  
    return (x, y, z)


def is_zero_vector(vector: Vector3d, accuracy: int) -> bool:
    
    return tuple(round(x, accuracy) for x in vector) == (0, 0, 0)


def are_collinear(a: Point3d, b: Point3d, c: Point3d, accuracy: int = 10) -> bool:
    
    ab = create_vector(a, b)
    ac = create_vector(a, c)
    return is_zero_vector(get_3d_vectors_cross(ab, ac), accuracy)