# Lines¶

class diofant.geometry.line.LinearEntity[source]

A base class for all linear entities (line, ray and segment) in a 2-dimensional Euclidean space.

p1
p2
coefficients
slope
points

Notes

This is an abstract class and is not meant to be instantiated.

angle_between(other)[source]

The angle formed between the two linear entities.

Parameters: self (LinearEntity) other (LinearEntity) angle (angle in radians)

Notes

From the dot product of vectors v1 and v2 it is known that:

dot(v1, v2) = |v1|*|v2|*cos(A)

where A is the angle formed between the two vectors. We can get the directional vectors of the two lines and readily find the angle between the two using the above formula.

Examples

>>> p1, p2, p3 = Point(0, 0), Point(0, 4), Point(2, 0)
>>> l1, l2 = Line(p1, p2), Line(p1, p3)
>>> l1.angle_between(l2)
pi/2

arbitrary_point(parameter='t')[source]

A parameterized point on the Line.

Parameters: parameter (str, optional) – The name of the parameter which will be used for the parametric point. The default value is ‘t’. When this parameter is 0, the first point used to define the line will be returned, and when it is 1 the second point will be returned. point (diofant.geometry.point.Point) ValueError – When parameter already appears in the Line’s definition.

Examples

>>> p1, p2 = Point(1, 0), Point(5, 3)
>>> l1 = Line(p1, p2)
>>> l1.arbitrary_point()
Point2D(4*t + 1, 3*t)

static are_concurrent(*lines)[source]

Is a sequence of linear entities concurrent?

Two or more linear entities are concurrent if they all intersect at a single point.

Parameters: lines (a sequence of linear entities.) True (if the set of linear entities are concurrent,) False (otherwise.)

Notes

Simply take the first two lines and find their intersection. If there is no intersection, then the first two lines were parallel and had no intersection so concurrency is impossible amongst the whole set. Otherwise, check to see if the intersection point of the first two lines is a member on the rest of the lines. If so, the lines are concurrent.

Examples

>>> p1, p2 = Point(0, 0), Point(3, 5)
>>> p3, p4 = Point(-2, -2), Point(0, 2)
>>> l1, l2, l3 = Line(p1, p2), Line(p1, p3), Line(p1, p4)
>>> Line.are_concurrent(l1, l2, l3)
True

>>> l4 = Line(p2, p3)
>>> Line.are_concurrent(l2, l3, l4)
False

coefficients

The coefficients ($$a$$, $$b$$, $$c$$) for $$ax + by + c = 0$$.

Examples

>>> p1, p2 = Point(0, 0), Point(5, 3)
>>> l = Line(p1, p2)
>>> l.coefficients
(-3, 5, 0)

>>> p3 = Point(x, y)
>>> l2 = Line(p1, p3)
>>> l2.coefficients
(-y, x, 0)

contains(other)[source]

Subclasses should implement this method and should return True if other is on the boundaries of self; False if not on the boundaries of self; None if a determination cannot be made.

intersection(o)[source]

The intersection with another geometrical entity.

Parameters: o (diofant.geometry.point.Point or LinearEntity) intersection (list of geometrical entities)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(7, 7)
>>> l1 = Line(p1, p2)
>>> l1.intersection(p3)
[Point2D(7, 7)]

>>> p4, p5 = Point(5, 0), Point(0, 3)
>>> l2 = Line(p4, p5)
>>> l1.intersection(l2)
[Point2D(15/8, 15/8)]

>>> p6, p7 = Point(0, 5), Point(2, 6)
>>> s1 = Segment(p6, p7)
>>> l1.intersection(s1)
[]

is_parallel(other)[source]

Are two linear entities parallel?

Parameters: self (LinearEntity) other (LinearEntity) True (if self and other are parallel,) False (otherwise.)

Examples

>>> p1, p2 = Point(0, 0), Point(1, 1)
>>> p3, p4 = Point(3, 4), Point(6, 7)
>>> l1, l2 = Line(p1, p2), Line(p3, p4)
>>> Line.is_parallel(l1, l2)
True

>>> p5 = Point(6, 6)
>>> l3 = Line(p3, p5)
>>> Line.is_parallel(l1, l3)
False

is_perpendicular(other)[source]

Are two linear entities perpendicular?

Parameters: self (LinearEntity) other (LinearEntity) True (if self and other are perpendicular,) False (otherwise.)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(-1, 1)
>>> l1, l2 = Line(p1, p2), Line(p1, p3)
>>> l1.is_perpendicular(l2)
True

>>> p4 = Point(5, 3)
>>> l3 = Line(p1, p4)
>>> l1.is_perpendicular(l3)
False

is_similar(other)[source]

Return True if self and other are contained in the same line.

Examples

>>> p1, p2, p3 = Point(0, 1), Point(3, 4), Point(2, 3)
>>> l1 = Line(p1, p2)
>>> l2 = Line(p1, p3)
>>> l1.is_similar(l2)
True

length

The length of the line.

Examples

>>> p1, p2 = Point(0, 0), Point(3, 5)
>>> l1 = Line(p1, p2)
>>> l1.length
oo

p1

The first defining point of a linear entity.

Examples

>>> p1, p2 = Point(0, 0), Point(5, 3)
>>> l = Line(p1, p2)
>>> l.p1
Point2D(0, 0)

p2

The second defining point of a linear entity.

Examples

>>> p1, p2 = Point(0, 0), Point(5, 3)
>>> l = Line(p1, p2)
>>> l.p2
Point2D(5, 3)

parallel_line(p)[source]

Create a new Line parallel to this linear entity which passes through the point $$p$$.

Parameters: p (diofant.geometry.point.Point) line (Line)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(2, 3), Point(-2, 2)
>>> l1 = Line(p1, p2)
>>> l2 = l1.parallel_line(p3)
>>> p3 in l2
True
>>> l1.is_parallel(l2)
True

perpendicular_line(p)[source]

Create a new Line perpendicular to this linear entity which passes through the point $$p$$.

Parameters: p (diofant.geometry.point.Point) line (Line)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(2, 3), Point(-2, 2)
>>> l1 = Line(p1, p2)
>>> l2 = l1.perpendicular_line(p3)
>>> p3 in l2
True
>>> l1.is_perpendicular(l2)
True

perpendicular_segment(p)[source]

Create a perpendicular line segment from $$p$$ to this line.

The enpoints of the segment are p and the closest point in the line containing self. (If self is not a line, the point might not be in self.)

Parameters: p (diofant.geometry.point.Point) segment (Segment)

Notes

Returns $$p$$ itself if $$p$$ is on this linear entity.

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(0, 2)
>>> l1 = Line(p1, p2)
>>> s1 = l1.perpendicular_segment(p3)
>>> l1.is_perpendicular(s1)
True
>>> p3 in s1
True
>>> l1.perpendicular_segment(Point(4, 0))
Segment(Point2D(2, 2), Point2D(4, 0))

points

The two points used to define this linear entity.

Returns: points (tuple of Points)

Examples

>>> p1, p2 = Point(0, 0), Point(5, 11)
>>> l1 = Line(p1, p2)
>>> l1.points
(Point2D(0, 0), Point2D(5, 11))

projection(o)[source]

Project a point, line, ray, or segment onto this linear entity.

Parameters: other (diofant.geometry.point.Point or LinearEntity) projection (diofant.geometry.point.Point or LinearEntity) – The return type matches the type of the parameter other. diofant.geometry.exceptions.GeometryError – When method is unable to perform projection.

Notes

A projection involves taking the two points that define the linear entity and projecting those points onto a Line and then reforming the linear entity using these projections. A point P is projected onto a line L by finding the point on L that is closest to P. This point is the intersection of L and the line perpendicular to L that passes through P.

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(Rational(1, 2), 0)
>>> l1 = Line(p1, p2)
>>> l1.projection(p3)
Point2D(1/4, 1/4)

>>> p4, p5 = Point(10, 0), Point(12, 1)
>>> s1 = Segment(p4, p5)
>>> l1.projection(s1)
Segment(Point2D(5, 5), Point2D(13/2, 13/2))

random_point()[source]

A random point on a LinearEntity.

Returns: point (diofant.geometry.point.Point)

Examples

>>> p1, p2 = Point(0, 0), Point(5, 3)
>>> l1 = Line(p1, p2)
>>> p3 = l1.random_point()
>>> # random point - don't know its coords in advance
>>> p3 # doctest: +ELLIPSIS
Point2D(...)
>>> # point should belong to the line
>>> p3 in l1
True

slope

The slope of this linear entity, or infinity if vertical.

Returns: slope (Expr)

Examples

>>> p1, p2 = Point(0, 0), Point(3, 5)
>>> l1 = Line(p1, p2)
>>> l1.slope
5/3

>>> p3 = Point(0, 4)
>>> l2 = Line(p1, p3)
>>> l2.slope
oo

class diofant.geometry.line.Line[source]

An infinite line in space.

A line is declared with two distinct points or a point and slope as defined using keyword $$slope$$.

Notes

At the moment only lines in a 2D space can be declared, because Points can be defined only for 2D spaces.

Parameters: p1 (diofant.geometry.point.Point) pt (diofant.geometry.point.Point) slope (Expr)

Examples

>>> from diofant.abc import L
>>> L = Line(Point(2, 3), Point(3, 5))
>>> L
Line(Point2D(2, 3), Point2D(3, 5))
>>> L.points
(Point2D(2, 3), Point2D(3, 5))
>>> L.equation()
-2*x + y + 1
>>> L.coefficients
(-2, 1, 1)


Instantiate with keyword slope:

>>> Line(Point(0, 0), slope=0)
Line(Point2D(0, 0), Point2D(1, 0))


Instantiate with another linear object

>>> s = Segment((0, 0), (0, 1))
>>> Line(s).equation()
x

contains(o)[source]

Return True if o is on this Line, or False otherwise.

Examples

>>> p1, p2 = Point(0, 1), Point(3, 4)
>>> l = Line(p1, p2)
>>> l.contains(p1)
True
>>> l.contains((0, 1))
True
>>> l.contains((0, 0))
False

distance(o)[source]

Finds the shortest distance between a line and a point.

Raises: NotImplementedError is raised if o is not a Point

Examples

>>> p1, p2 = Point(0, 0), Point(1, 1)
>>> s = Line(p1, p2)
>>> s.distance(Point(-1, 1))
sqrt(2)
>>> s.distance((-1, 2))
3*sqrt(2)/2

equal(other)[source]

Returns True if self and other are the same mathematical entities

equation(x='x', y='y')[source]

The equation of the line: ax + by + c.

Parameters: x (str, optional) – The name to use for the x-axis, default value is ‘x’. y (str, optional) – The name to use for the y-axis, default value is ‘y’. equation (diofant expression)

Examples

>>> p1, p2 = Point(1, 0), Point(5, 3)
>>> l1 = Line(p1, p2)
>>> l1.equation()
-3*x + 4*y + 3

plot_interval(parameter='t')[source]

The plot interval for the default geometric plot of line. Gives values that will produce a line that is +/- 5 units long (where a unit is the distance between the two points that define the line).

Parameters: parameter (str, optional) – Default value is ‘t’. plot_interval (list (plot interval)) – [parameter, lower_bound, upper_bound]

Examples

>>> p1, p2 = Point(0, 0), Point(5, 3)
>>> l1 = Line(p1, p2)
>>> l1.plot_interval()
[t, -5, 5]

class diofant.geometry.line.Ray[source]

A Ray is a semi-line in the space with a source point and a direction.

Parameters: p1 (diofant.geometry.point.Point) – The source of the Ray p2 (diofant.geometry.point.Point or Expr) – This point determines the direction in which the Ray propagates. If given as an angle it is interpreted in radians with the positive direction being ccw.
source
xdirection
ydirection

Notes

At the moment only rays in a 2D space can be declared, because Points can be defined only for 2D spaces.

Examples

>>> from diofant.abc import r
>>> r = Ray(Point(2, 3), Point(3, 5))
>>> r = Ray(Point(2, 3), Point(3, 5))
>>> r
Ray(Point2D(2, 3), Point2D(3, 5))
>>> r.points
(Point2D(2, 3), Point2D(3, 5))
>>> r.source
Point2D(2, 3)
>>> r.xdirection
oo
>>> r.ydirection
oo
>>> r.slope
2
>>> Ray(Point(0, 0), angle=pi/4).slope
1

contains(o)[source]

Is other GeometryEntity contained in this Ray?

Examples

>>> p1, p2 = Point(0, 0), Point(4, 4)
>>> r = Ray(p1, p2)
>>> r.contains(p1)
True
>>> r.contains((1, 1))
True
>>> r.contains((1, 3))
False
>>> s = Segment((1, 1), (2, 2))
>>> r.contains(s)
True
>>> s = Segment((1, 2), (2, 5))
>>> r.contains(s)
False
>>> r1 = Ray((2, 2), (3, 3))
>>> r.contains(r1)
True
>>> r1 = Ray((2, 2), (3, 5))
>>> r.contains(r1)
False

direction

The direction in which the ray emanates.

Examples

>>> p1, p2 = Point(0, 0), Point(4, 1)
>>> r1 = Ray(p1, p2)
>>> r1.direction
Point2D(4, 1)

distance(o)[source]

Finds the shortest distance between the ray and a point.

Raises: NotImplementedError is raised if o is not a Point

Examples

>>> p1, p2 = Point(0, 0), Point(1, 1)
>>> s = Ray(p1, p2)
>>> s.distance(Point(-1, -1))
sqrt(2)
>>> s.distance((-1, 2))
3*sqrt(2)/2

equals(other)[source]

Returns True if self and other are the same mathematical entities

plot_interval(parameter='t')[source]

The plot interval for the default geometric plot of the Ray. Gives values that will produce a ray that is 10 units long (where a unit is the distance between the two points that define the ray).

Parameters: parameter (str, optional) – Default value is ‘t’. plot_interval (list) – [parameter, lower_bound, upper_bound]

Examples

>>> r = Ray((0, 0), angle=pi/4)
>>> r.plot_interval()
[t, 0, 10]

source

The point from which the ray emanates.

Examples

>>> p1, p2 = Point(0, 0), Point(4, 1)
>>> r1 = Ray(p1, p2)
>>> r1.source
Point2D(0, 0)

xdirection

The x direction of the ray.

Positive infinity if the ray points in the positive x direction, negative infinity if the ray points in the negative x direction, or 0 if the ray is vertical.

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(0, -1)
>>> r1, r2 = Ray(p1, p2), Ray(p1, p3)
>>> r1.xdirection
oo
>>> r2.xdirection
0

ydirection

The y direction of the ray.

Positive infinity if the ray points in the positive y direction, negative infinity if the ray points in the negative y direction, or 0 if the ray is horizontal.

Examples

>>> p1, p2, p3 = Point(0, 0), Point(-1, -1), Point(-1, 0)
>>> r1, r2 = Ray(p1, p2), Ray(p1, p3)
>>> r1.ydirection
-oo
>>> r2.ydirection
0

class diofant.geometry.line.Segment[source]

A undirected line segment in space.

Parameters: p1 (diofant.geometry.point.Point) p2 (diofant.geometry.point.Point)
length
Type: Expr
midpoint

Notes

At the moment only segments in a 2D space can be declared, because Points can be defined only for 2D spaces.

Examples

>>> from diofant.abc import s
>>> Segment((1, 0), (1, 1)) # tuples are interpreted as pts
Segment(Point2D(1, 0), Point2D(1, 1))
>>> s = Segment(Point(4, 3), Point(1, 1))
>>> s
Segment(Point2D(1, 1), Point2D(4, 3))
>>> s.points
(Point2D(1, 1), Point2D(4, 3))
>>> s.slope
2/3
>>> s.length
sqrt(13)
>>> s.midpoint
Point2D(5/2, 2)

contains(other)[source]

Is the other GeometryEntity contained within this Segment?

Examples

>>> p1, p2 = Point(0, 1), Point(3, 4)
>>> s = Segment(p1, p2)
>>> s2 = Segment(p2, p1)
>>> s.contains(s2)
True

distance(o)[source]

Finds the shortest distance between a line segment and a point.

Raises: NotImplementedError is raised if o is not a Point

Examples

>>> p1, p2 = Point(0, 1), Point(3, 4)
>>> s = Segment(p1, p2)
>>> s.distance(Point(10, 15))
sqrt(170)
>>> s.distance((0, 12))
sqrt(73)

length

The length of the line segment.

Examples

>>> p1, p2 = Point(0, 0), Point(4, 3)
>>> s1 = Segment(p1, p2)
>>> s1.length
5

midpoint

The midpoint of the line segment.

Examples

>>> p1, p2 = Point(0, 0), Point(4, 3)
>>> s1 = Segment(p1, p2)
>>> s1.midpoint
Point2D(2, 3/2)

perpendicular_bisector(p=None)[source]

The perpendicular bisector of this segment.

If no point is specified or the point specified is not on the bisector then the bisector is returned as a Line. Otherwise a Segment is returned that joins the point specified and the intersection of the bisector and the segment.

Parameters: p (diofant.geometry.point.Point) bisector (Line or Segment)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(6, 6), Point(5, 1)
>>> s1 = Segment(p1, p2)
>>> s1.perpendicular_bisector()
Line(Point2D(3, 3), Point2D(9, -3))

>>> s1.perpendicular_bisector(p3)
Segment(Point2D(3, 3), Point2D(5, 1))

plot_interval(parameter='t')[source]

The plot interval for the default geometric plot of the Segment gives values that will produce the full segment in a plot.

Parameters: parameter (str, optional) – Default value is ‘t’. plot_interval (list) – [parameter, lower_bound, upper_bound]

Examples

>>> p1, p2 = Point(0, 0), Point(5, 3)
>>> s1 = Segment(p1, p2)
>>> s1.plot_interval()
[t, 0, 1]