def distance_point_line_sqrd_xy(point, line): """Compute the squared distance between a point and a line lying in the XY-plane. Parameters ---------- point : sequence of float XY(Z) coordinates of a 2D or 3D point (Z will be ignored). line : list, tuple Line defined by two points. Returns ------- float The squared distance between the point and the line. Notes ----- This implementation computes the orthogonal squared distance from a point P to a line defined by points A and B as twice the area of the triangle ABP divided by the length of AB [1]_. References ---------- .. [1] Wikipedia. *Distance from a point to a line*. Available at: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line. """ a, b = line ab = subtract_vectors_xy(b, a) pa = subtract_vectors_xy(a, point) pb = subtract_vectors_xy(b, point) l = cross_vectors_xy(pa, pb)[2]**2 l_ab = length_vector_sqrd_xy(ab) return l / l_ab
def distance_point_point_sqrd_xy(a, b): """Compute the squared distance between points a and b lying in the XY plane. Parameters ---------- a : sequence of float XY(Z) coordinates of the first point. b : sequence of float) XY(Z) coordinates of the second point. Returns ------- float Squared distance between a and b in the XY-plane. Examples -------- >>> distance([0.0, 0.0], [2.0, 0.0]) 4.0 >>> distance([0.0, 0.0, 0.0], [2.0, 0.0, 0.0]) 4.0 >>> distance([0.0, 0.0, 1.0], [2.0, 0.0, 1.0]) 4.0 """ ab = subtract_vectors_xy(b, a) return length_vector_sqrd_xy(ab)