SymPy - Entities



The geometry module in SymPy allows creation of two dimensional entities such as line, circle, etc. We can then obtain information about them such as checking colinearity or finding intersection.

Point

Point class represents a point in Euclidean space. Following example checks for collinearity of points −

>>> from sympy.geometry import Point 
>>> from sympy import * 
>>> x=Point(0,0) 
>>> y=Point(2,2) 
>>> z=Point(4,4) 
>>> Point.is_collinear(x,y,z)

Output

True

>>> a=Point(2,3) 
>>> Point.is_collinear(x,y,a)

Output

False

The distance() method of Point class calculates distance between two points

>>> x.distance(y)

Output

$2\sqrt2$

The distance may also be represented in terms of symbols.

Line

Line entity is obtained from two Point objects. The intersection() method returns point of intersection if two lines intersect each other.

>>> from sympy.geometry import Point, Line 
>>> p1, p2=Point(0,5), Point(5,0) 
>>> l1=Line(p1,p2)
>>> l2=Line(Point(0,0), Point(5,5)) 
>>> l1.intersection(l2)

Output

[Point2D(5/2, 5/2)]

>>> l1.intersection(Line(Point(0,0), Point(2,2)))

Output

[Point2D(5/2, 5/2)]

>>> x,y=symbols('x y') 
>>> p=Point(x,y) 
>>> p.distance(Point(0,0))

Output

$\sqrt{x^2 + y^2}$

Triangle

This function builds a triangle entity from three point objects.

Triangle(a,b,c)

>>> t=Triangle(Point(0,0),Point(0,5), Point(5,0)) 
>>> t.area

Output

$-\frac{25}{2}$

Ellipse

An elliptical geometry entity is constructed by passing a Point object corresponding to center and two numbers each for horizontal and vertical radius.

ellipse(center, hradius, vradius)

>>> from sympy.geometry import Ellipse, Line 
>>> e=Ellipse(Point(0,0),8,3) 
>>> e.area

Output

$24\pi$

The vradius can be indirectly provided by using eccentricity parameter.

>>> e1=Ellipse(Point(2,2), hradius=5, eccentricity=Rational(3,4)) 
>>> e1.vradius

Output

$\frac{5\sqrt7}{4}$

The apoapsis of the ellipse is the greatest distance between the focus and the contour.

>>> e1.apoapsis

Output

$\frac{35}{4}$

Following statement calculates circumference of ellipse −

>>> e1.circumference

Output

$20E(\frac{9}{16})$

The equation method of ellipse returns equation of ellipse.

>>> e1.equation(x,y)

Output

$(\frac{x}{5}-\frac{2}{5})^2 + \frac{16(y-2)2}{175} - 1$

Advertisements