Navigation

Source code for sympy.geometry.entity

"""The definition of the base geometrical entity with attributes common toall derived geometrical entities.Contains========GeometryEntity"""from__future__importprint_function,divisionfromsympy.core.compatibilityimportis_sequencefromsympy.core.basicimportBasicfromsympy.core.sympifyimportsympifyfromsympy.functionsimportcos,sinfromsympy.matricesimporteye# How entities are ordered; used by __cmp__ in GeometryEntityordering_of_classes=["Point","Segment","Ray","Line","Triangle","RegularPolygon","Polygon","Circle","Ellipse","Curve"]

[docs]classGeometryEntity(Basic):"""The base class for all geometrical entities. This class doesn't represent any particular geometric entity, it only provides the implementation of some methods common to all subclasses. """def__new__(cls,*args,**kwargs):args=map(sympify,args)returnBasic.__new__(cls,*args)def_sympy_(self):returnselfdef__getnewargs__(self):returntuple(self.args)

[docs]defintersection(self,o):""" Returns a list of all of the intersections of self with o. Notes ===== An entity is not required to implement this method. If two different types of entities can intersect, the item with higher index in ordering_of_classes should implement intersections with anything having a lower index. See Also ======== sympy.geometry.util.intersection """raiseNotImplementedError()

defreflect(self,line):fromsympyimportatan,Line,Point,Dummy,oog=selfl=lineo=Point(0,0)ifl.slope==0:y=l.args[0].yifnoty:# x-axisreturng.scale(y=-1)reps=[(p,p.translate(y=2*(y-p.y)))forping.atoms(Point)]elifl.slope==oo:x=l.args[0].xifnotx:# y-axisreturng.scale(x=-1)reps=[(p,p.translate(x=2*(x-p.x)))forping.atoms(Point)]else:ifnothasattr(g,'reflect')andnotall(isinstance(arg,Point)forarging.args):raiseNotImplementedError('reflect undefined or non-Point args in %s'%g)a=atan(l.slope)c=l.coefficientsd=-c[-1]/c[1]# y-intercept# apply the transform to a single pointx,y=Dummy(),Dummy()xf=Point(x,y)xf=xf.translate(y=-d).rotate(-a,o).scale(y=-1).rotate(a,o).translate(y=d)# replace every point using that transformreps=[(p,xf.xreplace({x:p.x,y:p.y}))forping.atoms(Point)]returng.xreplace(dict(reps))

[docs]defis_similar(self,other):"""Is this geometrical entity similar to another geometrical entity? Two entities are similar if a uniform scaling (enlarging or shrinking) of one of the entities will allow one to obtain the other. Notes ===== This method is not intended to be used directly but rather through the `are_similar` function found in util.py. An entity is not required to implement this method. If two different types of entities can be similar, it is only required that one of them be able to determine this. See Also ======== scale """raiseNotImplementedError()

def__ne__(self,o):"""Test inequality of two geometrical entities."""returnnotself.__eq__(o)def__radd__(self,a):returna.__add__(self)def__rsub__(self,a):returna.__sub__(self)def__rmul__(self,a):returna.__mul__(self)def__rdiv__(self,a):returna.__div__(self)def__str__(self):"""String representation of a GeometryEntity."""fromsympy.printingimportsstrreturntype(self).__name__+sstr(self.args)def__repr__(self):"""String representation of a GeometryEntity that can be evaluated by sympy."""returntype(self).__name__+repr(self.args)def__cmp__(self,other):"""Comparison of two GeometryEntities."""n1=self.__class__.__name__n2=other.__class__.__name__c=(n1>n2)-(n1<n2)ifnotc:return0i1=-1forclsinself.__class__.__mro__:try:i1=ordering_of_classes.index(cls.__name__)breakexceptValueError:i1=-1ifi1==-1:returnci2=-1forclsinother.__class__.__mro__:try:i2=ordering_of_classes.index(cls.__name__)breakexceptValueError:i2=-1ifi2==-1:returncreturn(i1>i2)-(i1<i2)def__contains__(self,other):"""Subclasses should implement this method for anything more complex than equality."""iftype(self)==type(other):returnself==otherraiseNotImplementedError()def_eval_subs(self,old,new):fromsympy.geometry.pointimportPointifis_sequence(old)oris_sequence(new):old=Point(old)new=Point(new)returnself._subs(old,new)

deftranslate(x,y):"""Return the matrix to translate a 2-D point by x and y."""rv=eye(3)rv[2,0]=xrv[2,1]=yreturnrvdefscale(x,y,pt=None):"""Return the matrix to multiply a 2-D point's coordinates by x and y. If pt is given, the scaling is done relative to that point."""rv=eye(3)rv[0,0]=xrv[1,1]=yifpt:fromsympy.geometry.pointimportPointpt=Point(pt)tr1=translate(*(-pt).args)tr2=translate(*pt.args)returntr1*rv*tr2returnrvdefrotate(th):"""Return the matrix to rotate a 2-D point about the origin by ``angle``. The angle is measured in radians. To Point a point about a point other then the origin, translate the Point, do the rotation, and translate it back: >>> from sympy.geometry.entity import rotate, translate >>> from sympy import Point, pi >>> rot_about_11 = translate(-1, -1)*rotate(pi/2)*translate(1, 1) >>> Point(1, 1).transform(rot_about_11) Point(1, 1) >>> Point(0, 0).transform(rot_about_11) Point(2, 0) """s=sin(th)rv=eye(3)*cos(th)rv[0,1]=srv[1,0]=-srv[2,2]=1returnrv