Geometry Module¶
Introduction¶
The geometry module for SymPy allows one to create two-dimensional geometrical entities, such as lines and circles, and query for information about these entities. This could include asking the area of an ellipse, checking for collinearity of a set of points, or finding the intersection between two lines. The primary use case of the module involves entities with numerical values, but it is possible to also use symbolic representations.
Available Entities¶
The following entities are currently available in the geometry module:
Point
Line
,Ray
,Segment
Ellipse
,Circle
Polygon
,RegularPolygon
,Triangle
Most of the work one will do will be through the properties and methods of these entities, but several global methods exist:
intersection(entity1, entity2)
are_similar(entity1, entity2)
convex_hull(points)
For a full API listing and an explanation of the methods and their return values please see the list of classes at the end of this document.
Example Usage¶
The following Python session gives one an idea of how to work with some of the geometry module.
>>> from sympy import *
>>> from sympy.geometry import *
>>> x = Point(0, 0)
>>> y = Point(1, 1)
>>> z = Point(2, 2)
>>> zp = Point(1, 0)
>>> Point.is_collinear(x, y, z)
True
>>> Point.is_collinear(x, y, zp)
False
>>> t = Triangle(zp, y, x)
>>> t.area
1/2
>>> t.medians[x]
Segment(Point(0, 0), Point(1, 1/2))
>>> Segment(Point(1, S(1)/2), Point(0, 0))
Segment(Point(0, 0), Point(1, 1/2))
>>> m = t.medians
>>> intersection(m[x], m[y], m[zp])
[Point(2/3, 1/3)]
>>> c = Circle(x, 5)
>>> l = Line(Point(5, -5), Point(5, 5))
>>> c.is_tangent(l) # is l tangent to c?
True
>>> l = Line(x, y)
>>> c.is_tangent(l) # is l tangent to c?
False
>>> intersection(c, l)
[Point(-5*sqrt(2)/2, -5*sqrt(2)/2), Point(5*sqrt(2)/2, 5*sqrt(2)/2)]
Intersection of medians¶
>>> from sympy import symbols
>>> from sympy.geometry import Point, Triangle, intersection
>>> a, b = symbols("a,b", positive=True)
>>> x = Point(0, 0)
>>> y = Point(a, 0)
>>> z = Point(2*a, b)
>>> t = Triangle(x, y, z)
>>> t.area
a*b/2
>>> t.medians[x]
Segment(Point(0, 0), Point(3*a/2, b/2))
>>> intersection(t.medians[x], t.medians[y], t.medians[z])
[Point(a, b/3)]
An in-depth example: Pappus’ Hexagon Theorem¶
From Wikipedia ([WikiPappus]):
Given one set of collinear points \(A\), \(B\), \(C\), and another set of collinear points \(a\), \(b\), \(c\), then the intersection points \(X\), \(Y\), \(Z\) of line pairs \(Ab\) and \(aB\), \(Ac\) and \(aC\), \(Bc\) and \(bC\) are collinear.
>>> from sympy import *
>>> from sympy.geometry import *
>>>
>>> l1 = Line(Point(0, 0), Point(5, 6))
>>> l2 = Line(Point(0, 0), Point(2, -2))
>>>
>>> def subs_point(l, val):
... """Take an arbitrary point and make it a fixed point."""
... t = Symbol('t', real=True)
... ap = l.arbitrary_point()
... return Point(ap.x.subs(t, val), ap.y.subs(t, val))
...
>>> p11 = subs_point(l1, 5)
>>> p12 = subs_point(l1, 6)
>>> p13 = subs_point(l1, 11)
>>>
>>> p21 = subs_point(l2, -1)
>>> p22 = subs_point(l2, 2)
>>> p23 = subs_point(l2, 13)
>>>
>>> ll1 = Line(p11, p22)
>>> ll2 = Line(p11, p23)
>>> ll3 = Line(p12, p21)
>>> ll4 = Line(p12, p23)
>>> ll5 = Line(p13, p21)
>>> ll6 = Line(p13, p22)
>>>
>>> pp1 = intersection(ll1, ll3)[0]
>>> pp2 = intersection(ll2, ll5)[0]
>>> pp3 = intersection(ll4, ll6)[0]
>>>
>>> Point.is_collinear(pp1, pp2, pp3)
True
References¶
[WikiPappus] | “Pappus’s Hexagon Theorem” Wikipedia, the Free Encyclopedia. Web. 26 Apr. 2013. <http://en.wikipedia.org/wiki/Pappus’s_hexagon_theorem> |
Miscellaneous Notes¶
- The area property of
Polygon
andTriangle
may return a positive or negative value, depending on whether or not the points are oriented counter-clockwise or clockwise, respectively. If you always want a positive value be sure to use theabs
function. - Although
Polygon
can refer to any type of polygon, the code has been written for simple polygons. Hence, expect potential problems if dealing with complex polygons (overlapping sides). - Since SymPy is still in its infancy some things may not simplify
properly and hence some things that should return
True
(e.g.,Point.is_collinear
) may not actually do so. Similarly, attempting to find the intersection of entities that do intersect may result in an empty result.
Future Work¶
Truth Setting Expressions¶
When one deals with symbolic entities, it often happens that an assertion cannot be guaranteed. For example, consider the following code:
>>> from sympy import *
>>> from sympy.geometry import *
>>> x,y,z = map(Symbol, 'xyz')
>>> p1,p2,p3 = Point(x, y), Point(y, z), Point(2*x*y, y)
>>> Point.is_collinear(p1, p2, p3)
False
Even though the result is currently False
, this is not always true. If the
quantity \(z - y - 2*y*z + 2*y**2 == 0\) then the points will be collinear. It
would be really nice to inform the user of this because such a quantity may be
useful to a user for further calculation and, at the very least, being nice to
know. This could be potentially done by returning an object (e.g.,
GeometryResult) that the user could use. This actually would not involve an
extensive amount of work.
Three Dimensions and Beyond¶
Currently there are no plans for extending the module to three dimensions, but it certainly would be a good addition. This would probably involve a fair amount of work since many of the algorithms used are specific to two dimensions.
Geometry Visualization¶
The plotting module is capable of plotting geometric entities. See Plotting Geometric Entities in the plotting module entry.
API Reference¶
Entities¶
-
class
sympy.geometry.entity.
GeometryEntity
[source]¶ The base class for all geometrical entities.
This class doesn’t represent any particular geometric entity, it only provides the implementation of some methods common to all subclasses.
-
encloses
(o)[source]¶ Return True if o is inside (not on or outside) the boundaries of self.
The object will be decomposed into Points and individual Entities need only define an encloses_point method for their class.
See also
sympy.geometry.ellipse.Ellipse.encloses_point
,sympy.geometry.polygon.Polygon.encloses_point
Examples
>>> from sympy import RegularPolygon, Point, Polygon >>> t = Polygon(*RegularPolygon(Point(0, 0), 1, 3).vertices) >>> t2 = Polygon(*RegularPolygon(Point(0, 0), 2, 3).vertices) >>> t2.encloses(t) True >>> t.encloses(t2) False
-
intersection
(o)[source]¶ Returns a list of all of the intersections of self with o.
See also
Notes
An entity is not required to implement this method.
If two different types of entities can intersect, the item with higher index in ordering_of_classes should implement intersections with anything having a lower index.
-
is_similar
(other)[source]¶ Is this geometrical entity similar to another geometrical entity?
Two entities are similar if a uniform scaling (enlarging or shrinking) of one of the entities will allow one to obtain the other.
See also
Notes
This method is not intended to be used directly but rather through the \(are_similar\) function found in util.py. An entity is not required to implement this method. If two different types of entities can be similar, it is only required that one of them be able to determine this.
-
rotate
(angle, pt=None)[source]¶ Rotate
angle
radians counterclockwise about Pointpt
.The default pt is the origin, Point(0, 0)
Examples
>>> from sympy import Point, RegularPolygon, Polygon, pi >>> t = Polygon(*RegularPolygon(Point(0, 0), 1, 3).vertices) >>> t # vertex on x axis Triangle(Point(1, 0), Point(-1/2, sqrt(3)/2), Point(-1/2, -sqrt(3)/2)) >>> t.rotate(pi/2) # vertex on y axis now Triangle(Point(0, 1), Point(-sqrt(3)/2, -1/2), Point(sqrt(3)/2, -1/2))
-
scale
(x=1, y=1, pt=None)[source]¶ Scale the object by multiplying the x,y-coordinates by x and y.
If pt is given, the scaling is done relative to that point; the object is shifted by -pt, scaled, and shifted by pt.
Examples
>>> from sympy import RegularPolygon, Point, Polygon >>> t = Polygon(*RegularPolygon(Point(0, 0), 1, 3).vertices) >>> t Triangle(Point(1, 0), Point(-1/2, sqrt(3)/2), Point(-1/2, -sqrt(3)/2)) >>> t.scale(2) Triangle(Point(2, 0), Point(-1, sqrt(3)/2), Point(-1, -sqrt(3)/2)) >>> t.scale(2,2) Triangle(Point(2, 0), Point(-1, sqrt(3)), Point(-1, -sqrt(3)))
-
translate
(x=0, y=0)[source]¶ Shift the object by adding to the x,y-coordinates the values x and y.
Examples
>>> from sympy import RegularPolygon, Point, Polygon >>> t = Polygon(*RegularPolygon(Point(0, 0), 1, 3).vertices) >>> t Triangle(Point(1, 0), Point(-1/2, sqrt(3)/2), Point(-1/2, -sqrt(3)/2)) >>> t.translate(2) Triangle(Point(3, 0), Point(3/2, sqrt(3)/2), Point(3/2, -sqrt(3)/2)) >>> t.translate(2, 2) Triangle(Point(3, 2), Point(3/2, sqrt(3)/2 + 2), Point(3/2, -sqrt(3)/2 + 2))
-
Utils¶
-
sympy.geometry.util.
intersection
(*entities)[source]¶ The intersection of a collection of GeometryEntity instances.
Parameters: entities : sequence of GeometryEntity
Returns: intersection : list of GeometryEntity
Raises: NotImplementedError :
When unable to calculate intersection.
Notes
The intersection of any geometrical entity with itself should return a list with one item: the entity in question. An intersection requires two or more entities. If only a single entity is given then the function will return an empty list. It is possible for \(intersection\) to miss intersections that one knows exists because the required quantities were not fully simplified internally. Reals should be converted to Rationals, e.g. Rational(str(real_num)) or else failures due to floating point issues may result.
Examples
>>> from sympy.geometry import Point, Line, Circle, intersection >>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(-1, 5) >>> l1, l2 = Line(p1, p2), Line(p3, p2) >>> c = Circle(p2, 1) >>> intersection(l1, p2) [Point(1, 1)] >>> intersection(l1, l2) [Point(1, 1)] >>> intersection(c, p2) [] >>> intersection(c, Point(1, 0)) [Point(1, 0)] >>> intersection(c, l2) [Point(-sqrt(5)/5 + 1, 2*sqrt(5)/5 + 1), Point(sqrt(5)/5 + 1, -2*sqrt(5)/5 + 1)]
-
sympy.geometry.util.
convex_hull
(*args)[source]¶ The convex hull surrounding the Points contained in the list of entities.
Parameters: args : a collection of Points, Segments and/or Polygons Returns: convex_hull : Polygon Notes
This can only be performed on a set of non-symbolic points.
References
[1] http://en.wikipedia.org/wiki/Graham_scan
[2] Andrew’s Monotone Chain Algorithm (A.M. Andrew, “Another Efficient Algorithm for Convex Hulls in Two Dimensions”, 1979) http://softsurfer.com/Archive/algorithm_0109/algorithm_0109.htm
Examples
>>> from sympy.geometry import Point, convex_hull >>> points = [(1,1), (1,2), (3,1), (-5,2), (15,4)] >>> convex_hull(*points) Polygon(Point(-5, 2), Point(1, 1), Point(3, 1), Point(15, 4))
-
sympy.geometry.util.
are_similar
(e1, e2)[source]¶ Are two geometrical entities similar.
Can one geometrical entity be uniformly scaled to the other?
Parameters: e1 : GeometryEntity
e2 : GeometryEntity
Returns: are_similar : boolean
Raises: GeometryError :
When \(e1\) and \(e2\) cannot be compared.
Notes
If the two objects are equal then they are similar.
Examples
>>> from sympy import Point, Circle, Triangle, are_similar >>> c1, c2 = Circle(Point(0, 0), 4), Circle(Point(1, 4), 3) >>> t1 = Triangle(Point(0, 0), Point(1, 0), Point(0, 1)) >>> t2 = Triangle(Point(0, 0), Point(2, 0), Point(0, 2)) >>> t3 = Triangle(Point(0, 0), Point(3, 0), Point(0, 1)) >>> are_similar(t1, t2) True >>> are_similar(t1, t3) False
-
sympy.geometry.util.
centroid
(*args)[source]¶ Find the centroid (center of mass) of the collection containing only Points, Segments or Polygons. The centroid is the weighted average of the individual centroid where the weights are the lengths (of segments) or areas (of polygons). Overlapping regions will add to the weight of that region.
If there are no objects (or a mixture of objects) then None is returned.
Examples
>>> from sympy import Point, Segment, Polygon >>> from sympy.geometry.util import centroid >>> p = Polygon((0, 0), (10, 0), (10, 10)) >>> q = p.translate(0, 20) >>> p.centroid, q.centroid (Point(20/3, 10/3), Point(20/3, 70/3)) >>> centroid(p, q) Point(20/3, 40/3) >>> p, q = Segment((0, 0), (2, 0)), Segment((0, 0), (2, 2)) >>> centroid(p, q) Point(1, -sqrt(2) + 2) >>> centroid(Point(0, 0), Point(2, 0)) Point(1, 0)
Stacking 3 polygons on top of each other effectively triples the weight of that polygon:
>>> p = Polygon((0, 0), (1, 0), (1, 1), (0, 1)) >>> q = Polygon((1, 0), (3, 0), (3, 1), (1, 1)) >>> centroid(p, q) Point(3/2, 1/2) >>> centroid(p, p, p, q) # centroid x-coord shifts left Point(11/10, 1/2)
Stacking the squares vertically above and below p has the same effect:
>>> centroid(p, p.translate(0, 1), p.translate(0, -1), q) Point(11/10, 1/2)
Points¶
-
class
sympy.geometry.point.
Point
[source]¶ A point in a 2-dimensional Euclidean space.
Parameters: coords : sequence of 2 coordinate values.
Raises: NotImplementedError :
When trying to create a point with more than two dimensions. When \(intersection\) is called with object other than a Point.
TypeError :
When trying to add or subtract points with different dimensions.
See also
sympy.geometry.line.Segment
- Connects two Points
Notes
Currently only 2-dimensional points are supported.
Examples
>>> from sympy.geometry import Point >>> from sympy.abc import x >>> Point(1, 2) Point(1, 2) >>> Point([1, 2]) Point(1, 2) >>> Point(0, x) Point(0, x)
Floats are automatically converted to Rational unless the evaluate flag is False:
>>> Point(0.5, 0.25) Point(1/2, 1/4) >>> Point(0.5, 0.25, evaluate=False) Point(0.5, 0.25)
Attributes
x y length -
distance
(p)[source]¶ The Euclidean distance from self to point p.
Parameters: p : Point Returns: distance : number or symbolic expression. See also
Examples
>>> from sympy.geometry import Point >>> p1, p2 = Point(1, 1), Point(4, 5) >>> p1.distance(p2) 5
>>> from sympy.abc import x, y >>> p3 = Point(x, y) >>> p3.distance(Point(0, 0)) sqrt(x**2 + y**2)
-
evalf
(prec=None, **options)[source]¶ Evaluate the coordinates of the point.
This method will, where possible, create and return a new Point where the coordinates are evaluated as floating point numbers to the precision indicated (default=15).
Returns: point : Point Examples
>>> from sympy import Point, Rational >>> p1 = Point(Rational(1, 2), Rational(3, 2)) >>> p1 Point(1/2, 3/2) >>> p1.evalf() Point(0.5, 1.5)
-
intersection
(o)[source]¶ The intersection between this point and another point.
Parameters: other : Point Returns: intersection : list of Points Notes
The return value will either be an empty list if there is no intersection, otherwise it will contain this point.
Examples
>>> from sympy import Point >>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(0, 0) >>> p1.intersection(p2) [] >>> p1.intersection(p3) [Point(0, 0)]
-
is_collinear
(*points)[source]¶ Is a sequence of points collinear?
Test whether or not a set of points are collinear. Returns True if the set of points are collinear, or False otherwise.
Parameters: points : sequence of Point Returns: is_collinear : boolean See also
Notes
Slope is preserved everywhere on a line, so the slope between any two points on the line should be the same. Take the first two points, p1 and p2, and create a translated point v1 with p1 as the origin. Now for every other point we create a translated point, vi with p1 also as the origin. Note that these translations preserve slope since everything is consistently translated to a new origin of p1. Since slope is preserved then we have the following equality:
- v1_slope = vi_slope
- v1.y/v1.x = vi.y/vi.x (due to translation)
- v1.y*vi.x = vi.y*v1.x
- v1.y*vi.x - vi.y*v1.x = 0 (*)
Hence, if we have a vi such that the equality in (*) is False then the points are not collinear. We do this test for every point in the list, and if all pass then they are collinear.
Examples
>>> from sympy import Point >>> from sympy.abc import x >>> p1, p2 = Point(0, 0), Point(1, 1) >>> p3, p4, p5 = Point(2, 2), Point(x, x), Point(1, 2) >>> Point.is_collinear(p1, p2, p3, p4) True >>> Point.is_collinear(p1, p2, p3, p5) False
-
is_concyclic
(*points)[source]¶ Is a sequence of points concyclic?
Test whether or not a sequence of points are concyclic (i.e., they lie on a circle).
Parameters: points : sequence of Points
Returns: is_concyclic : boolean
True if points are concyclic, False otherwise.
See also
Notes
No points are not considered to be concyclic. One or two points are definitely concyclic and three points are conyclic iff they are not collinear.
For more than three points, create a circle from the first three points. If the circle cannot be created (i.e., they are collinear) then all of the points cannot be concyclic. If the circle is created successfully then simply check the remaining points for containment in the circle.
Examples
>>> from sympy.geometry import Point >>> p1, p2 = Point(-1, 0), Point(1, 0) >>> p3, p4 = Point(0, 1), Point(-1, 2) >>> Point.is_concyclic(p1, p2, p3) True >>> Point.is_concyclic(p1, p2, p3, p4) False
-
length
¶ Treating a Point as a Line, this returns 0 for the length of a Point.
Examples
>>> from sympy import Point >>> p = Point(0, 1) >>> p.length 0
-
midpoint
(p)[source]¶ The midpoint between self and point p.
Parameters: p : Point Returns: midpoint : Point See also
Examples
>>> from sympy.geometry import Point >>> p1, p2 = Point(1, 1), Point(13, 5) >>> p1.midpoint(p2) Point(7, 3)
-
n
(prec=None, **options)¶ Evaluate the coordinates of the point.
This method will, where possible, create and return a new Point where the coordinates are evaluated as floating point numbers to the precision indicated (default=15).
Returns: point : Point Examples
>>> from sympy import Point, Rational >>> p1 = Point(Rational(1, 2), Rational(3, 2)) >>> p1 Point(1/2, 3/2) >>> p1.evalf() Point(0.5, 1.5)
-
rotate
(angle, pt=None)[source]¶ Rotate
angle
radians counterclockwise about Pointpt
.Examples
>>> from sympy import Point, pi >>> t = Point(1, 0) >>> t.rotate(pi/2) Point(0, 1) >>> t.rotate(pi/2, (2, 0)) Point(2, -1)
-
scale
(x=1, y=1, pt=None)[source]¶ Scale the coordinates of the Point by multiplying by
x
andy
after subtractingpt
– default is (0, 0) – and then addingpt
back again (i.e.pt
is the point of reference for the scaling).Examples
>>> from sympy import Point >>> t = Point(1, 1) >>> t.scale(2) Point(2, 1) >>> t.scale(2, 2) Point(2, 2)
-
transform
(matrix)[source]¶ Return the point after applying the transformation described by the 3x3 Matrix,
matrix
.See also
geometry.entity.rotate
,geometry.entity.scale
,geometry.entity.translate
-
translate
(x=0, y=0)[source]¶ Shift the Point by adding x and y to the coordinates of the Point.
Examples
>>> from sympy import Point >>> t = Point(0, 1) >>> t.translate(2) Point(2, 1) >>> t.translate(2, 2) Point(2, 3) >>> t + Point(2, 2) Point(2, 3)
-
x
¶ Returns the X coordinate of the Point.
Examples
>>> from sympy import Point >>> p = Point(0, 1) >>> p.x 0
-
y
¶ Returns the Y coordinate of the Point.
Examples
>>> from sympy import Point >>> p = Point(0, 1) >>> p.y 1
Lines¶
-
class
sympy.geometry.line.
LinearEntity
[source]¶ An abstract base class for all linear entities (line, ray and segment) in a 2-dimensional Euclidean space.
See also
Notes
This is an abstract class and is not meant to be instantiated. Subclasses should implement the following methods:
- __eq__
- contains
Attributes
p1 p2 coefficients slope points -
angle_between
(l1, l2)[source]¶ The angle formed between the two linear entities.
Parameters: l1 : LinearEntity
l2 : LinearEntity
Returns: angle : angle in radians
See also
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
>>> from sympy import Point, Line >>> 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.
Returns: point : Point
Raises: ValueError :
When
parameter
already appears in the Line’s definition.See also
Examples
>>> from sympy import Point, Line >>> p1, p2 = Point(1, 0), Point(5, 3) >>> l1 = Line(p1, p2) >>> l1.arbitrary_point() Point(4*t + 1, 3*t)
-
coefficients
¶ The coefficients (\(a\), \(b\), \(c\)) for the linear equation \(ax + by + c = 0\).
See also
Examples
>>> from sympy import Point, Line >>> from sympy.abc import x, y >>> 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 : Point or LinearEntity Returns: intersection : list of geometrical entities See also
Examples
>>> from sympy import Point, Line, Segment >>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(7, 7) >>> l1 = Line(p1, p2) >>> l1.intersection(p3) [Point(7, 7)]
>>> p4, p5 = Point(5, 0), Point(0, 3) >>> l2 = Line(p4, p5) >>> l1.intersection(l2) [Point(15/8, 15/8)]
>>> p6, p7 = Point(0, 5), Point(2, 6) >>> s1 = Segment(p6, p7) >>> l1.intersection(s1) []
-
is_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.
Returns: True : if the set of linear entities are concurrent,
False : otherwise.
See also
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
>>> from sympy import Point, Line >>> 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) >>> l1.is_concurrent(l2, l3) True
>>> l4 = Line(p2, p3) >>> l4.is_concurrent(l2, l3) False
-
is_parallel
(l1, l2)[source]¶ Are two linear entities parallel?
Parameters: l1 : LinearEntity
l2 : LinearEntity
Returns: True : if l1 and l2 are parallel,
False : otherwise.
See also
Examples
>>> from sympy import Point, Line >>> 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
(l1, l2)[source]¶ Are two linear entities perpendicular?
Parameters: l1 : LinearEntity
l2 : LinearEntity
Returns: True : if l1 and l2 are perpendicular,
False : otherwise.
See also
Examples
>>> from sympy import Point, Line >>> 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
>>> from sympy import Point, Line >>> 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
>>> from sympy import Point, Line >>> p1, p2 = Point(0, 0), Point(3, 5) >>> l1 = Line(p1, p2) >>> l1.length oo
-
p1
¶ The first defining point of a linear entity.
See also
Examples
>>> from sympy import Point, Line >>> p1, p2 = Point(0, 0), Point(5, 3) >>> l = Line(p1, p2) >>> l.p1 Point(0, 0)
-
p2
¶ The second defining point of a linear entity.
See also
Examples
>>> from sympy import Point, Line >>> p1, p2 = Point(0, 0), Point(5, 3) >>> l = Line(p1, p2) >>> l.p2 Point(5, 3)
-
parallel_line
(p)[source]¶ Create a new Line parallel to this linear entity which passes through the point \(p\).
Parameters: p : Point Returns: line : Line See also
Examples
>>> from sympy import Point, Line >>> 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 : Point Returns: line : Line See also
Examples
>>> from sympy import Point, Line >>> 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 : Point Returns: segment : Segment See also
Notes
Returns \(p\) itself if \(p\) is on this linear entity.
Examples
>>> from sympy import Point, Line >>> 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(Point(2, 2), Point(4, 0))
-
points
¶ The two points used to define this linear entity.
Returns: points : tuple of Points See also
Examples
>>> from sympy import Point, Line >>> p1, p2 = Point(0, 0), Point(5, 11) >>> l1 = Line(p1, p2) >>> l1.points (Point(0, 0), Point(5, 11))
-
projection
(o)[source]¶ Project a point, line, ray, or segment onto this linear entity.
Parameters: other : Point or LinearEntity (Line, Ray, Segment)
Returns: projection : Point or LinearEntity (Line, Ray, Segment)
The return type matches the type of the parameter
other
.Raises: GeometryError :
When method is unable to perform projection.
See also
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 is done by creating a perpendicular line through P and L and finding its intersection with L.
Examples
>>> from sympy import Point, Line, Segment, Rational >>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(Rational(1, 2), 0) >>> l1 = Line(p1, p2) >>> l1.projection(p3) Point(1/4, 1/4)
>>> p4, p5 = Point(10, 0), Point(12, 1) >>> s1 = Segment(p4, p5) >>> l1.projection(s1) Segment(Point(5, 5), Point(13/2, 13/2))
-
random_point
()[source]¶ A random point on a LinearEntity.
Returns: point : Point See also
Examples
>>> from sympy import Point, Line >>> 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 Point(...) >>> # point should belong to the line >>> p3 in l1 True
-
slope
¶ The slope of this linear entity, or infinity if vertical.
Returns: slope : number or sympy expression See also
Examples
>>> from sympy import Point, Line >>> 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
sympy.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\).
Parameters: p1 : Point
pt : Point
slope : sympy expression
See also
Notes
At the moment only lines in a 2D space can be declared, because Points can be defined only for 2D spaces.
Examples
>>> import sympy >>> from sympy import Point >>> from sympy.abc import L >>> from sympy.geometry import Line, Segment >>> L = Line(Point(2,3), Point(3,5)) >>> L Line(Point(2, 3), Point(3, 5)) >>> L.points (Point(2, 3), Point(3, 5)) >>> L.equation() -2*x + y + 1 >>> L.coefficients (-2, 1, 1)
Instantiate with keyword
slope
:>>> Line(Point(0, 0), slope=0) Line(Point(0, 0), Point(1, 0))
Instantiate with another linear object
>>> s = Segment((0, 0), (0, 1)) >>> Line(s).equation() x
-
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’.
Returns: equation : sympy expression
See also
Examples
>>> from sympy import Point, Line >>> 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’.
Returns: plot_interval : list (plot interval)
[parameter, lower_bound, upper_bound]
Examples
>>> from sympy import Point, Line >>> p1, p2 = Point(0, 0), Point(5, 3) >>> l1 = Line(p1, p2) >>> l1.plot_interval() [t, -5, 5]
-
-
class
sympy.geometry.line.
Ray
[source]¶ A Ray is a semi-line in the space with a source point and a direction.
Parameters: p1 : Point
The source of the Ray
p2 : Point or radian value
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.
See also
Notes
At the moment only rays in a 2D space can be declared, because Points can be defined only for 2D spaces.
Examples
>>> import sympy >>> from sympy import Point, pi >>> from sympy.abc import r >>> from sympy.geometry import Ray >>> r = Ray(Point(2, 3), Point(3, 5)) >>> r = Ray(Point(2, 3), Point(3, 5)) >>> r Ray(Point(2, 3), Point(3, 5)) >>> r.points (Point(2, 3), Point(3, 5)) >>> r.source Point(2, 3) >>> r.xdirection oo >>> r.ydirection oo >>> r.slope 2 >>> Ray(Point(0, 0), angle=pi/4).slope 1
Attributes
source xdirection ydirection -
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’.
Returns: plot_interval : list
[parameter, lower_bound, upper_bound]
Examples
>>> from sympy import Point, Ray, pi >>> r = Ray((0, 0), angle=pi/4) >>> r.plot_interval() [t, 0, 10]
-
source
¶ The point from which the ray emanates.
See also
Examples
>>> from sympy import Point, Ray >>> p1, p2 = Point(0, 0), Point(4, 1) >>> r1 = Ray(p1, p2) >>> r1.source Point(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.
See also
Examples
>>> from sympy import Point, Ray >>> 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.
See also
Examples
>>> from sympy import Point, Ray >>> 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
sympy.geometry.line.
Segment
[source]¶ An undirected line segment in space.
Parameters: p1 : Point
p2 : Point
See also
Notes
At the moment only segments in a 2D space can be declared, because Points can be defined only for 2D spaces.
Examples
>>> import sympy >>> from sympy import Point >>> from sympy.abc import s >>> from sympy.geometry import Segment >>> Segment((1, 0), (1, 1)) # tuples are interpreted as pts Segment(Point(1, 0), Point(1, 1)) >>> s = Segment(Point(4, 3), Point(1, 1)) >>> s Segment(Point(1, 1), Point(4, 3)) >>> s.points (Point(1, 1), Point(4, 3)) >>> s.slope 2/3 >>> s.length sqrt(13) >>> s.midpoint Point(5/2, 2)
Attributes
length number or sympy expression midpoint Point -
contains
(other)[source]¶ Is the other GeometryEntity contained within this Segment?
Examples
>>> from sympy import Point, Segment >>> 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
>>> from sympy import Point, Segment >>> p1, p2 = Point(0, 1), Point(3, 4) >>> s = Segment(p1, p2) >>> s.distance(Point(10, 15)) sqrt(170)
-
length
¶ The length of the line segment.
See also
Examples
>>> from sympy import Point, Segment >>> p1, p2 = Point(0, 0), Point(4, 3) >>> s1 = Segment(p1, p2) >>> s1.length 5
-
midpoint
¶ The midpoint of the line segment.
See also
Examples
>>> from sympy import Point, Segment >>> p1, p2 = Point(0, 0), Point(4, 3) >>> s1 = Segment(p1, p2) >>> s1.midpoint Point(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 : Point Returns: bisector : Line or Segment See also
Examples
>>> from sympy import Point, Segment >>> p1, p2, p3 = Point(0, 0), Point(6, 6), Point(5, 1) >>> s1 = Segment(p1, p2) >>> s1.perpendicular_bisector() Line(Point(3, 3), Point(9, -3))
>>> s1.perpendicular_bisector(p3) Segment(Point(3, 3), Point(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’.
Returns: plot_interval : list
[parameter, lower_bound, upper_bound]
Examples
>>> from sympy import Point, Segment >>> p1, p2 = Point(0, 0), Point(5, 3) >>> s1 = Segment(p1, p2) >>> s1.plot_interval() [t, 0, 1]
-
Curves¶
-
class
sympy.geometry.curve.
Curve
[source]¶ A curve in space.
A curve is defined by parametric functions for the coordinates, a parameter and the lower and upper bounds for the parameter value.
Parameters: function : list of functions
limits : 3-tuple
Function parameter and lower and upper bounds.
Raises: ValueError :
When \(functions\) are specified incorrectly. When \(limits\) are specified incorrectly.
Examples
>>> from sympy import sin, cos, Symbol, interpolate >>> from sympy.abc import t, a >>> from sympy.geometry import Curve >>> C = Curve((sin(t), cos(t)), (t, 0, 2)) >>> C.functions (sin(t), cos(t)) >>> C.limits (t, 0, 2) >>> C.parameter t >>> C = Curve((t, interpolate([1, 4, 9, 16], t)), (t, 0, 1)); C Curve((t, t**2), (t, 0, 1)) >>> C.subs(t, 4) Point(4, 16) >>> C.arbitrary_point(a) Point(a, a**2)
Attributes
functions parameter limits -
arbitrary_point
(parameter='t')[source]¶ A parameterized point on the curve.
Parameters: parameter : str or Symbol, optional
Default value is ‘t’; the Curve’s parameter is selected with None or self.parameter otherwise the provided symbol is used.
Returns: arbitrary_point : Point
Raises: ValueError :
When \(parameter\) already appears in the functions.
See also
Examples
>>> from sympy import Symbol >>> from sympy.abc import s >>> from sympy.geometry import Curve >>> C = Curve([2*s, s**2], (s, 0, 2)) >>> C.arbitrary_point() Point(2*t, t**2) >>> C.arbitrary_point(C.parameter) Point(2*s, s**2) >>> C.arbitrary_point(None) Point(2*s, s**2) >>> C.arbitrary_point(Symbol('a')) Point(2*a, a**2)
-
free_symbols
¶ Return a set of symbols other than the bound symbols used to parametrically define the Curve.
Examples
>>> from sympy.abc import t, a >>> from sympy.geometry import Curve >>> Curve((t, t**2), (t, 0, 2)).free_symbols set() >>> Curve((t, t**2), (t, a, 2)).free_symbols set([a])
-
functions
¶ The functions specifying the curve.
Returns: functions : list of parameterized coordinate functions. See also
Examples
>>> from sympy.abc import t >>> from sympy.geometry import Curve >>> C = Curve((t, t**2), (t, 0, 2)) >>> C.functions (t, t**2)
-
limits
¶ The limits for the curve.
Returns: limits : tuple
Contains parameter and lower and upper limits.
See also
Examples
>>> from sympy.abc import t >>> from sympy.geometry import Curve >>> C = Curve([t, t**3], (t, -2, 2)) >>> C.limits (t, -2, 2)
-
parameter
¶ The curve function variable.
Returns: parameter : SymPy symbol See also
Examples
>>> from sympy.abc import t >>> from sympy.geometry import Curve >>> C = Curve([t, t**2], (t, 0, 2)) >>> C.parameter t
-
plot_interval
(parameter='t')[source]¶ The plot interval for the default geometric plot of the curve.
Parameters: parameter : str or Symbol, optional
Default value is ‘t’; otherwise the provided symbol is used.
Returns: plot_interval : list (plot interval)
[parameter, lower_bound, upper_bound]
See also
limits
- Returns limits of the parameter interval
Examples
>>> from sympy import Curve, sin >>> from sympy.abc import x, t, s >>> Curve((x, sin(x)), (x, 1, 2)).plot_interval() [t, 1, 2] >>> Curve((x, sin(x)), (x, 1, 2)).plot_interval(s) [s, 1, 2]
-
rotate
(angle=0, pt=None)[source]¶ Rotate
angle
radians counterclockwise about Pointpt
.The default pt is the origin, Point(0, 0).
Examples
>>> from sympy.geometry.curve import Curve >>> from sympy.abc import x >>> from sympy import pi >>> Curve((x, x), (x, 0, 1)).rotate(pi/2) Curve((-x, x), (x, 0, 1))
-
Ellipses¶
-
class
sympy.geometry.ellipse.
Ellipse
[source]¶ An elliptical GeometryEntity.
Parameters: center : Point, optional
Default value is Point(0, 0)
hradius : number or SymPy expression, optional
vradius : number or SymPy expression, optional
eccentricity : number or SymPy expression, optional
Two of \(hradius\), \(vradius\) and \(eccentricity\) must be supplied to create an Ellipse. The third is derived from the two supplied.
Raises: GeometryError :
When \(hradius\), \(vradius\) and \(eccentricity\) are incorrectly supplied as parameters.
TypeError :
When \(center\) is not a Point.
See also
Notes
Constructed from a center and two radii, the first being the horizontal radius (along the x-axis) and the second being the vertical radius (along the y-axis).
When symbolic value for hradius and vradius are used, any calculation that refers to the foci or the major or minor axis will assume that the ellipse has its major radius on the x-axis. If this is not true then a manual rotation is necessary.
Examples
>>> from sympy import Ellipse, Point, Rational >>> e1 = Ellipse(Point(0, 0), 5, 1) >>> e1.hradius, e1.vradius (5, 1) >>> e2 = Ellipse(Point(3, 1), hradius=3, eccentricity=Rational(4, 5)) >>> e2 Ellipse(Point(3, 1), 3, 9/5)
Plotting:
>>> from sympy.plotting.pygletplot import PygletPlot as Plot >>> from sympy import Circle, Segment >>> c1 = Circle(Point(0,0), 1) >>> Plot(c1) [0]: cos(t), sin(t), 'mode=parametric' >>> p = Plot() >>> p[0] = c1 >>> radius = Segment(c1.center, c1.random_point()) >>> p[1] = radius >>> p [0]: cos(t), sin(t), 'mode=parametric' [1]: t*cos(1.546086215036205357975518382), t*sin(1.546086215036205357975518382), 'mode=parametric'
Attributes
center hradius vradius area circumference eccentricity periapsis apoapsis focus_distance foci -
apoapsis
¶ The apoapsis of the ellipse.
The greatest distance between the focus and the contour.
Returns: apoapsis : number See also
periapsis
- Returns shortest distance between foci and contour
Examples
>>> from sympy import Point, Ellipse >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.apoapsis 2*sqrt(2) + 3
-
arbitrary_point
(parameter='t')[source]¶ A parameterized point on the ellipse.
Parameters: parameter : str, optional
Default value is ‘t’.
Returns: arbitrary_point : Point
Raises: ValueError :
When \(parameter\) already appears in the functions.
See also
Examples
>>> from sympy import Point, Ellipse >>> e1 = Ellipse(Point(0, 0), 3, 2) >>> e1.arbitrary_point() Point(3*cos(t), 2*sin(t))
-
area
¶ The area of the ellipse.
Returns: area : number Examples
>>> from sympy import Point, Ellipse >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.area 3*pi
-
center
¶ The center of the ellipse.
Returns: center : number See also
Examples
>>> from sympy import Point, Ellipse >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.center Point(0, 0)
-
circumference
¶ The circumference of the ellipse.
Examples
>>> from sympy import Point, Ellipse >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.circumference 12*Integral(sqrt((-8*_x**2/9 + 1)/(-_x**2 + 1)), (_x, 0, 1))
-
eccentricity
¶ The eccentricity of the ellipse.
Returns: eccentricity : number Examples
>>> from sympy import Point, Ellipse, sqrt >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, sqrt(2)) >>> e1.eccentricity sqrt(7)/3
-
encloses_point
(p)[source]¶ Return True if p is enclosed by (is inside of) self.
Parameters: p : Point Returns: encloses_point : True, False or None See also
Notes
Being on the border of self is considered False.
Examples
>>> from sympy import Ellipse, S >>> from sympy.abc import t >>> e = Ellipse((0, 0), 3, 2) >>> e.encloses_point((0, 0)) True >>> e.encloses_point(e.arbitrary_point(t).subs(t, S.Half)) False >>> e.encloses_point((4, 0)) False
-
equation
(x='x', y='y')[source]¶ The equation of the ellipse.
Parameters: x : str, optional
Label for the x-axis. Default value is ‘x’.
y : str, optional
Label for the y-axis. Default value is ‘y’.
Returns: equation : sympy expression
See also
arbitrary_point
- Returns parameterized point on ellipse
Examples
>>> from sympy import Point, Ellipse >>> e1 = Ellipse(Point(1, 0), 3, 2) >>> e1.equation() y**2/4 + (x/3 - 1/3)**2 - 1
-
foci
¶ The foci of the ellipse.
Raises: ValueError :
When the major and minor axis cannot be determined.
Notes
The foci can only be calculated if the major/minor axes are known.
Examples
>>> from sympy import Point, Ellipse >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.foci (Point(-2*sqrt(2), 0), Point(2*sqrt(2), 0))
-
focus_distance
¶ The focale distance of the ellipse.
The distance between the center and one focus.
Returns: focus_distance : number See also
Examples
>>> from sympy import Point, Ellipse >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.focus_distance 2*sqrt(2)
-
hradius
¶ The horizontal radius of the ellipse.
Returns: hradius : number Examples
>>> from sympy import Point, Ellipse >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.hradius 3
-
intersection
(o)[source]¶ The intersection of this ellipse and another geometrical entity \(o\).
Parameters: o : GeometryEntity Returns: intersection : list of GeometryEntity objects See also
Notes
Currently supports intersections with Point, Line, Segment, Ray, Circle and Ellipse types.
Examples
>>> from sympy import Ellipse, Point, Line, sqrt >>> e = Ellipse(Point(0, 0), 5, 7) >>> e.intersection(Point(0, 0)) [] >>> e.intersection(Point(5, 0)) [Point(5, 0)] >>> e.intersection(Line(Point(0,0), Point(0, 1))) [Point(0, -7), Point(0, 7)] >>> e.intersection(Line(Point(5,0), Point(5, 1))) [Point(5, 0)] >>> e.intersection(Line(Point(6,0), Point(6, 1))) [] >>> e = Ellipse(Point(-1, 0), 4, 3) >>> e.intersection(Ellipse(Point(1, 0), 4, 3)) [Point(0, -3*sqrt(15)/4), Point(0, 3*sqrt(15)/4)] >>> e.intersection(Ellipse(Point(5, 0), 4, 3)) [Point(2, -3*sqrt(7)/4), Point(2, 3*sqrt(7)/4)] >>> e.intersection(Ellipse(Point(100500, 0), 4, 3)) [] >>> e.intersection(Ellipse(Point(0, 0), 3, 4)) [Point(-363/175, -48*sqrt(111)/175), Point(-363/175, 48*sqrt(111)/175), Point(3, 0)] >>> e.intersection(Ellipse(Point(-1, 0), 3, 4)) [Point(-17/5, -12/5), Point(-17/5, 12/5), Point(7/5, -12/5), Point(7/5, 12/5)]
-
is_tangent
(o)[source]¶ Is \(o\) tangent to the ellipse?
Parameters: o : GeometryEntity
An Ellipse, LinearEntity or Polygon
Returns: is_tangent: boolean :
True if o is tangent to the ellipse, False otherwise.
Raises: NotImplementedError :
When the wrong type of argument is supplied.
See also
Examples
>>> from sympy import Point, Ellipse, Line >>> p0, p1, p2 = Point(0, 0), Point(3, 0), Point(3, 3) >>> e1 = Ellipse(p0, 3, 2) >>> l1 = Line(p1, p2) >>> e1.is_tangent(l1) True
-
major
¶ Longer axis of the ellipse (if it can be determined) else hradius.
Returns: major : number or expression Examples
>>> from sympy import Point, Ellipse, Symbol >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.major 3
>>> a = Symbol('a') >>> b = Symbol('b') >>> Ellipse(p1, a, b).major a >>> Ellipse(p1, b, a).major b
>>> m = Symbol('m') >>> M = m + 1 >>> Ellipse(p1, m, M).major m + 1
-
minor
¶ Shorter axis of the ellipse (if it can be determined) else vradius.
Returns: minor : number or expression Examples
>>> from sympy import Point, Ellipse, Symbol >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.minor 1
>>> a = Symbol('a') >>> b = Symbol('b') >>> Ellipse(p1, a, b).minor b >>> Ellipse(p1, b, a).minor a
>>> m = Symbol('m') >>> M = m + 1 >>> Ellipse(p1, m, M).minor m
-
periapsis
¶ The periapsis of the ellipse.
The shortest distance between the focus and the contour.
Returns: periapsis : number See also
apoapsis
- Returns greatest distance between focus and contour
Examples
>>> from sympy import Point, Ellipse >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.periapsis -2*sqrt(2) + 3
-
plot_interval
(parameter='t')[source]¶ The plot interval for the default geometric plot of the Ellipse.
Parameters: parameter : str, optional
Default value is ‘t’.
Returns: plot_interval : list
[parameter, lower_bound, upper_bound]
Examples
>>> from sympy import Point, Ellipse >>> e1 = Ellipse(Point(0, 0), 3, 2) >>> e1.plot_interval() [t, -pi, pi]
-
random_point
(seed=None)[source]¶ A random point on the ellipse.
Returns: point : Point Notes
An arbitrary_point with a random value of t substituted into it may not test as being on the ellipse because the expression tested that a point is on the ellipse doesn’t simplify to zero and doesn’t evaluate exactly to zero:
>>> from sympy.abc import t >>> e1.arbitrary_point(t) Point(3*cos(t), 2*sin(t)) >>> p2 = _.subs(t, 0.1) >>> p2 in e1 False
Note that arbitrary_point routine does not take this approach. A value for cos(t) and sin(t) (not t) is substituted into the arbitrary point. There is a small chance that this will give a point that will not test as being in the ellipse, so the process is repeated (up to 10 times) until a valid point is obtained.
Examples
>>> from sympy import Point, Ellipse, Segment >>> e1 = Ellipse(Point(0, 0), 3, 2) >>> e1.random_point() # gives some random point Point(...) >>> p1 = e1.random_point(seed=0); p1.n(2) Point(2.1, 1.4)
The random_point method assures that the point will test as being in the ellipse:
>>> p1 in e1 True
-
reflect
(line)[source]¶ Override GeometryEntity.reflect since the radius is not a GeometryEntity.
Examples
>>> from sympy import Circle, Line >>> Circle((0, 1), 1).reflect(Line((0, 0), (1, 1))) Circle(Point(1, 0), -1)
-
rotate
(angle=0, pt=None)[source]¶ Rotate
angle
radians counterclockwise about Pointpt
.Note: since the general ellipse is not supported, the axes of the ellipse will not be rotated. Only the center is rotated to a new position.
Examples
>>> from sympy import Ellipse, pi >>> Ellipse((1, 0), 2, 1).rotate(pi/2) Ellipse(Point(0, 1), 2, 1)
-
scale
(x=1, y=1, pt=None)[source]¶ Override GeometryEntity.scale since it is the major and minor axes which must be scaled and they are not GeometryEntities.
Examples
>>> from sympy import Ellipse >>> Ellipse((0, 0), 2, 1).scale(2, 4) Circle(Point(0, 0), 4) >>> Ellipse((0, 0), 2, 1).scale(2) Ellipse(Point(0, 0), 4, 1)
-
tangent_lines
(p)[source]¶ Tangent lines between \(p\) and the ellipse.
If \(p\) is on the ellipse, returns the tangent line through point \(p\). Otherwise, returns the tangent line(s) from \(p\) to the ellipse, or None if no tangent line is possible (e.g., \(p\) inside ellipse).
Parameters: p : Point
Returns: tangent_lines : list with 1 or 2 Lines
Raises: NotImplementedError :
Can only find tangent lines for a point, \(p\), on the ellipse.
Examples
>>> from sympy import Point, Ellipse >>> e1 = Ellipse(Point(0, 0), 3, 2) >>> e1.tangent_lines(Point(3, 0)) [Line(Point(3, 0), Point(3, -12))]
>>> # This will plot an ellipse together with a tangent line. >>> from sympy.plotting.pygletplot import PygletPlot as Plot >>> from sympy import Point, Ellipse >>> e = Ellipse(Point(0,0), 3, 2) >>> t = e.tangent_lines(e.random_point()) >>> p = Plot() >>> p[0] = e >>> p[1] = t
-
vradius
¶ The vertical radius of the ellipse.
Returns: vradius : number Examples
>>> from sympy import Point, Ellipse >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.vradius 1
-
-
class
sympy.geometry.ellipse.
Circle
[source]¶ A circle in space.
Constructed simply from a center and a radius, or from three non-collinear points.
Parameters: center : Point
radius : number or sympy expression
points : sequence of three Points
Raises: GeometryError :
When trying to construct circle from three collinear points. When trying to construct circle from incorrect parameters.
See also
Examples
>>> from sympy.geometry import Point, Circle >>> # a circle constructed from a center and radius >>> c1 = Circle(Point(0, 0), 5) >>> c1.hradius, c1.vradius, c1.radius (5, 5, 5)
>>> # a circle costructed from three points >>> c2 = Circle(Point(0, 0), Point(1, 1), Point(1, 0)) >>> c2.hradius, c2.vradius, c2.radius, c2.center (sqrt(2)/2, sqrt(2)/2, sqrt(2)/2, Point(1/2, 1/2))
Attributes
radius (synonymous with hradius, vradius, major and minor) circumference equation -
circumference
¶ The circumference of the circle.
Returns: circumference : number or SymPy expression Examples
>>> from sympy import Point, Circle >>> c1 = Circle(Point(3, 4), 6) >>> c1.circumference 12*pi
-
equation
(x='x', y='y')[source]¶ The equation of the circle.
Parameters: x : str or Symbol, optional
Default value is ‘x’.
y : str or Symbol, optional
Default value is ‘y’.
Returns: equation : SymPy expression
Examples
>>> from sympy import Point, Circle >>> c1 = Circle(Point(0, 0), 5) >>> c1.equation() x**2 + y**2 - 25
-
intersection
(o)[source]¶ The intersection of this circle with another geometrical entity.
Parameters: o : GeometryEntity Returns: intersection : list of GeometryEntities Examples
>>> from sympy import Point, Circle, Line, Ray >>> p1, p2, p3 = Point(0, 0), Point(5, 5), Point(6, 0) >>> p4 = Point(5, 0) >>> c1 = Circle(p1, 5) >>> c1.intersection(p2) [] >>> c1.intersection(p4) [Point(5, 0)] >>> c1.intersection(Ray(p1, p2)) [Point(5*sqrt(2)/2, 5*sqrt(2)/2)] >>> c1.intersection(Line(p2, p3)) []
-
radius
¶ The radius of the circle.
Returns: radius : number or sympy expression See also
Ellipse.major
,Ellipse.minor
,Ellipse.hradius
,Ellipse.vradius
Examples
>>> from sympy import Point, Circle >>> c1 = Circle(Point(3, 4), 6) >>> c1.radius 6
-
reflect
(line)[source]¶ Override GeometryEntity.reflect since the radius is not a GeometryEntity.
Examples
>>> from sympy import Circle, Line >>> Circle((0, 1), 1).reflect(Line((0, 0), (1, 1))) Circle(Point(1, 0), -1)
-
scale
(x=1, y=1, pt=None)[source]¶ Override GeometryEntity.scale since the radius is not a GeometryEntity.
Examples
>>> from sympy import Circle >>> Circle((0, 0), 1).scale(2, 2) Circle(Point(0, 0), 2) >>> Circle((0, 0), 1).scale(2, 4) Ellipse(Point(0, 0), 2, 4)
-
vradius
¶ This Ellipse property is an alias for the Circle’s radius.
Whereas hradius, major and minor can use Ellipse’s conventions, the vradius does not exist for a circle. It is always a positive value in order that the Circle, like Polygons, will have an area that can be positive or negative as determined by the sign of the hradius.
Examples
>>> from sympy import Point, Circle >>> c1 = Circle(Point(3, 4), 6) >>> c1.vradius 6
-
Polygons¶
-
class
sympy.geometry.polygon.
Polygon
[source]¶ A two-dimensional polygon.
A simple polygon in space. Can be constructed from a sequence of points or from a center, radius, number of sides and rotation angle.
Parameters: vertices : sequence of Points
Raises: GeometryError :
If all parameters are not Points.
If the Polygon has intersecting sides.
Notes
Polygons are treated as closed paths rather than 2D areas so some calculations can be be negative or positive (e.g., area) based on the orientation of the points.
Any consecutive identical points are reduced to a single point and any points collinear and between two points will be removed unless they are needed to define an explicit intersection (see examples).
A Triangle, Segment or Point will be returned when there are 3 or fewer points provided.
Examples
>>> from sympy import Point, Polygon, pi >>> p1, p2, p3, p4, p5 = [(0, 0), (1, 0), (5, 1), (0, 1), (3, 0)] >>> Polygon(p1, p2, p3, p4) Polygon(Point(0, 0), Point(1, 0), Point(5, 1), Point(0, 1)) >>> Polygon(p1, p2) Segment(Point(0, 0), Point(1, 0)) >>> Polygon(p1, p2, p5) Segment(Point(0, 0), Point(3, 0))
While the sides of a polygon are not allowed to cross implicitly, they can do so explicitly. For example, a polygon shaped like a Z with the top left connecting to the bottom right of the Z must have the point in the middle of the Z explicitly given:
>>> mid = Point(1, 1) >>> Polygon((0, 2), (2, 2), mid, (0, 0), (2, 0), mid).area 0 >>> Polygon((0, 2), (2, 2), mid, (2, 0), (0, 0), mid).area -2
When the the keyword \(n\) is used to define the number of sides of the Polygon then a RegularPolygon is created and the other arguments are interpreted as center, radius and rotation. The unrotated RegularPolygon will always have a vertex at Point(r, 0) where \(r\) is the radius of the circle that circumscribes the RegularPolygon. Its method \(spin\) can be used to increment that angle.
>>> p = Polygon((0,0), 1, n=3) >>> p RegularPolygon(Point(0, 0), 1, 3, 0) >>> p.vertices[0] Point(1, 0) >>> p.args[0] Point(0, 0) >>> p.spin(pi/2) >>> p.vertices[0] Point(0, 1)
Attributes
area angles perimeter vertices centroid sides -
angles
¶ The internal angle at each vertex.
Returns: angles : dict
A dictionary where each key is a vertex and each value is the internal angle at that vertex. The vertices are represented as Points.
Examples
>>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.angles[p1] pi/2 >>> poly.angles[p2] acos(-4*sqrt(17)/17)
-
arbitrary_point
(parameter='t')[source]¶ A parameterized point on the polygon.
The parameter, varying from 0 to 1, assigns points to the position on the perimeter that is that fraction of the total perimeter. So the point evaluated at t=1/2 would return the point from the first vertex that is 1/2 way around the polygon.
Parameters: parameter : str, optional
Default value is ‘t’.
Returns: arbitrary_point : Point
Raises: ValueError :
When \(parameter\) already appears in the Polygon’s definition.
See also
Examples
>>> from sympy import Polygon, S, Symbol >>> t = Symbol('t', real=True) >>> tri = Polygon((0, 0), (1, 0), (1, 1)) >>> p = tri.arbitrary_point('t') >>> perimeter = tri.perimeter >>> s1, s2 = [s.length for s in tri.sides[:2]] >>> p.subs(t, (s1 + s2/2)/perimeter) Point(1, 1/2)
-
area
¶ The area of the polygon.
See also
Notes
The area calculation can be positive or negative based on the orientation of the points.
Examples
>>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.area 3
-
centroid
¶ The centroid of the polygon.
Returns: centroid : Point Examples
>>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.centroid Point(31/18, 11/18)
-
distance
(o)[source]¶ Returns the shortest distance between self and o.
If o is a point, then self does not need to be convex. If o is another polygon self and o must be complex.
Examples
>>> from sympy import Point, Polygon, RegularPolygon >>> p1, p2 = map(Point, [(0, 0), (7, 5)]) >>> poly = Polygon(*RegularPolygon(p1, 1, 3).vertices) >>> poly.distance(p2) sqrt(61)
-
encloses_point
(p)[source]¶ Return True if p is enclosed by (is inside of) self.
Parameters: p : Point Returns: encloses_point : True, False or None Notes
Being on the border of self is considered False.
References
[1] http://www.ariel.com.au/a/python-point-int-poly.html
Examples
>>> from sympy import Polygon, Point >>> from sympy.abc import t >>> p = Polygon((0, 0), (4, 0), (4, 4)) >>> p.encloses_point(Point(2, 1)) True >>> p.encloses_point(Point(2, 2)) False >>> p.encloses_point(Point(5, 5)) False
-
intersection
(o)[source]¶ The intersection of two polygons.
The intersection may be empty and can contain individual Points and complete Line Segments.
Parameters: other: Polygon :
Returns: intersection : list
The list of Segments and Points
Examples
>>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly1 = Polygon(p1, p2, p3, p4) >>> p5, p6, p7 = map(Point, [(3, 2), (1, -1), (0, 2)]) >>> poly2 = Polygon(p5, p6, p7) >>> poly1.intersection(poly2) [Point(2/3, 0), Point(9/5, 1/5), Point(7/3, 1), Point(1/3, 1)]
-
is_convex
()[source]¶ Is the polygon convex?
A polygon is convex if all its interior angles are less than 180 degrees.
Returns: is_convex : boolean
True if this polygon is convex, False otherwise.
See also
Examples
>>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.is_convex() True
-
perimeter
¶ The perimeter of the polygon.
Returns: perimeter : number or Basic instance See also
Examples
>>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.perimeter sqrt(17) + 7
-
plot_interval
(parameter='t')[source]¶ The plot interval for the default geometric plot of the polygon.
Parameters: parameter : str, optional
Default value is ‘t’.
Returns: plot_interval : list (plot interval)
[parameter, lower_bound, upper_bound]
Examples
>>> from sympy import Polygon >>> p = Polygon((0, 0), (1, 0), (1, 1)) >>> p.plot_interval() [t, 0, 1]
-
sides
¶ The line segments that form the sides of the polygon.
Returns: sides : list of sides
Each side is a Segment.
Notes
The Segments that represent the sides are an undirected line segment so cannot be used to tell the orientation of the polygon.
Examples
>>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.sides [Segment(Point(0, 0), Point(1, 0)), Segment(Point(1, 0), Point(5, 1)), Segment(Point(0, 1), Point(5, 1)), Segment(Point(0, 0), Point(0, 1))]
-
vertices
¶ The vertices of the polygon.
Returns: vertices : tuple of Points See also
Notes
When iterating over the vertices, it is more efficient to index self rather than to request the vertices and index them. Only use the vertices when you want to process all of them at once. This is even more important with RegularPolygons that calculate each vertex.
Examples
>>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.vertices (Point(0, 0), Point(1, 0), Point(5, 1), Point(0, 1)) >>> poly.args[0] Point(0, 0)
-
-
class
sympy.geometry.polygon.
RegularPolygon
[source]¶ A regular polygon.
Such a polygon has all internal angles equal and all sides the same length.
Parameters: center : Point
radius : number or Basic instance
The distance from the center to a vertex
n : int
The number of sides
Raises: GeometryError :
If the \(center\) is not a Point, or the \(radius\) is not a number or Basic instance, or the number of sides, \(n\), is less than three.
See also
Notes
A RegularPolygon can be instantiated with Polygon with the kwarg n.
Regular polygons are instantiated with a center, radius, number of sides and a rotation angle. Whereas the arguments of a Polygon are vertices, the vertices of the RegularPolygon must be obtained with the vertices method.
Examples
>>> from sympy.geometry import RegularPolygon, Point >>> r = RegularPolygon(Point(0, 0), 5, 3) >>> r RegularPolygon(Point(0, 0), 5, 3, 0) >>> r.vertices[0] Point(5, 0)
Attributes
vertices center radius rotation apothem interior_angle exterior_angle circumcircle incircle angles -
angles
¶ Returns a dictionary with keys, the vertices of the Polygon, and values, the interior angle at each vertex.
Examples
>>> from sympy import RegularPolygon, Point >>> r = RegularPolygon(Point(0, 0), 5, 3) >>> r.angles {Point(-5/2, -5*sqrt(3)/2): pi/3, Point(-5/2, 5*sqrt(3)/2): pi/3, Point(5, 0): pi/3}
-
apothem
¶ The inradius of the RegularPolygon.
The apothem/inradius is the radius of the inscribed circle.
Returns: apothem : number or instance of Basic Examples
>>> from sympy import Symbol >>> from sympy.geometry import RegularPolygon, Point >>> radius = Symbol('r') >>> rp = RegularPolygon(Point(0, 0), radius, 4) >>> rp.apothem sqrt(2)*r/2
-
area
¶ Returns the area.
Examples
>>> from sympy.geometry import RegularPolygon >>> square = RegularPolygon((0, 0), 1, 4) >>> square.area 2 >>> _ == square.length**2 True
-
args
¶ Returns the center point, the radius, the number of sides, and the orientation angle.
Examples
>>> from sympy import RegularPolygon, Point >>> r = RegularPolygon(Point(0, 0), 5, 3) >>> r.args (Point(0, 0), 5, 3, 0)
-
center
¶ The center of the RegularPolygon
This is also the center of the circumscribing circle.
Returns: center : Point Examples
>>> from sympy.geometry import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 5, 4) >>> rp.center Point(0, 0)
-
centroid
¶ The center of the RegularPolygon
This is also the center of the circumscribing circle.
Returns: center : Point Examples
>>> from sympy.geometry import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 5, 4) >>> rp.center Point(0, 0)
-
circumcenter
¶ Alias for center.
Examples
>>> from sympy.geometry import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 5, 4) >>> rp.circumcenter Point(0, 0)
-
circumcircle
¶ The circumcircle of the RegularPolygon.
Returns: circumcircle : Circle See also
Examples
>>> from sympy.geometry import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 4, 8) >>> rp.circumcircle Circle(Point(0, 0), 4)
-
circumradius
¶ Alias for radius.
Examples
>>> from sympy import Symbol >>> from sympy.geometry import RegularPolygon, Point >>> radius = Symbol('r') >>> rp = RegularPolygon(Point(0, 0), radius, 4) >>> rp.circumradius r
-
encloses_point
(p)[source]¶ Return True if p is enclosed by (is inside of) self.
Parameters: p : Point Returns: encloses_point : True, False or None Notes
Being on the border of self is considered False.
The general Polygon.encloses_point method is called only if a point is not within or beyond the incircle or circumcircle, respectively.
Examples
>>> from sympy import RegularPolygon, S, Point, Symbol >>> p = RegularPolygon((0, 0), 3, 4) >>> p.encloses_point(Point(0, 0)) True >>> r, R = p.inradius, p.circumradius >>> p.encloses_point(Point((r + R)/2, 0)) True >>> p.encloses_point(Point(R/2, R/2 + (R - r)/10)) False >>> t = Symbol('t', real=True) >>> p.encloses_point(p.arbitrary_point().subs(t, S.Half)) False >>> p.encloses_point(Point(5, 5)) False
-
exterior_angle
¶ Measure of the exterior angles.
Returns: exterior_angle : number Examples
>>> from sympy.geometry import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 4, 8) >>> rp.exterior_angle pi/4
-
incircle
¶ The incircle of the RegularPolygon.
Returns: incircle : Circle See also
Examples
>>> from sympy.geometry import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 4, 7) >>> rp.incircle Circle(Point(0, 0), 4*cos(pi/7))
-
inradius
¶ Alias for apothem.
Examples
>>> from sympy import Symbol >>> from sympy.geometry import RegularPolygon, Point >>> radius = Symbol('r') >>> rp = RegularPolygon(Point(0, 0), radius, 4) >>> rp.inradius sqrt(2)*r/2
-
interior_angle
¶ Measure of the interior angles.
Returns: interior_angle : number Examples
>>> from sympy.geometry import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 4, 8) >>> rp.interior_angle 3*pi/4
-
length
¶ Returns the length of the sides.
The half-length of the side and the apothem form two legs of a right triangle whose hypotenuse is the radius of the regular polygon.
Examples
>>> from sympy.geometry import RegularPolygon >>> from sympy import sqrt >>> s = square_in_unit_circle = RegularPolygon((0, 0), 1, 4) >>> s.length sqrt(2) >>> sqrt((_/2)**2 + s.apothem**2) == s.radius True
-
radius
¶ Radius of the RegularPolygon
This is also the radius of the circumscribing circle.
Returns: radius : number or instance of Basic Examples
>>> from sympy import Symbol >>> from sympy.geometry import RegularPolygon, Point >>> radius = Symbol('r') >>> rp = RegularPolygon(Point(0, 0), radius, 4) >>> rp.radius r
-
reflect
(line)[source]¶ Override GeometryEntity.reflect since this is not made of only points.
>>> from sympy import RegularPolygon, Line
>>> RegularPolygon((0, 0), 1, 4).reflect(Line((0, 1), slope=-2)) RegularPolygon(Point(4/5, 2/5), -1, 4, acos(3/5))
-
rotate
(angle, pt=None)[source]¶ Override GeometryEntity.rotate to first rotate the RegularPolygon about its center.
>>> from sympy import Point, RegularPolygon, Polygon, pi >>> t = RegularPolygon(Point(1, 0), 1, 3) >>> t.vertices[0] # vertex on x-axis Point(2, 0) >>> t.rotate(pi/2).vertices[0] # vertex on y axis now Point(0, 2)
-
rotation
¶ CCW angle by which the RegularPolygon is rotated
Returns: rotation : number or instance of Basic Examples
>>> from sympy import pi >>> from sympy.geometry import RegularPolygon, Point >>> RegularPolygon(Point(0, 0), 3, 4, pi).rotation pi
-
scale
(x=1, y=1, pt=None)[source]¶ Override GeometryEntity.scale since it is the radius that must be scaled (if x == y) or else a new Polygon must be returned.
>>> from sympy import RegularPolygon
Symmetric scaling returns a RegularPolygon:
>>> RegularPolygon((0, 0), 1, 4).scale(2, 2) RegularPolygon(Point(0, 0), 2, 4, 0)
Asymmetric scaling returns a kite as a Polygon:
>>> RegularPolygon((0, 0), 1, 4).scale(2, 1) Polygon(Point(2, 0), Point(0, 1), Point(-2, 0), Point(0, -1))
-
spin
(angle)[source]¶ Increment in place the virtual Polygon’s rotation by ccw angle.
See also: rotate method which moves the center.
>>> from sympy import Polygon, Point, pi >>> r = Polygon(Point(0,0), 1, n=3) >>> r.vertices[0] Point(1, 0) >>> r.spin(pi/6) >>> r.vertices[0] Point(sqrt(3)/2, 1/2)
-
vertices
¶ The vertices of the RegularPolygon.
Returns: vertices : list
Each vertex is a Point.
See also
Examples
>>> from sympy.geometry import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 5, 4) >>> rp.vertices [Point(5, 0), Point(0, 5), Point(-5, 0), Point(0, -5)]
-
-
class
sympy.geometry.polygon.
Triangle
[source]¶ A polygon with three vertices and three sides.
Parameters: points : sequence of Points
keyword: asa, sas, or sss to specify sides/angles of the triangle :
Raises: GeometryError :
If the number of vertices is not equal to three, or one of the vertices is not a Point, or a valid keyword is not given.
See also
Examples
>>> from sympy.geometry import Triangle, Point >>> Triangle(Point(0, 0), Point(4, 0), Point(4, 3)) Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
Keywords sss, sas, or asa can be used to give the desired side lengths (in order) and interior angles (in degrees) that define the triangle:
>>> Triangle(sss=(3, 4, 5)) Triangle(Point(0, 0), Point(3, 0), Point(3, 4)) >>> Triangle(asa=(30, 1, 30)) Triangle(Point(0, 0), Point(1, 0), Point(1/2, sqrt(3)/6)) >>> Triangle(sas=(1, 45, 2)) Triangle(Point(0, 0), Point(2, 0), Point(sqrt(2)/2, sqrt(2)/2))
Attributes
vertices altitudes orthocenter circumcenter circumradius circumcircle inradius incircle medians medial -
altitudes
¶ The altitudes of the triangle.
An altitude of a triangle is a segment through a vertex, perpendicular to the opposite side, with length being the height of the vertex measured from the line containing the side.
Returns: altitudes : dict
The dictionary consists of keys which are vertices and values which are Segments.
Examples
>>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.altitudes[p1] Segment(Point(0, 0), Point(1/2, 1/2))
-
bisectors
()[source]¶ The angle bisectors of the triangle.
An angle bisector of a triangle is a straight line through a vertex which cuts the corresponding angle in half.
Returns: bisectors : dict
Each key is a vertex (Point) and each value is the corresponding bisector (Segment).
Examples
>>> from sympy.geometry import Point, Triangle, Segment >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> from sympy import sqrt >>> t.bisectors()[p2] == Segment(Point(0, sqrt(2) - 1), Point(1, 0)) True
-
circumcenter
¶ The circumcenter of the triangle
The circumcenter is the center of the circumcircle.
Returns: circumcenter : Point See also
Examples
>>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.circumcenter Point(1/2, 1/2)
-
circumcircle
¶ The circle which passes through the three vertices of the triangle.
Returns: circumcircle : Circle See also
Examples
>>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.circumcircle Circle(Point(1/2, 1/2), sqrt(2)/2)
-
circumradius
¶ The radius of the circumcircle of the triangle.
Returns: circumradius : number of Basic instance See also
Examples
>>> from sympy import Symbol >>> from sympy.geometry import Point, Triangle >>> a = Symbol('a') >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, a) >>> t = Triangle(p1, p2, p3) >>> t.circumradius sqrt(a**2/4 + 1/4)
-
incenter
¶ The center of the incircle.
The incircle is the circle which lies inside the triangle and touches all three sides.
Returns: incenter : Point See also
Examples
>>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.incenter Point(-sqrt(2)/2 + 1, -sqrt(2)/2 + 1)
-
incircle
¶ The incircle of the triangle.
The incircle is the circle which lies inside the triangle and touches all three sides.
Returns: incircle : Circle See also
Examples
>>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(2, 0), Point(0, 2) >>> t = Triangle(p1, p2, p3) >>> t.incircle Circle(Point(-sqrt(2) + 2, -sqrt(2) + 2), -sqrt(2) + 2)
-
inradius
¶ The radius of the incircle.
Returns: inradius : number of Basic instance See also
Examples
>>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(4, 0), Point(0, 3) >>> t = Triangle(p1, p2, p3) >>> t.inradius 1
-
is_equilateral
()[source]¶ Are all the sides the same length?
Returns: is_equilateral : boolean See also
sympy.geometry.entity.GeometryEntity.is_similar
,RegularPolygon
,is_isosceles
,is_right
,is_scalene
Examples
>>> from sympy.geometry import Triangle, Point >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3)) >>> t1.is_equilateral() False
>>> from sympy import sqrt >>> t2 = Triangle(Point(0, 0), Point(10, 0), Point(5, 5*sqrt(3))) >>> t2.is_equilateral() True
-
is_isosceles
()[source]¶ Are two or more of the sides the same length?
Returns: is_isosceles : boolean See also
Examples
>>> from sympy.geometry import Triangle, Point >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(2, 4)) >>> t1.is_isosceles() True
-
is_right
()[source]¶ Is the triangle right-angled.
Returns: is_right : boolean See also
sympy.geometry.line.LinearEntity.is_perpendicular
,is_equilateral
,is_isosceles
,is_scalene
Examples
>>> from sympy.geometry import Triangle, Point >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3)) >>> t1.is_right() True
-
is_scalene
()[source]¶ Are all the sides of the triangle of different lengths?
Returns: is_scalene : boolean See also
Examples
>>> from sympy.geometry import Triangle, Point >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(1, 4)) >>> t1.is_scalene() True
-
is_similar
(t1, t2)[source]¶ Is another triangle similar to this one.
Two triangles are similar if one can be uniformly scaled to the other.
Parameters: other: Triangle : Returns: is_similar : boolean Examples
>>> from sympy.geometry import Triangle, Point >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3)) >>> t2 = Triangle(Point(0, 0), Point(-4, 0), Point(-4, -3)) >>> t1.is_similar(t2) True
>>> t2 = Triangle(Point(0, 0), Point(-4, 0), Point(-4, -4)) >>> t1.is_similar(t2) False
-
medial
¶ The medial triangle of the triangle.
The triangle which is formed from the midpoints of the three sides.
Returns: medial : Triangle See also
Examples
>>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.medial Triangle(Point(1/2, 0), Point(1/2, 1/2), Point(0, 1/2))
-
medians
¶ The medians of the triangle.
A median of a triangle is a straight line through a vertex and the midpoint of the opposite side, and divides the triangle into two equal areas.
Returns: medians : dict
Each key is a vertex (Point) and each value is the median (Segment) at that point.
Examples
>>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.medians[p1] Segment(Point(0, 0), Point(1/2, 1/2))
-
orthocenter
¶ The orthocenter of the triangle.
The orthocenter is the intersection of the altitudes of a triangle. It may lie inside, outside or on the triangle.
Returns: orthocenter : Point See also
Examples
>>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.orthocenter Point(0, 0)
-
vertices
¶ The triangle’s vertices
Returns: vertices : tuple
Each element in the tuple is a Point
See also
Examples
>>> from sympy.geometry import Triangle, Point >>> t = Triangle(Point(0, 0), Point(4, 0), Point(4, 3)) >>> t.vertices (Point(0, 0), Point(4, 0), Point(4, 3))
-