# Polygons¶

class diofant.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)

area
angles
perimeter
vertices
centroid
sides
Raises

diofant.geometry.exceptions.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

>>> p1, p2, p3, p4, p5 = [(0, 0), (1, 0), (5, 1), (0, 1), (3, 0)]
>>> Polygon(p1, p2, p3, p4)
Polygon(Point2D(0, 0), Point2D(1, 0), Point2D(5, 1), Point2D(0, 1))
>>> Polygon(p1, p2)
Segment(Point2D(0, 0), Point2D(1, 0))
>>> Polygon(p1, p2, p5)
Segment(Point2D(0, 0), Point2D(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(Point2D(0, 0), 1, 3, 0)
>>> p.vertices[0]
Point2D(1, 0)
>>> p.args[0]
Point2D(0, 0)
>>> p.spin(pi/2)
>>> p.vertices[0]
Point2D(0, 1)

property 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

>>> 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.

Examples

>>> t = Symbol('t', extended_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})
Point2D(1, 1/2)

property area

The area of the polygon.

Notes

The area calculation can be positive or negative based on the orientation of the points.

Examples

>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
>>> poly = Polygon(p1, p2, p3, p4)
>>> poly.area
3

property centroid

The centroid of the polygon.

Returns

centroid (Point)

Examples

>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
>>> poly = Polygon(p1, p2, p3, p4)
>>> poly.centroid
Point2D(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

>>> 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.

Notes

Being on the border of self is considered False.

Parameters

p (Point)

Returns

encloses_point (True, False or None)

Examples

>>> 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


References

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

>>> 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)
[Point2D(2/3, 0), Point2D(9/5, 1/5), Point2D(7/3, 1), Point2D(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.

Examples

>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
>>> poly = Polygon(p1, p2, p3, p4)
>>> poly.is_convex()
True

property perimeter

The perimeter of the polygon.

Returns

perimeter (number or Basic instance)

Examples

>>> 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

>>> p = Polygon((0, 0), (1, 0), (1, 1))
>>> p.plot_interval()
[t, 0, 1]

property 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

>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
>>> poly = Polygon(p1, p2, p3, p4)
>>> poly.sides
[Segment(Point2D(0, 0), Point2D(1, 0)),
Segment(Point2D(1, 0), Point2D(5, 1)),
Segment(Point2D(0, 1), Point2D(5, 1)), Segment(Point2D(0, 0), Point2D(0, 1))]

property vertices

The vertices of the polygon.

Returns

vertices (tuple of Points)

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

>>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
>>> poly = Polygon(p1, p2, p3, p4)
>>> poly.vertices
(Point2D(0, 0), Point2D(1, 0), Point2D(5, 1), Point2D(0, 1))
>>> poly.args[0]
Point2D(0, 0)

class diofant.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

vertices
center
radius
rotation
apothem
interior_angle
exterior_angle
circumcircle
incircle
angles
Raises

diofant.geometry.exceptions.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.

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

>>> r = RegularPolygon(Point(0, 0), 5, 3)
>>> r
RegularPolygon(Point2D(0, 0), 5, 3, 0)
>>> r.vertices[0]
Point2D(5, 0)

property angles

Returns a dictionary with keys, the vertices of the Polygon, and values, the interior angle at each vertex.

Examples

>>> r = RegularPolygon(Point2D(0, 0), 5, 3)
>>> r.angles
{Point2D(-5/2, -5*sqrt(3)/2): pi/3,
Point2D(-5/2, 5*sqrt(3)/2): pi/3,
Point2D(5, 0): pi/3}

property apothem

Returns

apothem (number or instance of Basic)

Examples

>>> radius = Symbol('r')
>>> rp = RegularPolygon(Point(0, 0), radius, 4)
>>> rp.apothem
sqrt(2)*r/2

property area

Returns the area.

Examples

>>> square = RegularPolygon((0, 0), 1, 4)
>>> square.area
2
>>> _ == square.length**2
True

property args

Returns the center point, the radius, the number of sides, and the orientation angle.

Examples

>>> r = RegularPolygon(Point(0, 0), 5, 3)
>>> r.args
(Point2D(0, 0), 5, 3, 0)

property center

The center of the RegularPolygon

This is also the center of the circumscribing circle.

Returns

center (Point)

Examples

>>> rp = RegularPolygon(Point(0, 0), 5, 4)
>>> rp.center
Point2D(0, 0)

property centroid

The center of the RegularPolygon

This is also the center of the circumscribing circle.

Returns

center (Point)

Examples

>>> rp = RegularPolygon(Point(0, 0), 5, 4)
>>> rp.center
Point2D(0, 0)

property circumcenter

Alias for center.

Examples

>>> rp = RegularPolygon(Point(0, 0), 5, 4)
>>> rp.circumcenter
Point2D(0, 0)

property circumcircle

The circumcircle of the RegularPolygon.

Returns

circumcircle (Circle)

Examples

>>> rp = RegularPolygon(Point(0, 0), 4, 8)
>>> rp.circumcircle
Circle(Point2D(0, 0), 4)

property circumradius

Examples

>>> radius = Symbol('r')
>>> rp = RegularPolygon(Point(0, 0), radius, 4)
r

encloses_point(p)[source]

Return True if p is enclosed by (is inside of) self.

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.

Parameters

p (Point)

Returns

encloses_point (True, False or None)

Examples

>>> p = RegularPolygon((0, 0), 3, 4)
>>> p.encloses_point(Point(0, 0))
True
>>> p.encloses_point(Point((r + R)/2, 0))
True
>>> p.encloses_point(Point(R/2, R/2 + (R - r)/10))
False
>>> t = Symbol('t', extended_real=True)
>>> p.encloses_point(p.arbitrary_point().subs({t: Rational(1, 2)}))
False
>>> p.encloses_point(Point(5, 5))
False

property exterior_angle

Measure of the exterior angles.

Returns

exterior_angle (number)

Examples

>>> rp = RegularPolygon(Point(0, 0), 4, 8)
>>> rp.exterior_angle
pi/4

property incircle

The incircle of the RegularPolygon.

Returns

incircle (Circle)

Examples

>>> rp = RegularPolygon(Point(0, 0), 4, 7)
>>> rp.incircle
Circle(Point2D(0, 0), 4*cos(pi/7))

property inradius

Alias for apothem.

Examples

>>> radius = Symbol('r')
>>> rp = RegularPolygon(Point(0, 0), radius, 4)
sqrt(2)*r/2

property interior_angle

Measure of the interior angles.

Returns

interior_angle (number)

Examples

>>> rp = RegularPolygon(Point(0, 0), 4, 8)
>>> rp.interior_angle
3*pi/4

property 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

>>> s = square_in_unit_circle = RegularPolygon((0, 0), 1, 4)
>>> s.length
sqrt(2)
>>> sqrt((_/2)**2 + s.apothem**2) == s.radius
True

property radius

This is also the radius of the circumscribing circle.

Returns

radius (number or instance of Basic)

Examples

>>> radius = Symbol('r')
>>> rp = RegularPolygon(Point(0, 0), radius, 4)
r

reflect(line)[source]

Override GeometryEntity.reflect since this is not made of only points.

>>> RegularPolygon((0, 0), 1, 4).reflect(Line((0, 1), slope=-2))
RegularPolygon(Point2D(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.

>>> t = RegularPolygon(Point(1, 0), 1, 3)
>>> t.vertices[0] # vertex on x-axis
Point2D(2, 0)
>>> t.rotate(pi/2).vertices[0] # vertex on y axis now
Point2D(0, 2)


rotation()

spin()

Rotates a RegularPolygon in place

property rotation

CCW angle by which the RegularPolygon is rotated

Returns

rotation (number or instance of Basic)

Examples

>>> 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.

Symmetric scaling returns a RegularPolygon:

>>> RegularPolygon((0, 0), 1, 4).scale(2, 2)
RegularPolygon(Point2D(0, 0), 2, 4, 0)


Asymmetric scaling returns a kite as a Polygon:

>>> RegularPolygon((0, 0), 1, 4).scale(2, 1)
Polygon(Point2D(2, 0), Point2D(0, 1), Point2D(-2, 0), Point2D(0, -1))

spin(angle)[source]

Increment in place the virtual Polygon’s rotation by ccw angle.

>>> r = Polygon(Point(0, 0), 1, n=3)
>>> r.vertices[0]
Point2D(1, 0)
>>> r.spin(pi/6)
>>> r.vertices[0]
Point2D(sqrt(3)/2, 1/2)


rotation()

rotate()

Creates a copy of the RegularPolygon rotated about a Point

property vertices

The vertices of the RegularPolygon.

Returns

vertices (list) – Each vertex is a Point.

Examples

>>> rp = RegularPolygon(Point(0, 0), 5, 4)
>>> rp.vertices
[Point2D(5, 0), Point2D(0, 5), Point2D(-5, 0), Point2D(0, -5)]

class diofant.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)

vertices
altitudes
orthocenter
circumcenter
circumradius
circumcircle
inradius
incircle
medians
medial
Raises

diofant.geometry.exceptions.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.

Examples

>>> Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
Triangle(Point2D(0, 0), Point2D(4, 0), Point2D(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(Point2D(0, 0), Point2D(3, 0), Point2D(3, 4))
>>> Triangle(asa=(30, 1, 30))
Triangle(Point2D(0, 0), Point2D(1, 0), Point2D(1/2, sqrt(3)/6))
>>> Triangle(sas=(1, 45, 2))
Triangle(Point2D(0, 0), Point2D(2, 0), Point2D(sqrt(2)/2, sqrt(2)/2))

property 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

>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
>>> t = Triangle(p1, p2, p3)
>>> t.altitudes[p1]
Segment(Point2D(0, 0), Point2D(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

>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
>>> t = Triangle(p1, p2, p3)
>>> t.bisectors()[p2] == Segment(Point(0, sqrt(2) - 1), Point(1, 0))
True

property circumcenter

The circumcenter of the triangle

The circumcenter is the center of the circumcircle.

Returns

circumcenter (Point)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
>>> t = Triangle(p1, p2, p3)
>>> t.circumcenter
Point2D(1/2, 1/2)

property circumcircle

The circle which passes through the three vertices of the triangle.

Returns

circumcircle (Circle)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
>>> t = Triangle(p1, p2, p3)
>>> t.circumcircle
Circle(Point2D(1/2, 1/2), sqrt(2)/2)

property circumradius

The radius of the circumcircle of the triangle.

Returns

Examples

>>> a = Symbol('a')
>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, a)
>>> t = Triangle(p1, p2, p3)
sqrt(a**2/4 + 1/4)

property incenter

The center of the incircle.

The incircle is the circle which lies inside the triangle and touches all three sides.

Returns

incenter (Point)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
>>> t = Triangle(p1, p2, p3)
>>> t.incenter
Point2D(-sqrt(2)/2 + 1, -sqrt(2)/2 + 1)

property incircle

The incircle of the triangle.

The incircle is the circle which lies inside the triangle and touches all three sides.

Returns

incircle (Circle)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(2, 0), Point(0, 2)
>>> t = Triangle(p1, p2, p3)
>>> t.incircle
Circle(Point2D(-sqrt(2) + 2, -sqrt(2) + 2), -sqrt(2) + 2)

property inradius

Returns

Examples

>>> p1, p2, p3 = Point(0, 0), Point(4, 0), Point(0, 3)
>>> t = Triangle(p1, p2, p3)
1

is_equilateral()[source]

Are all the sides the same length?

Returns

is_equilateral (boolean)

Examples

>>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
>>> t1.is_equilateral()
False

>>> 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)

Examples

>>> 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)

Examples

>>> 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)

Examples

>>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(1, 4))
>>> t1.is_scalene()
True

is_similar(other)[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

>>> 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

property medial

The medial triangle of the triangle.

The triangle which is formed from the midpoints of the three sides.

Returns

medial (Triangle)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
>>> t = Triangle(p1, p2, p3)
>>> t.medial
Triangle(Point2D(1/2, 0), Point2D(1/2, 1/2), Point2D(0, 1/2))

property 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

>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
>>> t = Triangle(p1, p2, p3)
>>> t.medians[p1]
Segment(Point2D(0, 0), Point2D(1/2, 1/2))

property 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)

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
>>> t = Triangle(p1, p2, p3)
>>> t.orthocenter
Point2D(0, 0)

property vertices

The triangle’s vertices

Returns

vertices (tuple) – Each element in the tuple is a Point

Examples

>>> t = Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
>>> t.vertices
(Point2D(0, 0), Point2D(4, 0), Point2D(4, 3))