def distance_from_point(l0: Point, l1: Point, p: Point): ''' source: http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html condition: distance l0, l1 can't be zero :param l0: :param l1: :param p: :return: ''' vec0 = Vector.create_from_points(l0, p) print("vec0", vec0) vec1 = Vector.create_from_points(l1, p) print("vec1", vec1) vec2 = Vector.create_from_points(l0, l1) print("vec2", vec2) vec_cx = vec0.cross(vec1) print("vec_cx", vec_cx) num_sq = vec_cx.dot(vec_cx) den_sq = vec2.dot(vec2) print("N:", num_sq) print("D:", den_sq) dist_sq = mpmath.fdiv(num_sq, den_sq) return mpmath.sqrt(dist_sq)
def create_from_point_vector(normal_vector: Vector, point: Point): d = normal_vector.dot(Vector.convert_point(point)) plane_params = [ normal_vector.x, normal_vector.y, normal_vector.z, mpmath.fneg(d) ] return Plane(plane_params)
def test_vector_dot(self): v0 = Vector.from_float(-14.7, 26.4, 18.2) v1 = Vector.from_float(26.47, 76.42, 108.2) vec_dot = v0.dot(v1) print(vec_dot) self.assertAlmostEqual(3597.619, float(vec_dot), 12)
def create_from_3points(points): vec_a = Vector.create_from_points(points[0], points[1]) vec_b = Vector.create_from_points(points[0], points[2]) normal_vector = vec_a.cross(vec_b) d = normal_vector.dot(Vector.convert_point(points[2])) plane_params = [ normal_vector.a, normal_vector.b, normal_vector.c, mpmath.fneg(d) ] return Plane(plane_params)
def distance_from_point3(l0: Point, l1: Point, p: Point): ''' http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html :param l0: :param l1: :param p: :return: ''' vec_v = Vector.create_from_points(l0, p) vec_w = Vector.create_from_points(l1, p) vec_x = Vector.create_from_points(l0, l1) c = vec_v.cross(vec_w) nom_sq = c.dot(c) den_sq = vec_x.dot(vec_x) dist = mpmath.sqrt(mpmath.fdiv(nom_sq, den_sq)) return dist
def test_vector_magnitude(self): p0 = Point.from_float(-14.7, 26.4, 18.2) p1 = Point.from_float(26.47, 76.42, 108.2) vec = Vector.create_from_points(p0, p1) print(vec) self.assertAlmostEqual(41.17, float(vec.a), 2) self.assertAlmostEqual(50.02, float(vec.b), 2) self.assertAlmostEqual(90.00, float(vec.c), 2) mag = vec.magnitude() print(mag) self.assertAlmostEqual(110.891700771519, float(mag), 12) vec = Vector.from_float(0.0027, 10705, 90.00004) mag = vec.magnitude() self.assertAlmostEqual(10705.378321535736, float(mag), 12)
def test_cross_product(self): v0 = Vector.from_float(-14.7, 26.4, 18.2) v1 = Vector.from_float(26.47, 76.42, 108.2) vec_cross = v0.cross(v1) # print(vec_cross) self.assertAlmostEqual(1465.636, vec_cross.a) self.assertAlmostEqual(2072.294, vec_cross.b) self.assertAlmostEqual(-1822.182, vec_cross.c) v0 = Vector.from_float(-1, -2, 3) v1 = Vector.from_float(4, 0, -8) vec_cross = v0.cross(v1) # print(vec_cross) self.assertAlmostEqual(16, vec_cross.a) self.assertAlmostEqual(4, vec_cross.b) self.assertAlmostEqual(8, vec_cross.c)
def distance_from_point4(l0: Point, l1: Point, p: Point): ''' :param l0: :param l1: :param p: :return: ''' vec_nv = Vector.create_from_points(l0, l1) plane0 = Plane.create_from_point_vector(vec_nv, p) plane1 = Plane.create_from_3points([l0, l1, p]) vec_nv2 = plane1.nv.cross(plane0.nv) plane2 = Plane.create_from_point_vector(vec_nv2, l0) dist = plane2.point_plane_distance(p) return dist
def create_from_points(p0: Point, p1: Point): a_vec = Vector.create_from_points(p0, p1) return Line(p0, a_vec)
def nv(self): return Vector(self._params[:3])