def test_two_vectors(arrays): array_a, array_b = arrays vector_a = Vector(array_a) is_perpendicular = vector_a.is_perpendicular(array_b) is_parallel = vector_a.is_parallel(array_b) # Two non-zero vectors cannot be both perpendicular and parallel. assert not (is_perpendicular and is_parallel) angle = vector_a.angle_between(array_b) if is_perpendicular: assert math.isclose(angle, np.pi / 2) if is_parallel: assert math.isclose(angle, 0, abs_tol=ATOL) or math.isclose( angle, np.pi, rel_tol=1e-6) # The zero vector is perpendicular and parallel to any other vector. vector_zero = np.zeros(vector_a.size) assert vector_a.is_perpendicular(vector_zero) assert vector_a.is_parallel(vector_zero) # The angle with the zero vector is undefined. message_expected = "The vectors must have non-zero magnitudes." with pytest.raises(ValueError, match=message_expected): vector_a.angle_between(vector_zero) # The projection of vector B onto A is parallel to A. vector_b_projected = vector_a.project_vector(array_b) assert vector_a.is_parallel(vector_b_projected) # The projection is zero if vectors A and B are perpendicular. if is_perpendicular: assert vector_b_projected.is_zero(abs_tol=ATOL)
""" 2D Vector-Vector Projection =========================== Project a vector onto another vector. """ from skspatial.objects import Vector from skspatial.plotting import plot_2d vector_a = Vector([1, 1]) vector_b = Vector([2, 0]) vector_projected = vector_b.project_vector(vector_a) _, ax = plot_2d( vector_a.plotter(color='k', head_width=0.1), vector_b.plotter(color='k', head_width=0.1), vector_projected.plotter(color='r', head_width=0.1), ) ax.axis([-0.5, 2.5, -0.5, 1.5])