# Geometry#

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

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]
Segment2D(Point2D(0, 0), Point2D(1, 1/2))
>>> m = t.medians
>>> intersection(m[x], m[y], m[zp])
[Point2D(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)
[Point2D(-5*sqrt(2)/2, -5*sqrt(2)/2), Point2D(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]
Segment2D(Point2D(0, 0), Point2D(3*a/2, b/2))

>>> intersection(t.medians[x], t.medians[y], t.medians[z])
[Point2D(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)
>>> pp2 = intersection(ll2, ll5)
>>> pp3 = intersection(ll4, ll6)
>>>
>>> Point.is_collinear(pp1, pp2, pp3)
True