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)
[Point2D(1, 1)]
>>> intersection(l1, l2)
[Point2D(1, 1)]
>>> intersection(c, p2)
[]
>>> intersection(c, Point(1, 0))
[Point2D(1, 0)]
>>> intersection(c, l2)
[Point2D(-sqrt(5)/5 + 1, 2*sqrt(5)/5 + 1),
 Point2D(sqrt(5)/5 + 1, -2*sqrt(5)/5 + 1)]
sympy.geometry.util.convex_hull(*args, **kwargs)[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 if polygon is True else as a tuple \((U, L)\) where L and U are the lower and upper hulls, respectively.

Notes

This can only be performed on a set of points whose coordinates can be ordered on the number line.

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://geomalgorithms.com/a10-_hull-1.html

Examples

>>> from sympy.geometry import Point, convex_hull
>>> points = [(1, 1), (1, 2), (3, 1), (-5, 2), (15, 4)]
>>> convex_hull(*points)
Polygon(Point2D(-5, 2), Point2D(1, 1), Point2D(3, 1), Point2D(15, 4))
>>> convex_hull(*points, **dict(polygon=False))
([Point2D(-5, 2), Point2D(15, 4)],
 [Point2D(-5, 2), Point2D(1, 1), Point2D(3, 1), Point2D(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
(Point2D(20/3, 10/3), Point2D(20/3, 70/3))
>>> centroid(p, q)
Point2D(20/3, 40/3)
>>> p, q = Segment((0, 0), (2, 0)), Segment((0, 0), (2, 2))
>>> centroid(p, q)
Point2D(1, -sqrt(2) + 2)
>>> centroid(Point(0, 0), Point(2, 0))
Point2D(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)
Point2D(3/2, 1/2)
>>> centroid(p, p, p, q) # centroid x-coord shifts left
Point2D(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)
Point2D(11/10, 1/2)

Previous topic

Entities

Next topic

Points

This Page