Skip to content
Related Articles

Related Articles

What are Entities in SymPy?

View Discussion
Improve Article
Save Article
  • Last Updated : 29 Mar, 2022
View Discussion
Improve Article
Save Article

The geometry module in SymPy is the foundation class for all geometrical entities Python, allowing you to create two-dimensional objects like lines and circles, polygons, etc. Then we may find out more about it by looking for collinearity or detecting intersections. Any object with particular geometric qualities is referred to as a GeometryEntity.

class sympy.geometry.entity.GeometryEntity(*args, **kwargs)

All geometrical entities inherit from this basic class. This class does not represent any specific geometric entity; instead, it implements several methods that are shared by all subclasses.

Points

A location is a point in geometry. It has no dimensions, i.e. no width, length, or depth. A dot represents a point. Collinearity is the property of a set of points lying on a single line in geometry. Collinearity refers to a group of points that have this property. Point() function is used to create a point in space. Point class contains the distance() method to find the distance between two points.

Python3




# import packages
from sympy.geometry import Point
 
# create points
x = Point(1, 1)
y = Point(2, 2)
z = Point(3, 3)
w = Point(5, 2)
 
# checking if points are collinear.
print(Point.is_collinear(x, y, z))
print(Point.is_collinear(y, z, w))
 
# calculating distance between two points
print('Distance between x and y points is ' + str(x.distance(y)))


Output:

True
False
Distance between x and y points is sqrt(2)

The formula for distance of a point from origin: 

Python3




# importing packages
from sympy.geometry import Point
from sympy.abc import a, b
 
# defining a point
p = Point(a, b)
 
# distance of the point from the origin
print(p.distance(Point(0, 0)))


Output:

sqrt(a**2 + b**2)

Line

A line is defined as a set of points that stretches in two directions indefinitely. It just has one dimension, which is length. Line() is created with the help of two points. intersection() method is used to find the point of intersection between two lines. angle_between() function is used to find angles between two lines.

Python3




# importing packages
from sympy.geometry import Point, Line
 
# creating two points
p1, p2 = Point(1, 2), Point(2, 0)
line1 = Line(p1, p2)
print(line1)
 
# creating two points
line2 = Line(Point(2, 4), Point(6, 2))
print(line2)
 
# intersection point of two lines
print(line1.intersection(line2))
 
# Angle between the two lines
print('Angle between two lines is : \
' + str(line1.angle_between(line2)))


Output:

Line2D(Point2D(1, 2), Point2D(2, 0))
Line2D(Point2D(2, 4), Point2D(6, 2))
[Point2D(-2/3, 16/3)]
Angle between two lines is : acos(4/5)

Triangle

A triangle is a three-sided polygon with three vertices and three edges. It is one of the most fundamental geometric forms. Triangle is formed with the help of three points or vertices. .area property is used to find the area of the triangle.

Triangle(vertex1,vertex2,vertex3)

Python3




# importing packages
from sympy.geometry import Point, Triangle
 
# constructing a triangle with three points
triangle = Triangle(Point(0, 0), Point(3, 0), Point(3, 3))
 
# area of the triangle
print('area of the triangle is : '+str(triangle.area))


Output:

area of the triangle is : 9/2

Polygon

RegularPolygon() method from the geometry class is used to construct a RegularPolygon. It takes the below parameters :

class sympy.geometry.polygon.RegularPolygon()

Python3




# importing packages
from sympy import RegularPolygon, Point
 
fig  = RegularPolygon(Point(0, 0), 1, 3)
 
# area of the regular polygon
print(fig.area)


Output:

3*sqrt(3)/4

Circle

A circle is a curve sketched out by a point moving in a plane so that its distance from a given point is constant; alternatively, it is the shape formed by all points in a plane that are at a set distance from a given point, the center. 

class sympy.geometry.ellipse.Circle(*args, **kwargs)

Circle() method takes a point as center and other parameters like radius and constructs a circle. Area property is used to find the area of the circle. 

Python3




# importing packages
from sympy import Circle, Point
 
fig = Circle(Point(0, 0), 3)
 
# area of the regular polygon
print(fig.area)


Output:

9*pi

Ellipse

An ellipse is a closed curve made up of points whose distances from two fixed points (foci) all add up to the same number. The center is the location where the foci meet in the middle. An ellipse’s property is that a line reflected off its border from one focus will pass through the other.

class sympy.geometry.ellipse.Ellipse(center=None, hradius=None, vradius=None, eccentricity=None, **kwargs)

Equation of the ellipse can be constructed with the equation() method, it takes symbols as inputs. .area property is used to display the area of the circle and the .circumference attribute is used to find the circumference of the circle. 

Python3




# importing packages
from sympy.geometry import Ellipse, Point
from sympy.abc import x, y
 
# ellipse
ellipse = Ellipse(Point(0, 0), 5, 8)
 
# area of ellipse
print('area of the ellipse is : '+str(ellipse.area))
 
# equation of ellipse
print('equation of the ellipse is : ')
print(ellipse.equation(x, y))
 
# circumference of ellipse
print('circumference of the ellipse is : \
'+str(ellipse.circumference))


Output:

area of the ellipse is : 40*pi
equation of the ellipse is : 
x**2/25 + y**2/64 - 1
circumference of the ellipse is : 32*elliptic_e(39/64)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!