def distance_point_line_sqrd(point, line): """Compute the squared distance between a point and a line. Parameters ---------- point : sequence of float XYZ coordinates of the point. line : list, tuple Line defined by two points. Returns ------- float The squared distance between the point and the line. Notes ----- For more info, see [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(b, a) pa = subtract_vectors(a, point) pb = subtract_vectors(b, point) l = length_vector_sqrd(cross_vectors(pa, pb)) l_ab = length_vector_sqrd(ab) return l / l_ab
def distance_point_line_sqrd(point, line): """Compute the squared distance between a point and a line.""" a, b = line ab = subtract_vectors(b, a) pa = subtract_vectors(a, point) pb = subtract_vectors(b, point) l = length_vector_sqrd(cross_vectors(pa, pb)) l_ab = length_vector_sqrd(ab) return l / l_ab
def distance_point_point_sqrd(a, b): """Compute the squared distance bewteen points a and b. Parameters ---------- a : sequence of float XYZ coordinates of point a. b : sequence of float XYZ coordinates of point b. Returns ------- d2 : float Squared distance bewteen a and b. Examples -------- >>> distance_point_point_sqrd([0.0, 0.0, 0.0], [2.0, 0.0, 0.0]) 4.0 See Also -------- distance_point_point_sqrd_xy """ ab = subtract_vectors(b, a) return length_vector_sqrd(ab)
def circle_from_points(a, b, c): """Construct a circle from three points. Parameters ---------- a : sequence of float XYZ coordinates. b : sequence of float XYZ coordinates. c : sequence of float XYZ coordinates. Returns ------- circle : tuple Center, radius, normal of the circle. References ---------- https://en.wikipedia.org/wiki/Circumscribed_circle Examples -------- >>> """ ab = subtract_vectors(b, a) cb = subtract_vectors(b, c) ba = subtract_vectors(a, b) ca = subtract_vectors(a, c) ac = subtract_vectors(c, a) bc = subtract_vectors(c, b) normal = normalize_vector(cross_vectors(ab, ac)) d = 2 * length_vector_sqrd(cross_vectors(ba, cb)) A = length_vector_sqrd(cb) * dot_vectors(ba, ca) / d B = length_vector_sqrd(ca) * dot_vectors(ab, cb) / d C = length_vector_sqrd(ba) * dot_vectors(ac, bc) / d Aa = scale_vector(a, A) Bb = scale_vector(b, B) Cc = scale_vector(c, C) center = sum_vectors([Aa, Bb, Cc]) radius = length_vector(subtract_vectors(a, center)) return center, radius, normal