Skip to content

Commit

Permalink
Merge branch 'change_vector_class'
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hynes committed Mar 11, 2019
2 parents feb8aee + 3259d85 commit 7d06fa9
Show file tree
Hide file tree
Showing 34 changed files with 1,011 additions and 1,059 deletions.
22 changes: 10 additions & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,36 @@ Measure the angle between two vectors.
>>> import numpy as np
>>> from skspatial.objects import Vector

>>> vector_a = Vector([1, 0])
>>> vector_b = Vector([1, 1])
>>> vector = Vector([1, 0])
>>> angle = vector.angle_between([1, 1])

>>> angle = vector_a.angle_between(vector_b)
>>> np.degrees(angle).round()
45.0


Project a point onto a line.

>>> from skspatial.objects import Point, Line
>>> from skspatial.objects import Line

>>> line = Line(Point([0, 0]), Vector([1, 1]))
>>> point = Point([5, 6, 7])
>>> line = Line(point=[0, 0], vector=[1, 1])

>>> line.project_point(point)
Point([5.5 5.5 0. ])
>>> line.project_point([5, 6, 7])
Point([5.5, 5.5, 0. ])


An error is returned if the computation is undefined.

>>> line_a = Line(Point([0, 0]), Vector([1, 0]))
>>> line_b = Line(Point([1, 0]), Vector([1, 0]))
>>> line_a = Line([0, 0], [1, 0])
>>> line_b = Line([1, 0], [1, 0])

>>> line_a.intersect_line(line_b)
Traceback (most recent call last):
...
dpcontracts.PreconditionError: The lines must not be parallel.


Credits
-------
Acknowledgment
--------------

This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.

Expand Down
28 changes: 14 additions & 14 deletions doc/source/computations/intersection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ Line-Line intersection

The intersection of a `Line` with a `Line` is a `Point`.

>>> from skspatial.objects import Point, Vector, Line
>>> from skspatial.objects import Line

>>> line_a = Line(Point([0, 0]), Vector([1, 1]))
>>> line_b = Line(Point([10, 0]), Vector([0, 1]))
>>> line_a = Line(point=[0, 0], vector=[1, 1])
>>> line_b = Line([10, 0], [0, 1])

>>> line_a.intersect_line(line_b)
Point([10. 10. 0.])
Point([10., 10., 0.])


In order to intersect, the lines must be coplanar and not parallel. An error is returned otherwise.

>>> line_b = Line(Point([10, 0]), Vector([1, 1, 1]))
>>> line_b = Line([10, 0], [1, 1, 1])
>>> line_a.intersect_line(line_b)
Traceback (most recent call last):
...
dpcontracts.PreconditionError: The lines must be coplanar.

>>> line_b = Line(Point([10, 0]), Vector([1, 1]))
>>> line_b = Line([10, 0], [1, 1])
>>> line_a.intersect_line(line_b)
Traceback (most recent call last):
...
Expand All @@ -39,16 +39,16 @@ The intersection of a `Line` with a `Plane` is a `Point`.

>>> from skspatial.objects import Plane

>>> line = Line(Point([5, 5, 3]), Vector([0, 0, -1]))
>>> plane = Plane(Point([0, 0, 0]), Vector([0, 0, 1]))
>>> line = Line([5, 5, 3], [0, 0, -1])
>>> plane = Plane([0, 0, 0], [0, 0, 1])

>>> plane.intersect_line(line)
Point([5. 5. 0.])
Point([5., 5., 0.])


The line must not be parallel to the plane.

>>> line = Line(Point([5, 5, 3]), Vector([0, 1, 0]))
>>> line = Line([5, 5, 3], [0, 1, 0])
>>> plane.intersect_line(line)
Traceback (most recent call last):
...
Expand All @@ -61,16 +61,16 @@ Plane-Plane intersection

The intersection of a `Plane` with a `Plane` is a `Line`.

>>> plane_a = Plane(Point([0, 0, 0]), Vector([-1, 1, 0]))
>>> plane_b = Plane(Point([8, 0, 0]), Vector([1, 1, 0]))
>>> plane_a = Plane([0, 0, 0], [-1, 1, 0])
>>> plane_b = Plane([8, 0, 0], [1, 1, 0])

>>> plane_a.intersect_plane(plane_b)
Line(point=Point([4. 4. 0.]), direction=Vector([ 0. 0. -1.]))
Line(point=Point([4., 4., 0.]), direction=Vector([ 0., 0., -1.]))


The planes must not be parallel.

>>> plane_b = Plane(Point([8, 0, 0]), Vector([-1, 1, 0]))
>>> plane_b = Plane([8, 0, 0], [-1, 1, 0])
>>> plane_a.intersect_plane(plane_b)
Traceback (most recent call last):
...
Expand Down
53 changes: 25 additions & 28 deletions doc/source/computations/projection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@
Projection
==========

Vector-Vector Projection
------------------------

Project a vector onto a vector.

>>> from skspatial.objects import Vector

>>> vector_a = Vector([1, 0])

>>> vector_a.project([22, 9]) # Project vector B onto vector A.
Vector([22., 0., 0.])


Point-Line Projection
---------------------

Project a point onto a line.

>>> from skspatial.objects import Point, Vector, Line
>>> from skspatial.objects import Line

>>> point = Point([5, 5, 3])
>>> line = Line(Point([0, 0]), Vector([1, 1]))
>>> line = Line(point=[0, 0], vector=[1, 1])

>>> line.project_point(point)
Point([5. 5. 0.])
>>> line.project_point([5, 5, 3])
Point([5., 5., 0.])


Point-Plane Projection
Expand All @@ -24,44 +35,30 @@ Project a point onto a plane.

>>> from skspatial.objects import Plane

>>> point = Point([5, 9, -3])
>>> plane = Plane(Point([0, 0, 0]), Vector([0, 0, 2]))
>>> plane = Plane(point=[0, 0, 0], vector=[0, 0, 2])

>>> plane.project_point(point)
Point([5. 9. 0.])


Vector-Vector Projection
------------------------

Project a vector onto a vector.

>>> vector_a = Vector([1, 0])
>>> vector_b = Vector([22, 9])
>>> plane.project_point([5, 9, -3])
Point([5., 9., 0.])

>>> vector_a.project_vector(vector_b) # Project vector B onto vector A.
Vector([22. 0. 0.])


Vector-Line Projection
----------------------

Project a vector onto a line.

>>> line = Line(Point([-1, 5, 3]), Vector([3, 4, 5]))
>>> vector = Vector([1, 1, 1])
>>> line = Line([-1, 5, 3], [3, 4, 5])

>>> line.project_vector(vector)
Vector([0.72 0.96 1.2 ])
>>> line.project_vector([1, 1, 1])
Vector([0.72, 0.96, 1.2 ])


Vector-Plane Projection
-----------------------

Project a vector onto a plane.

>>> plane = Plane(Point([0, 4]), Vector([0, 1, 1]))
>>> vector = Vector([2, 4, 8])
>>> plane = Plane([0, 4], [0, 1, 1])

>>> plane.project_vector(vector)
Vector([ 2. -2. 2.])
>>> plane.project_vector([2, 4, 8])
Vector([ 2., -2., 2.])
33 changes: 18 additions & 15 deletions doc/source/objects/line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,41 @@
Line
----

A `Line` is defined by a `Point` and a `Vector`. The direction of the line is the unit vector of the input `Vector`.
A line is defined by a point and a direction vector. The direction of the line is the unit vector of the input vector.

>>> from skspatial.objects import Point, Vector, Line
>>> from skspatial.objects import Line

>>> line_1 = Line(Point([0, 0]), Vector([5, 0]))
>>> line_1 = Line(point=[0, 0], vector=[5, 0])

>>> line_1
Line(point=Point([0. 0. 0.]), direction=Vector([1. 0. 0.]))
Line(point=Point([0., 0., 0.]), direction=Vector([1., 0., 0.]))


The `Point` and `Vector` inputs are not interchangeable.
Alternatively, a `Line` can be defined by two points.

>>> Line(Vector([0, 0]), Point([5, 0]))
Traceback (most recent call last):
...
dpcontracts.PreconditionError: the types of arguments must be valid
>>> line_2 = Line.from_points([0, 0], [100, 0])

>>> line_1.is_close(line_2)
True

Alternatively, a `Line` can be defined by two points.

>>> line_2 = Line.from_points(Point([0, 0]), Point([100, 0]))
The `is_close` method checks if two lines are equal within a tolerance.

Lines with different points and directions can still be equal. One line must contain the other line's point, and their vectors must be parallel.

>>> line_1 = Line([0, 0], [1, 0])
>>> line_2 = Line([10, 0], [-5, 0])

>>> line_1 == line_2
>>> line_1.is_close(line_2)
True

The distance from a `Point` to a `Line` can be found.

>>> line_1.distance_point(Point([20, 75]))
>>> line_1.distance_point([20, 75])
75.0

A `Point` can be projected onto a `Line`, returning a new `Point`.

>>> line_1.project_point(Point([50, 20]))
Point([50. 0. 0.])
>>> line_1.project_point([50, 20])
Point([50., 0., 0.])

28 changes: 11 additions & 17 deletions doc/source/objects/plane.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,31 @@
Plane
-----

A `Plane` is defined by a `Point` and a `Vector`. The normal vector of the plane is the unit vector of the input `Vector`.
A plane is defined by a point and a normal vector. The normal vector of the plane is the unit vector of the input vector.

>>> from skspatial.objects import Point, Vector, Plane
>>> from skspatial.objects import Plane

>>> plane_1 = Plane(Point([0, 0]), Vector([0, 0, 23]))
>>> plane_1 = Plane(point=[0, 0], vector=[0, 0, 23])

>>> plane_1
Plane(point=Point([0. 0. 0.]), normal=Vector([0. 0. 1.]))
Plane(point=Point([0., 0., 0.]), normal=Vector([0., 0., 1.]))

Alternatively, a plane can be defined by three points.

>>> point_a, point_b, point_c = Point([0, 0]), Point([10, -2]), Point([50, 500])
>>> point_a, point_b, point_c = [0, 0], [10, -2], [50, 500]
>>> plane_2 = Plane.from_points(point_a, point_b, point_c)

>>> plane_1 == plane_2
>>> plane_1.is_close(plane_2)
True

However, changing the order of the points can reverse the direction of the normal vector.
Changing the order of the points can reverse the direction of the normal vector.

>>> plane_3 = Plane.from_points(point_a, point_c, point_b)

>>> plane_3
Plane(point=Point([0. 0. 0.]), normal=Vector([ 0. 0. -1.]))
Plane(point=Point([0., 0., 0.]), normal=Vector([ 0., 0., -1.]))

>>> plane_1 == plane_3
False

Again, a `Point` and a `Vector` are not interchangeable.

>>> Plane.from_points(point_a, point_b, Vector([50, 500]))
Traceback (most recent call last):
...
dpcontracts.PreconditionError: the types of arguments must be valid
The planes will still be equal.

>>> plane_1.is_close(plane_3)
True

0 comments on commit 7d06fa9

Please sign in to comment.