K ik@ddlmZmZmZmZmZddlmZddlm Z m Z ddl m Z m Z mZddlmZddlmZddlmZmZmZdd lmZdd lmZmZdd lmZdd lmZm Z m!Z!dd l"m#Z#ddl$m%Z%ddl&m'Z'ddl(m)Z)ddl*m+Z+ddl,m-Z-m.Z.m/Z/m0Z0m1Z1ddl2m3Z3m4Z4ddl5m6Z6ddl7Z7e8dDcgc] }e ddc}\Z9Z:Z;GddeZ<Gdde<Z=Gdde<Z>d Z?d!Z@d"ZAd#ZBd$ZCd%ZDycc}w)&)ExprSoopisympify)N)default_sort_keyordered)_symbolDummySymbol)sign) Piecewise)cossintan)Circle)GeometryEntity GeometrySet) GeometryError)LineSegmentRay)Point)And)Matrix)simplify)solve)has_dups has_varietyuniq rotate_leftleast_rotation)as_int func_name) prec_to_dpsN polygon_dummyTrealc$eZdZdZdZdddZedZedZ edZ ed Z ed Z ed Z ed Zd!dZd!dZdZd!dZedZedZdZdZd"dZdZd"dZdZdZdZdZd#dZdZdZ d!d Z!y )$Polygona A two-dimensional polygon. A simple polygon in space. Can be constructed from a sequence of points or from a center, radius, number of sides and rotation angle. Parameters ========== vertices A sequence of points. n : int, optional If $> 0$, an n-sided RegularPolygon is created. Default value is $0$. Attributes ========== area angles perimeter vertices centroid sides Raises ====== GeometryError If all parameters are not Points. See Also ======== sympy.geometry.point.Point, sympy.geometry.line.Segment, Triangle Notes ===== Polygons are treated as closed paths rather than 2D areas so some calculations can be be negative or positive (e.g., area) based on the orientation of the points. Any consecutive identical points are reduced to a single point and any points collinear and between two points will be removed unless they are needed to define an explicit intersection (see examples). A Triangle, Segment or Point will be returned when there are 3 or fewer points provided. Examples ======== >>> from sympy import Polygon, pi >>> p1, p2, p3, p4, p5 = [(0, 0), (1, 0), (5, 1), (0, 1), (3, 0)] >>> Polygon(p1, p2, p3, p4) Polygon(Point2D(0, 0), Point2D(1, 0), Point2D(5, 1), Point2D(0, 1)) >>> Polygon(p1, p2) Segment2D(Point2D(0, 0), Point2D(1, 0)) >>> Polygon(p1, p2, p5) Segment2D(Point2D(0, 0), Point2D(3, 0)) The area of a polygon is calculated as positive when vertices are traversed in a ccw direction. When the sides of a polygon cross the area will have positive and negative contributions. The following defines a Z shape where the bottom right connects back to the top left. >>> Polygon((0, 2), (2, 2), (0, 0), (2, 0)).area 0 When the keyword `n` is used to define the number of sides of the Polygon then a RegularPolygon is created and the other arguments are interpreted as center, radius and rotation. The unrotated RegularPolygon will always have a vertex at Point(r, 0) where `r` is the radius of the circle that circumscribes the RegularPolygon. Its method `spin` can be used to increment that angle. >>> p = Polygon((0,0), 1, n=3) >>> p RegularPolygon(Point2D(0, 0), 1, 3, 0) >>> p.vertices[0] Point2D(1, 0) >>> p.args[0] Point2D(0, 0) >>> p.spin(pi/2) >>> p.vertices[0] Point2D(0, 1) r)nc|rVt|}t|dk(r|j|n t|dk(r|jd|t |i|S|Dcgc]}t |fddi|}}g}|D]}|r ||dk(r|j| t|dkDr|d|dk(r|j d}|t|dz krt|dkDr|||||dz||dz} } }t j|| | r+|j |dz|| k(r|j |n|dz }|t|dz krt|dkDr|t|}t|dkDrtj|g|i|St|dk(r t|i|St|dk(r t|i|St |i|Scc}w)Nr(dimrr) listlenappendinsertRegularPolygonrpop is_collinearr__new__Triangler) clsr/argskwargsaverticesnoduppibcs \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/geometry/polygon.pyr<zPolygon.__new__zs :D4yA~ ATa Aq!!4262 27;eBi583 IIK #e*q. SZ!^Aha!e eAEl!qA!!!Q* !a% 6IIaLQ#e*q. SZ!^; x=1 !))#CCFC C ]a X00 0 ]a H// /(-f- -?=sGcd}|j}tt|D]7}||dz j\}}||j\}}|||z||zz z }9t|dz S)a The area of the polygon. Notes ===== The area calculation can be positive or negative based on the orientation of the points. If any side of the polygon crosses any other side, there will be areas having opposite signs. See Also ======== sympy.geometry.ellipse.Ellipse.area Examples ======== >>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.area 3 In the Z shaped polygon (with the lower right connecting back to the upper left) the areas cancel out: >>> Z = Polygon((0, 1), (1, 1), (0, 0), (1, 0)) >>> Z.area 0 In the M shaped polygon, areas do not cancel because no side crosses any other (though there is a point of contact). >>> M = Polygon((0, 0), (0, 1), (2, 0), (3, 1), (3, 0)) >>> M.area -3/2 rrr1)r?ranger6r)selfarear?rEx1y1x2y2s rHrLz Polygon.areaszRyys4y! "A!a%[%%FB!W\\FB BrEBrEM !D "~!!c||z }||z }t|j|jz|j|jzz }|j}| t d|S)a7Return True/False for cw/ccw orientation. Examples ======== >>> from sympy import Point, Polygon >>> a, b, c = [Point(i) for i in [(0, 0), (1, 1), (1, 0)]] >>> Polygon._is_clockwise(a, b, c) True >>> Polygon._is_clockwise(a, c, b) False zCan't determine orientation)rxyis_nonpositive ValueError)rArFrGbacat_arearess rH _is_clockwisezPolygon._is_clockwises^U U"$$rtt)bdd244i/0## ;:; ; rQc|j}t|}i}t|D]n}||dz ||dz ||}}}t||j t||}|j |||rdt jz|z ||<j|||<pt|jt jz dz |dz z dz j} | r'dt jz} |D] }| ||z ||<|S| td|S)aThe internal angle at each vertex. Returns ======= angles : dict A dictionary where each key is a vertex and each value is the internal angle at that vertex. The vertices are represented as Points. See Also ======== sympy.geometry.point.Point, sympy.geometry.line.LinearEntity.angle_between Examples ======== >>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.angles[p1] pi/2 >>> poly.angles[p2] acos(-4*sqrt(17)/17) r1rz(could not determine Polygon orientation.) rBr6rJr angle_betweenr[rPisumvalues is_positiverV) rKr?r/retrErArFrG reflex_angwrongtwo_pis rHangleszPolygon.angless<}} Iq $A1q5k4A;Q!qAQ00Q;J!!!Q*144*,A#A  $cjjl#ADD(*QU3a7DD qttVF )#a&A ) ]GH H rQc4|jdjS)Nr)rBambient_dimensionrKs rHrhzPolygon.ambient_dimension s}}Q111rQcd}|j}tt|D]}|||dz j||z }!t |S)aThe perimeter of the polygon. Returns ======= perimeter : number or Basic instance See Also ======== sympy.geometry.line.Segment.length Examples ======== >>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.perimeter sqrt(17) + 7 rr)rBrJr6distancer)rKrDr?rEs rH perimeterzPolygon.perimeter$sU. }}s4y! /A a!e%%d1g. .A /{rQc,t|jS)aQThe vertices of the polygon. Returns ======= vertices : list of Points Notes ===== When iterating over the vertices, it is more efficient to index self rather than to request the vertices and index them. Only use the vertices when you want to process all of them at once. This is even more important with RegularPolygons that calculate each vertex. See Also ======== sympy.geometry.point.Point Examples ======== >>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.vertices [Point2D(0, 0), Point2D(1, 0), Point2D(5, 1), Point2D(0, 1)] >>> poly.vertices[0] Point2D(0, 0) )r5r?ris rHrBzPolygon.verticesAsDDIIrQcRdd|jzz }d\}}|j}tt|D]J}||dz j\}}||j\}} || z||zz } || ||zzz }|| || zzz }Lt t ||zt ||zS)aThe centroid of the polygon. Returns ======= centroid : Point See Also ======== sympy.geometry.point.Point, sympy.geometry.util.centroid Examples ======== >>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.centroid Point2D(31/18, 11/18) rrr)rLr?rJr6rr) rKAcxcyr?rErMrNrOrPvs rHcentroidzPolygon.centroides0 q{OByys4y! A!a%[%%FB!W\\FB22 A !R"W+ B !R"W+ B   Xad^Xad^44rQNcd\}}}|j}tt|D]}||dz j\}}||j\} } || z| |zz } ||dz|| zz| dzz| zz }||dz|| zz| dzz| zz }||| zd|z|zzd| z| zz| |zz| zz }|j} |j d} |j d}|dz | |dzzz }|dz | | dzzz }|dz | | |zzz }||||fS|| |d|z dzzz}|| |d| z dzzz}|| |d| z |d|z zzz}|||fS)aReturns the second moment and product moment of area of a two dimensional polygon. Parameters ========== point : Point, two-tuple of sympifyable objects, or None(default=None) point is the point about which second moment of area is to be found. If "point=None" it will be calculated about the axis passing through the centroid of the polygon. Returns ======= I_xx, I_yy, I_xy : number or SymPy expression I_xx, I_yy are second moment of area of a two dimensional polygon. I_xy is product moment of area of a two dimensional polygon. Examples ======== >>> from sympy import Polygon, symbols >>> a, b = symbols('a, b') >>> p1, p2, p3, p4, p5 = [(0, 0), (a, 0), (a, b), (0, b), (a/3, b/3)] >>> rectangle = Polygon(p1, p2, p3, p4) >>> rectangle.second_moment_of_area() (a*b**3/12, a**3*b/12, 0) >>> rectangle.second_moment_of_area(p5) (a*b**3/9, a**3*b/9, a**2*b**2/36) References ========== .. [1] https://en.wikipedia.org/wiki/Second_moment_of_area )rrrrr1r )rBrJr6r?rLru)rKpointI_xxI_yyI_xyr?rErMrNrOrPrtrqc_xc_yI_xx_cI_yy_cI_xy_cs rHsecond_moment_of_areazPolygon.second_moment_of_areasJ#dD}}s4y! :A!A#Y^^FB!W\\FB22 A RURU]RU*A- -D RURU]RU*A- -D RUQrT"W_qtBw.B69 9D  : IImmAmmAr'aaj)r'aaj)r'aSk* =66) )U1Xc\A-..U1Xc\A-..U1Xc\E!HSL9::T4rQc |r|j\}}n|j}|\}}t|d}t|tj}|j |}|j |}|dj |dj kr|dn|d}|dj |dj kr|dn|d} |jj |z |j z} | jj|z | j z} | | fS)a Returns the first moment of area of a two-dimensional polygon with respect to a certain point of interest. First moment of area is a measure of the distribution of the area of a polygon in relation to an axis. The first moment of area of the entire polygon about its own centroid is always zero. Therefore, here it is calculated for an area, above or below a certain point of interest, that makes up a smaller portion of the polygon. This area is bounded by the point of interest and the extreme end (top or bottom) of the polygon. The first moment for this area is is then determined about the centroidal axis of the initial polygon. References ========== .. [1] https://skyciv.com/docs/tutorials/section-tutorials/calculating-the-statical-or-first-moment-of-area-of-beam-sections/?cc=BMD .. [2] https://mechanicalc.com/reference/cross-sections Parameters ========== point: Point, two-tuple of sympifyable objects, or None (default=None) point is the point above or below which the area of interest lies If ``point=None`` then the centroid acts as the point of interest. Returns ======= Q_x, Q_y: number or SymPy expressions Q_x is the first moment of area about the x-axis Q_y is the first moment of area about the y-axis A negative sign indicates that the section modulus is determined for a section below (or left of) the centroidal axis Examples ======== >>> from sympy import Point, Polygon >>> a, b = 50, 10 >>> p1, p2, p3, p4 = [(0, b), (0, 0), (a, 0), (a, b)] >>> p = Polygon(p1, p2, p3, p4) >>> p.first_moment_of_area() (625, 3125) >>> p.first_moment_of_area(point=Point(30, 7)) (525, 3000) rsloper)rurrInfinity cut_sectionrLrTrS) rKryxcych_linev_lineh_polyv_polypoly_1poly_2Q_xQ_ys rHfirst_moment_of_areazPolygon.first_moment_of_areas` ]]FBMMEFBe1%e1::.!!&)!!&)$Qinnq >F1I$Qinnq >F1I  2%v{{2  2%v{{2CxrQc8|j}|d|dzS)aReturns the polar modulus of a two-dimensional polygon It is a constituent of the second moment of area, linked through the perpendicular axis theorem. While the planar second moment of area describes an object's resistance to deflection (bending) when subjected to a force applied to a plane parallel to the central axis, the polar second moment of area describes an object's resistance to deflection when subjected to a moment applied in a plane perpendicular to the object's central axis (i.e. parallel to the cross-section) Examples ======== >>> from sympy import Polygon, symbols >>> a, b = symbols('a, b') >>> rectangle = Polygon((0, 0), (a, 0), (a, b), (0, b)) >>> rectangle.polar_second_moment_of_area() a**3*b/12 + a*b**3/12 References ========== .. [1] https://en.wikipedia.org/wiki/Polar_moment_of_inertia rr)r)rK second_moments rHpolar_second_moment_of_areaz#Polygon.polar_second_moment_of_area s'6224 Q-"222rQc|j\}}|6|j\}}}}t||z ||z }t||z ||z } n|j|z }|j|z } |j } | d|z } | d| z } | | fS)aReturns a tuple with the section modulus of a two-dimensional polygon. Section modulus is a geometric property of a polygon defined as the ratio of second moment of area to the distance of the extreme end of the polygon from the centroidal axis. Parameters ========== point : Point, two-tuple of sympifyable objects, or None(default=None) point is the point at which section modulus is to be found. If "point=None" it will be calculated for the point farthest from the centroidal axis of the polygon. Returns ======= S_x, S_y: numbers or SymPy expressions S_x is the section modulus with respect to the x-axis S_y is the section modulus with respect to the y-axis A negative sign indicates that the section modulus is determined for a point below the centroidal axis Examples ======== >>> from sympy import symbols, Polygon, Point >>> a, b = symbols('a, b', positive=True) >>> rectangle = Polygon((0, 0), (a, 0), (a, b), (0, b)) >>> rectangle.section_modulus() (a*b**2/6, a**2*b/6) >>> rectangle.section_modulus(Point(a/4, b/4)) (-a*b**2/3, -a**2*b/3) References ========== .. [1] https://en.wikipedia.org/wiki/Section_modulus rr)ruboundsmaxrTrSr) rKryx_cy_cx_miny_minx_maxy_maxrTrSrS_xS_ys rHsection_moduluszPolygon.section_modulus,sT==S =)- &E5%C%K-AC%K-A# A# A113 Aq Aq CxrQc g}|j}tt| dD]&}|jt ||||dz(|S)aThe directed line segments that form the sides of the polygon. Returns ======= sides : list of sides Each side is a directed Segment. See Also ======== sympy.geometry.point.Point, sympy.geometry.line.Segment Examples ======== >>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.sides [Segment2D(Point2D(0, 0), Point2D(1, 0)), Segment2D(Point2D(1, 0), Point2D(5, 1)), Segment2D(Point2D(5, 1), Point2D(0, 1)), Segment2D(Point2D(0, 1), Point2D(0, 0))] rr)rBrJr6r7r)rKrZr?rEs rHsidesz Polygon.sideshsR6}}D z1% 6A JJwtAwQU 4 5 6 rQc|j}|Dcgc]}|j}}|Dcgc]}|j}}t|t|t |t |fScc}wcc}w)zwReturn a tuple (xmin, ymin, xmax, ymax) representing the bounding rectangle for the geometric figure. )rBrSrTminr)rKvertsrDxsyss rHrzPolygon.boundssb  !aacc ! ! !aacc ! !BR#b'3r733" !s A)A.c|j}|j|d|d|d}tdt|D])}||j||dz ||dz ||z s)y|j}t |D]q\}}|j }t|t|dz k(rdnd|dz D];}||}|j|vs|j|vs&|j|} | s:ysy)a_Is the polygon convex? A polygon is convex if all its interior angles are less than 180 degrees and there are no intersections between sides. Returns ======= is_convex : boolean True if this polygon is convex, False otherwise. See Also ======== sympy.geometry.util.convex_hull Examples ======== >>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly = Polygon(p1, p2, p3, p4) >>> poly.is_convex() True r3rrr1FT) rBr[rJr6r enumerater?p1p2 intersection) rKr?cwrErsiptsjsjhits rH is_convexzPolygon.is_convexs8}}   R$r(DG <q#d)$ AD&&tAE{DQKaII  u% %EAr''CSZ!^ 31AEB %1X55#S(8//"-C$  % %rQcrtd|jvstfd|jDryg}|jD]'}|j |z |dj s'yt |}|j}ttt| d}|jr{d}|D]s}||} ||dz} | j | j| jz z| j | j| jz zz j} || }n| |ussyyd} |d j\} }|ddD]i}||j\}}d t||kDrBd t!||kr3d t!| |kr$||k7r| || z z||z z | z}| |k(sd |kr| } ||}} k| S) a4 Return True if p is enclosed by (is inside of) self. Notes ===== Being on the border of self is considered False. Parameters ========== p : Point Returns ======= encloses_point : True, False or None See Also ======== sympy.geometry.point.Point, sympy.geometry.ellipse.Ellipse.encloses_point Examples ======== >>> from sympy import Polygon, Point >>> p = Polygon((0, 0), (4, 0), (4, 4)) >>> p.encloses_point(Point(2, 1)) True >>> p.encloses_point(Point(2, 2)) False >>> p.encloses_point(Point(5, 5)) False References ========== .. [1] https://paulbourke.net/geometry/polygonmesh/#insidepoly r1r2c3&K|]}|v ywNr.).0srDs rH z)Polygon.encloses_point..s$@Q!V$@Fr3NrTr)rrBanyrr7 free_symbolsr-r?r5rJr6rrTrS is_negativerr)rKrDlitrtpolyr?indices orientationrErArFtesthit_oddp1xp1yp2xp2yxinterss ` rHencloses_pointzPolygon.encloses_pointsT !O  $@TZZ$@!@ A JJq1u 2w##  } yyuc$iZ+, >> K !GQK##acc *qssdQSS133Y-??LL&"&K,  !7<<S AAw||HC3sC= C %CSM)#:(+tcCi&8#)&Ds&JG"czQ'\.5+CC rQc t|d}|jd|jDvrtd|jzg}|j}d}|j D]b}|j |z }||z}|j|j|||z |z } |j| t||k||kf|}dt|S)a+A parameterized point on the polygon. The parameter, varying from 0 to 1, assigns points to the position on the perimeter that is that fraction of the total perimeter. So the point evaluated at t=1/2 would return the point from the first vertex that is 1/2 way around the polygon. Parameters ========== parameter : str, optional Default value is 't'. Returns ======= arbitrary_point : Point Raises ====== ValueError When `parameter` already appears in the Polygon's definition. See Also ======== sympy.geometry.point.Point Examples ======== >>> from sympy import Polygon, Symbol >>> t = Symbol('t', real=True) >>> tri = Polygon((0, 0), (1, 0), (1, 1)) >>> p = tri.arbitrary_point('t') >>> perimeter = tri.perimeter >>> s1, s2 = [s.length for s in tri.sides[:2]] >>> p.subs(t, (s1 + s2/2)/perimeter) Point2D(1, 1/2) Tr*c34K|]}|jywr)name)rfs rHrz*Polygon.arbitrary_point..Fs8aff8zFSymbol %s already appears in object and cannot be used as a parameter.r) r rrrVrlrlengtharbitrary_pointsubsr7rr) rK parametertrrlperim_fraction_startrside_perim_fractionperim_fraction_endpts rHrzPolygon.arbitrary_pointsV ID ) 668d&7&78 8ehihnhnno oNN   6A"#((9"4 !58K!K ""9-22A,,.AACB LLc.!3Q9K5KLN P#5  6%  rQct|tst||j}t|ts t d|j r t dd}|jt}|jD]S\}}t||z td}|s|dt}t|jt|dk(r||icSd}U|rt dt|zt d t|z) Nrzother must be a pointznon-numeric coordinatesFT)dictrzGiven point may not be on %szGiven point is not on %s) isinstancerrrhrVrNotImplementedErrorrTr?rrrr&) rKotherrunknownrDrcondsolvalues rHparameter_valuezPolygon.parameter_valueUs%/%T%;%;>> from sympy import Polygon >>> p = Polygon((0, 0), (1, 0), (1, 1)) >>> p.plot_interval() [t, 0, 1] Tr*rr)r )rKrrs rH plot_intervalzPolygon.plot_intervaljs0 94 (1ayrQc ng}t|tr |jn|g}|jD])}|D]"}|j|j |$+t t |}|Dcgc]}t|ts|}}|Dcgc]}t|ts|}}|rc|rat t |D cgc]} |D] } | | vs|  c} } } | r| D]} |j| t t||zSt t|Scc}wcc}wcc} } w)aThe intersection of polygon and geometry entity. The intersection may be empty and can contain individual Points and complete Line Segments. Parameters ========== other: GeometryEntity Returns ======= intersection : list The list of Segments and Points See Also ======== sympy.geometry.point.Point, sympy.geometry.line.Segment Examples ======== >>> from sympy import Point, Polygon, Line >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly1 = Polygon(p1, p2, p3, p4) >>> p5, p6, p7 = map(Point, [(3, 2), (1, -1), (0, 2)]) >>> poly2 = Polygon(p5, p6, p7) >>> poly1.intersection(poly2) [Point2D(1/3, 1), Point2D(2/3, 0), Point2D(9/5, 1/5), Point2D(7/3, 1)] >>> poly1.intersection(Line(p1, p2)) [Segment2D(Point2D(0, 0), Point2D(1, 0))] >>> poly1.intersection(p1) [Point2D(0, 0)] ) rr-rextendrr5r"rrremover ) rKointersection_resultksideside1entitypointssegmentsrysegmentpoints_in_segmentsrEs rHrzPolygon.intersections6J!!!W-AGGA3JJ ED E#**4+<+W&XX)<\v 6SZ@[F\\ h!%dv+reU]+r'afjqaqE+rE+r&s!t !+%AMM!$%6 123 3 345 5Y\,ss$2D'D'D,(D,D1D1c|j|}|s tdt|j}|j |d|j t t}|jt }|jt}g}g}d} d} |D]} |r4|jt | j t| ji|z n"|jt | j |z } | dkDr[| sEt| | } | j|}|j |d|j |d|j | d} n\| rG| rEt| | } | j|}|j |d|j |d|j | d} | } d\}}|rtt|trt|}|rtt|trt|}||fS)aL Returns a tuple of two polygon segments that lie above and below the intersecting line respectively. Parameters ========== line: Line object of geometry module line which cuts the Polygon. The part of the Polygon that lies above and below this line is returned. Returns ======= upper_polygon, lower_polygon: Polygon objects or None upper_polygon is the polygon that lies above the given line. lower_polygon is the polygon that lies below the given line. upper_polygon and lower polygon are ``None`` when no polygon exists above the line or below the line. Raises ====== ValueError: When the line does not intersect the polygon Examples ======== >>> from sympy import Polygon, Line >>> a, b = 20, 10 >>> p1, p2, p3, p4 = [(0, b), (0, 0), (a, 0), (a, b)] >>> rectangle = Polygon(p1, p2, p3, p4) >>> t = rectangle.cut_section(Line((0, 5), slope=0)) >>> t (Polygon(Point2D(0, 10), Point2D(0, 5), Point2D(20, 5), Point2D(20, 10)), Polygon(Point2D(0, 5), Point2D(0, 0), Point2D(20, 0), Point2D(20, 5))) >>> upper_segment, lower_segment = t >>> upper_segment.area 100 >>> upper_segment.centroid Point2D(10, 15/2) >>> lower_segment.centroid Point2D(10, 5/2) References ========== .. [1] https://github.com/sympy/sympy/wiki/A-method-to-return-a-cut-section-of-any-polygon-geometry z(This line does not intersect the polygonrTNF)NN) rrVr5rBr7equationrSrTcoeffrrrr-)rKlineintersection_pointsreqrArFupper_verticeslower_verticesprev prev_pointrycompareedge new_point upper_polygon lower_polygons rHrzPolygon.cut_sectionsf#//5"GH Hdmm$ fQi ]]1a  HHQK HHQK  E>?bggq%''1egg679EGG,Q. { z2D $ 1 1$ 7I")))A,7")))A,7%%e,Jz2D $ 1 1$ 7I")))A,7")))A,7%%e,J5 8(2$ } j.)A7K#^4M j.)A7K#^4Mm++rQcJt|trIt}|jD]2}|j |}|dk(rt j cS||ks1|}4|St|tr1|jr!|jr|j|St)a Returns the shortest distance between self and o. If o is a point, then self does not need to be convex. If o is another polygon self and o must be convex. Examples ======== >>> from sympy import Point, Polygon, RegularPolygon >>> p1, p2 = map(Point, [(0, 0), (7, 5)]) >>> poly = Polygon(*RegularPolygon(p1, 1, 3).vertices) >>> poly.distance(p2) sqrt(61) r) rrrrrkrZeror-r_do_poly_distancer)rKrdistrcurrents rHrkzPolygon.distance(s a D  #--*a<66Mt^"D  # K 7 #(8Q[[]))!, ,!##rQc |} |j}|j}tj}tj}|jD] }t j ||}||ks|}"|jD] }t j ||}||ks|}"t j ||} | ||zkrt jdd t dt } t dt} |jD]Q}|j| jkDs4|j| jk(s6|j| jkDsP|} S|jD]Q}|j| jks4|j| jk(s6|j| jksP|} St j | | } i} i}|jD]}|j| vr)| |jj|jn|jg| |j<|j| vr)| |jj|j|jg| |j<|jD]}|j|vr)||jj|jn|jg||j<|j|vr)||jj|j|jg||j<| }| }tt tjtjt tj tj} | | d}| | d}|j#t| |}|j#t| |}||kr|}n:||kr|}n2t j | |t j | |kDr|}n|}|| d}|| d}|j#t| |}|j#t| |}||kDr|}n:||kDr|}n2t j | |t j | |kDr|}n|} |j#t||}t$|j#t||z }||kdurot||}t'||}|j |}|j)| j)kr|} | |d|k7r |}| |d}n5|}| |d}n)||kDdurmt||}t'||}|j |}|j)| j)kr|} ||d|k7r |}||d}n|}||d}nt||}t'||}t'||}|j |}|j |}t+||}|j)| j)kr|} | |d|k7r |}| |d}n |}| |d}||d|k7r |}||d}n |}||d}|| k(r|| k(r | S)a Calculates the least distance between the exteriors of two convex polygons e1 and e2. Does not check for the convexity of the polygons as this is checked by Polygon.distance. Notes ===== - Prints a warning if the two polygons possibly intersect as the return value will not be valid in such a case. For a more through test of intersection use intersection(). See Also ======== sympy.geometry.point.Point.distance Examples ======== >>> from sympy import Point, Polygon >>> square = Polygon(Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)) >>> triangle = Polygon(Point(1, 2), Point(2, 2), Point(2, 1)) >>> square._do_poly_distance(triangle) sqrt(2)/2 Description of method used ========================== Method: [1] https://web.archive.org/web/20150509035744/http://cgm.cs.mcgill.ca/~orm/mind2p.html Uses rotating calipers: [2] https://en.wikipedia.org/wiki/Rotating_calipers and antipodal points: [3] https://en.wikipedia.org/wiki/Antipodal_point z1Polygons may intersect producing erroneous outputr() stacklevelrrT)rurrrBrrkwarningswarnrrTrSrrr7rrOner]rrevalfr) rKe2e1 e1_center e2_center e1_max_radius e2_max_radiusvertexr center_diste1_ymaxe2_yminmin_diste1_connectionse2_connectionsr e1_current e2_current support_linepoint1point2angle1angle2e1_nexte2_nexte1_anglee2_angle e1_segmentmin_dist_current e2_segmentmin1min2s rHrzPolygon._do_poly_distanceEs[JZKK KK   kk "Fy&1Aq !  "kk "Fy&1Aq !  "nnY : --7 7 MMM%& ( B3-2,kk !Fxx'))#GII(=&((WYYBV  !kk !Fxx'))#GII(=&((WYYBV  !>>'73 HH 4Dww.(tww'..tww7+/77)tww'ww.(tww'..tww7+/77)tww' 4HH 4Dww.(tww'..tww7+/77)tww'ww.(tww'..tww7+/77)tww' 4  E!&&!&&153GH   (+(+++D&,AB++D&,AB F?G f_G ^^GV ,u~~gv/N NGG(+(+++D&,AB++D&,AB F?G f_G ^^GV ,u~~gv/N NGG #11$z72KLHL66tG8%&&H8#,#J8 $Z9 #-#6#6z#B #))+hnn.>>/H!'*1-;!(J,W5a8G!(J,W5a8GX%$.#GZ8 $Z9 #-#6#6z#B #))+hnn.>>/H!'*1-;!(J,W5a8G!(J,W5a8G#J8 $Z9 $Z9 !**73!**73#&tT? #))+hnn.>>/H!'*1-;!(J,W5a8G!(J,W5a8G!'*1-;!(J,W5a8G!(J,W5a8GW$w)>wrQc tt|j}|Dcgc](}dj|j|j *}}dj|ddj |dd}djd|z||Scc}w) a"Returns SVG path element for the Polygon. Parameters ========== scale_factor : float Multiplication factor for the SVG stroke-width. Default is 1. fill_color : str, optional Hex string for fill color. Default is "#66cc99". z{},{}z M {} L {} zrz L rNzag@)maprrBformatrSrTjoin)rK scale_factor fill_colorrrDcoordspaths rH_svgz Polygon._svg sAt}}%49:q'..acc*::##F1Iuzz&*/EF :fR,&j9 :;s-B c* i fd}||j}t|t|}|tt |j}t|t|}||kr|}n|}|Dcgc]} | }}t |Scc}w)Nci}ttt|D]\}}|||<||<|Dcgc]}|| c}Scc}wr)rr set) point_listkeerErDDs rHref_listz+Polygon._hashable_content..ref_list#sUC!'#j/":; 1A! %//qCF/ //s A)r?r#r$r5reversedtuple) rKr<S1r_norS2r_revrordercanonical_argsr;s @rH_hashable_contentzPolygon._hashable_content s  0dii Br 23 d8DII./ 0Br 23 5=AA1241U844^$$5s7 Bcttr|k(Sttrtfd|jDStt r&|j vry|jD]}|vsyy)a Return True if o is contained within the boundary lines of self.altitudes Parameters ========== other : GeometryEntity Returns ======= contained in : bool The points (and sides, if applicable) are contained in self. See Also ======== sympy.geometry.entity.GeometryEntity.encloses Examples ======== >>> from sympy import Line, Segment, Point >>> p = Point(0, 0) >>> q = Point(1, 1) >>> s = Segment(p, q*2) >>> l = Line(p, q) >>> p in q False >>> p in s True >>> q*3 in s False >>> s in l True c3&K|]}|v ywrr.)rrrs rHrz'Polygon.__contains__.._s2!qAv2rTF)rr-rrrrrB)rKrrs ` rH __contains__zPolygon.__contains__5stN a !19  7 #2tzz22 2 5 !DMM!  9 rQc i}t|j}|j|dtj|dd}|rtt |}|j jD]\}}|j|}tj||||dz\}} t|| j|dz |} | j} t| j| j| | jdz z} |/t| j| j j#|} | ||<|S)aReturns angle bisectors of a polygon. If prec is given then approximate the point defining the ray to that precision. The distance between the points defining the bisector ray is 1. Examples ======== >>> from sympy import Polygon, Point >>> p = Polygon(Point(0, 0), Point(2, 0), Point(1, 1), Point(0, 3)) >>> p.bisectors(2) {Point2D(0, 0): Ray2D(Point2D(0, 0), Point2D(0.71, 0.71)), Point2D(0, 3): Ray2D(Point2D(0, 3), Point2D(0.23, 2.0)), Point2D(1, 1): Ray2D(Point2D(1, 1), Point2D(0.19, 0.42)), Point2D(2, 0): Ray2D(Point2D(2, 0), Point2D(1.1, 0.38))} rNr(rr1rp)r5r?r7r-r[r=rfitemsindexr_normalize_dimensionrrotate directionrrkrr/) rDprecrFrrrtrArErrraydirs rH bisectorszPolygon.bisectorsis" 166l 3q6  " "CG , x}%CHHNN$ DAq ! A//AAE CFBb"+$$QqS!,C--Ccffcffs3<<+?'??@C#&&#&&((4.1AaD rQr)r)g?z#66cc99)"__name__ __module__ __qualname____doc__ __slots__r<propertyrL staticmethodr[rfrhrlrBrurrrrrrrrrrrrrrkrr5rErHrRr.rQrHr-r-s6YvI !).V."."`*11f228!!F 5 5F< ~BJ3>9x@ 4 4,\Un9!vG*666rg,T$:FP:&%*2h rQr-cneZdZdZdZddZddZedZdZ dZ edZ ed Z ed Z e Zed Zed Zed ZedZedZedZedZedZedZedZedZdZdZd dZd!dZdZedZdZ fdZ!xZ"S)"r9a A regular polygon. Such a polygon has all internal angles equal and all sides the same length. Parameters ========== center : Point radius : number or Basic instance The distance from the center to a vertex n : int The number of sides Attributes ========== vertices center radius rotation apothem interior_angle exterior_angle circumcircle incircle angles Raises ====== GeometryError If the `center` is not a Point, or the `radius` is not a number or Basic instance, or the number of sides, `n`, is less than three. Notes ===== A RegularPolygon can be instantiated with Polygon with the kwarg n. Regular polygons are instantiated with a center, radius, number of sides and a rotation angle. Whereas the arguments of a Polygon are vertices, the vertices of the RegularPolygon must be obtained with the vertices method. See Also ======== sympy.geometry.point.Point, Polygon Examples ======== >>> from sympy import RegularPolygon, Point >>> r = RegularPolygon(Point(0, 0), 5, 3) >>> r RegularPolygon(Point2D(0, 0), 5, 3, 0) >>> r.vertices[0] Point2D(5, 0) )_n_center_radius_rotc tt|||f\}}}t|fddi|}t|tst d|z|j rt||dkrt d|ztj||||fi|}||_ ||_ ||_ |jr |dtjz|z z|_|S||_|S)Nr2r1z r must be an Expr object, not %sr(zn must be a >= 3, not %s)r.rrrrr is_Numberr%rr<r[r\r] is_numberrr^r^)rKrGrr/rotr@objs rHr<zRegularPolygon.__new__s!Q- 1c ! % %f %!T" BQ FG G ;; 1I1u#$>$BCC$$T1a=f=  '*}}3!ADD&(# ;> rQc |j\}}}}t|}|||fDcgc]}|jdd|i|c}\}}}|j||||Scc}w)Nr/r.)r?r'rfunc) rKrOoptionsrGrr/rAdpsrEs rH _eval_evalfzRegularPolygon._eval_evalfsgYY 1a$78!Qi@7177,S,G,@1ayyAq!$$AsAc^|j|j|j|jfS)a- Returns the center point, the radius, the number of sides, and the orientation angle. Examples ======== >>> from sympy import RegularPolygon, Point >>> r = RegularPolygon(Point(0, 0), 5, 3) >>> r.args (Point2D(0, 0), 5, 3, 0) )r\r]r[r^ris rHr?zRegularPolygon.argss#||T\\477DII==rQc2dt|jzSNzRegularPolygon(%s, %s, %s, %s)r>r?ris rH__str__zRegularPolygon.__str__/% 2BBBrQc2dt|jzSrkrlris rH__repr__zRegularPolygon.__repr__rnrQc|j\}}}}t||z|jdzzdtt|z zz S)zReturns the area. Examples ======== >>> from sympy import RegularPolygon >>> square = RegularPolygon((0, 0), 1, 4) >>> square.area 2 >>> _ == square.length**2 True r1)r?rrrr)rKrGrr/rbs rHrLzRegularPolygon.areasAyy 1aAwqya'3r!t955rQcZ|jdztt|jz zS)aReturns the length of the sides. The half-length of the side and the apothem form two legs of a right triangle whose hypotenuse is the radius of the regular polygon. Examples ======== >>> from sympy import RegularPolygon >>> from sympy import sqrt >>> s = square_in_unit_circle = RegularPolygon((0, 0), 1, 4) >>> s.length sqrt(2) >>> sqrt((_/2)**2 + s.apothem**2) == s.radius True r1)radiusrrr[ris rHrzRegularPolygon.length s#({{1}SDGG_,,rQc|jS)aThe center of the RegularPolygon This is also the center of the circumscribing circle. Returns ======= center : Point See Also ======== sympy.geometry.point.Point, sympy.geometry.ellipse.Ellipse.center Examples ======== >>> from sympy import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 5, 4) >>> rp.center Point2D(0, 0) )r\ris rHcenterzRegularPolygon.center s0||rQc|jS)z Alias for center. Examples ======== >>> from sympy import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 5, 4) >>> rp.circumcenter Point2D(0, 0) )rvris rH circumcenterzRegularPolygon.circumcenter<s{{rQc|jS)aRadius of the RegularPolygon This is also the radius of the circumscribing circle. Returns ======= radius : number or instance of Basic See Also ======== sympy.geometry.line.Segment.length, sympy.geometry.ellipse.Circle.radius Examples ======== >>> from sympy import Symbol >>> from sympy import RegularPolygon, Point >>> radius = Symbol('r') >>> rp = RegularPolygon(Point(0, 0), radius, 4) >>> rp.radius r )r]ris rHrtzRegularPolygon.radiusKs6||rQc|jS)a Alias for radius. Examples ======== >>> from sympy import Symbol >>> from sympy import RegularPolygon, Point >>> radius = Symbol('r') >>> rp = RegularPolygon(Point(0, 0), radius, 4) >>> rp.circumradius r )rtris rH circumradiuszRegularPolygon.circumradiushs{{rQc|jS)a3CCW angle by which the RegularPolygon is rotated Returns ======= rotation : number or instance of Basic Examples ======== >>> from sympy import pi >>> from sympy.abc import a >>> from sympy import RegularPolygon, Point >>> RegularPolygon(Point(0, 0), 3, 4, pi/4).rotation pi/4 Numerical rotation angles are made canonical: >>> RegularPolygon(Point(0, 0), 3, 4, a).rotation a >>> RegularPolygon(Point(0, 0), 3, 4, pi).rotation 0 r^ris rHrotationzRegularPolygon.rotationys4yyrQch|jttj|jz zS)a8The inradius of the RegularPolygon. The apothem/inradius is the radius of the inscribed circle. Returns ======= apothem : number or instance of Basic See Also ======== sympy.geometry.line.Segment.length, sympy.geometry.ellipse.Circle.radius Examples ======== >>> from sympy import Symbol >>> from sympy import RegularPolygon, Point >>> radius = Symbol('r') >>> rp = RegularPolygon(Point(0, 0), radius, 4) >>> rp.apothem sqrt(2)*r/2 )rtrrr^r[ris rHapothemzRegularPolygon.apothems$6{{Sdgg...rQc|jS)a& Alias for apothem. Examples ======== >>> from sympy import Symbol >>> from sympy import RegularPolygon, Point >>> radius = Symbol('r') >>> rp = RegularPolygon(Point(0, 0), radius, 4) >>> rp.inradius sqrt(2)*r/2 )rris rHinradiuszRegularPolygon.inradiuss||rQc\|jdz tjz|jz S)a~Measure of the interior angles. Returns ======= interior_angle : number See Also ======== sympy.geometry.line.LinearEntity.angle_between Examples ======== >>> from sympy import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 4, 8) >>> rp.interior_angle 3*pi/4 r1)r[rr^ris rHinterior_anglezRegularPolygon.interior_angles$.! QTT!$''))rQcBdtjz|jz S)a|Measure of the exterior angles. Returns ======= exterior_angle : number See Also ======== sympy.geometry.line.LinearEntity.angle_between Examples ======== >>> from sympy import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 4, 8) >>> rp.exterior_angle pi/4 r1)rr^r[ris rHexterior_anglezRegularPolygon.exterior_angles.vdgg~rQcBt|j|jS)aThe circumcircle of the RegularPolygon. Returns ======= circumcircle : Circle See Also ======== circumcenter, sympy.geometry.ellipse.Circle Examples ======== >>> from sympy import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 4, 8) >>> rp.circumcircle Circle(Point2D(0, 0), 4) )rrvrtris rH circumcirclezRegularPolygon.circumcircles.dkk4;;//rQcBt|j|jS)aThe incircle of the RegularPolygon. Returns ======= incircle : Circle See Also ======== inradius, sympy.geometry.ellipse.Circle Examples ======== >>> from sympy import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 4, 7) >>> rp.incircle Circle(Point2D(0, 0), 4*cos(pi/7)) )rrvrris rHincirclezRegularPolygon.incircles.dkk4<<00rQcNi}|j}|jD]}|||< |S)a Returns a dictionary with keys, the vertices of the Polygon, and values, the interior angle at each vertex. Examples ======== >>> from sympy import RegularPolygon, Point >>> r = RegularPolygon(Point(0, 0), 5, 3) >>> r.angles {Point2D(-5/2, -5*sqrt(3)/2): pi/3, Point2D(-5/2, 5*sqrt(3)/2): pi/3, Point2D(5, 0): pi/3} )rrB)rKrbangrts rHrfzRegularPolygon.angles's5 !! ACF  rQc|j}t||j}||jk\ry||jkryt j ||S)aN Return True if p is enclosed by (is inside of) self. Notes ===== Being on the border of self is considered False. The general Polygon.encloses_point method is called only if a point is not within or beyond the incircle or circumcircle, respectively. Parameters ========== p : Point Returns ======= encloses_point : True, False or None See Also ======== sympy.geometry.ellipse.Ellipse.encloses_point Examples ======== >>> from sympy import RegularPolygon, S, Point, Symbol >>> p = RegularPolygon((0, 0), 3, 4) >>> p.encloses_point(Point(0, 0)) True >>> r, R = p.inradius, p.circumradius >>> p.encloses_point(Point((r + R)/2, 0)) True >>> p.encloses_point(Point(R/2, R/2 + (R - r)/10)) False >>> t = Symbol('t', real=True) >>> p.encloses_point(p.arbitrary_point().subs(t, S.Half)) False >>> p.encloses_point(Point(5, 5)) False FT)rvrrrtrr-r)rKrDrGds rHrzRegularPolygon.encloses_point=sR` KK AqM     ))$2 2rQc.|xj|z c_y)aIncrement *in place* the virtual Polygon's rotation by ccw angle. See also: rotate method which moves the center. >>> from sympy import Polygon, Point, pi >>> r = Polygon(Point(0,0), 1, n=3) >>> r.vertices[0] Point2D(1, 0) >>> r.spin(pi/6) >>> r.vertices[0] Point2D(sqrt(3)/2, 1/2) See Also ======== rotation rotate : Creates a copy of the RegularPolygon rotated about a Point Nr})rKangles rHspinzRegularPolygon.spinws( U rQct||j}|xj|z c_tj|||S)aOverride GeometryEntity.rotate to first rotate the RegularPolygon about its center. >>> from sympy import Point, RegularPolygon, pi >>> t = RegularPolygon(Point(1, 0), 1, 3) >>> t.vertices[0] # vertex on x-axis Point2D(2, 0) >>> t.rotate(pi/2).vertices[0] # vertex on y axis now Point2D(0, 2) See Also ======== rotation spin : Rotates a RegularPolygon in place )typer?r^rrM)rKrrrs rHrMzRegularPolygon.rotates:& DJ " %$$Qr22rQcD|rNt|d}|j| jj||j|jS||k7r"t |j j||S|j\}}}}||z}|j ||||S)aOverride GeometryEntity.scale since it is the radius that must be scaled (if x == y) or else a new Polygon must be returned. >>> from sympy import RegularPolygon Symmetric scaling returns a RegularPolygon: >>> RegularPolygon((0, 0), 1, 4).scale(2, 2) RegularPolygon(Point2D(0, 0), 2, 4, 0) Asymmetric scaling returns a kite as a Polygon: >>> RegularPolygon((0, 0), 1, 4).scale(2, 1) Polygon(Point2D(2, 0), Point2D(0, 1), Point2D(-2, 0), Point2D(0, -1)) r1r)r translater?scaler-rBre)rKrSrTrrGrr/rbs rHrzRegularPolygon.scales" rq!BD>4>>RC::.44Q:DDbggN N 6DMM*00A6 6yy 1a QyyAq#&&rQc |j\}}}}|jd}||z }|j|}|j|} | |z } td| } td|} | j | } || z }|j || ||S)a5Override GeometryEntity.reflect since this is not made of only points. Examples ======== >>> from sympy import RegularPolygon, Line >>> RegularPolygon((0, 0), 1, 4).reflect(Line((0, 1), slope=-2)) RegularPolygon(Point2D(4/5, 2/5), -1, 4, atan(4/3)) rrp)r?rBreflectr closing_anglere)rKrrGrr/rbrtrccvvddl1l2rs rHrzRegularPolygon.reflectsyy 1a MM!  E YYt_ YYt_ "W_ ^r" s yyaRC((rQc ||j}t|j}|j}dtj z|j z }t|j Dcgc]L}t|j|t||z|zzz|j|t||z|zzzNc}Scc}w)aThe vertices of the RegularPolygon. Returns ======= vertices : list Each vertex is a Point. See Also ======== sympy.geometry.point.Point Examples ======== >>> from sympy import RegularPolygon, Point >>> rp = RegularPolygon(Point(0, 0), 5, 4) >>> rp.vertices [Point2D(5, 0), Point2D(0, 5), Point2D(-5, 0), Point2D(0, -5)] r1) r\absr]r^rr^r[rJrrSrrTr)rKrGrrbrtrs rHrBzRegularPolygon.verticess0 LL  ii addF477Ntww)accAc!A#)n,,accAc!A#)n4D.DE) ))s%AB9ct|tsyt|tstj||S|j|jk(S)NF)rr-r9__eq__r?)rKrs rHrzRegularPolygon.__eq__s=!W%A~.>>!T* *yyAFF""rQc t|Sr)super__hash__)rK __class__s rHrzRegularPolygon.__hash__sw!!rQ)r)r)rrN)#rSrTrUrVrWr<rhrXr?rmrprLrrvrurxrtr{r~rrrrrrrfrrrMrrrBrr __classcell__)rs@rHr9r9s;z5I"%  > >CC66 --*2H   8 6//8 **00000110*83t,3.'4)8))>#""rQr9c*eZdZdZdZedZdZdZdZ dZ dZ ed Z ed Z ed Zed Zed ZdZedZedZedZedZedZedZedZedZedZy)r=a A polygon with three vertices and three sides. Parameters ========== points : sequence of Points keyword: asa, sas, or sss to specify sides/angles of the triangle Attributes ========== vertices altitudes orthocenter circumcenter circumradius circumcircle inradius incircle exradii medians medial nine_point_circle Raises ====== GeometryError If the number of vertices is not equal to three, or one of the vertices is not a Point, or a valid keyword is not given. See Also ======== sympy.geometry.point.Point, Polygon Examples ======== >>> from sympy import Triangle, Point >>> Triangle(Point(0, 0), Point(4, 0), Point(4, 3)) Triangle(Point2D(0, 0), Point2D(4, 0), Point2D(4, 3)) Keywords sss, sas, or asa can be used to give the desired side lengths (in order) and interior angles (in degrees) that define the triangle: >>> Triangle(sss=(3, 4, 5)) Triangle(Point2D(0, 0), Point2D(3, 0), Point2D(3, 4)) >>> Triangle(asa=(30, 1, 30)) Triangle(Point2D(0, 0), Point2D(1, 0), Point2D(1/2, sqrt(3)/6)) >>> Triangle(sas=(1, 45, 2)) Triangle(Point2D(0, 0), Point2D(2, 0), Point2D(sqrt(2)/2, sqrt(2)/2)) c"t|dk7rd|vr"t|dDcgc] }t|c}Sd|vr"t|dDcgc] }t|c}Sd|vr"t |dDcgc] }t|c}Sd}t ||Dcgc]}t |fddi|}}g}|D]}|r ||dk(r|j| t|d kDr|d|d k(r|jd }|t|dz krt|dkDrt||||d z||dzgt \}} } t j|| | r!|||<d||d z<|j|d z|d z }|t|dz krt|dkDrttd |}t|dk(rtj|g|i|St|dk(r t!|i|St |i|Scc}wcc}wcc}wcc}w)Nr(sssasasasz;Triangle instantiates with three points or a valid keyword.r2r1r3rrr4)keyc |duSrr.)rSs rHz"Triangle.__new__..`s $rQ)r6_sssr_asa_sasrrr7r:sortedr r;r5filterrr<r) r>r?r@rAmsgrBrCrDrErFrGs rHr<zTriangle.__new__?s- t9>6%=AahqkABB6%=AahqkABB6%=AahqkABBOC$ $7;eBi583 IIK #e*q. SZ!^q5Q<q1u6? x=A !))#CCFC C ]a H// /(-f- -KBAA=sG=H+HH c|jS)aThe triangle's vertices Returns ======= vertices : tuple Each element in the tuple is a Point See Also ======== sympy.geometry.point.Point Examples ======== >>> from sympy import Triangle, Point >>> t = Triangle(Point(0, 0), Point(4, 0), Point(4, 3)) >>> t.vertices (Point2D(0, 0), Point2D(4, 0), Point2D(4, 3)) )r?ris rHrBzTriangle.verticesis0yyrQclt|tsy|jDcgc]}|jc}\}}}|jDcgc]}|j}}d}||||g|xs?||||g|xs2||||g|xs%||||g|xs||||g|xs ||||g|Scc}wcc}w)aIs another triangle similar to this one. Two triangles are similar if one can be uniformly scaled to the other. Parameters ========== other: Triangle Returns ======= is_similar : boolean See Also ======== sympy.geometry.entity.GeometryEntity.is_similar Examples ======== >>> from sympy import Triangle, Point >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3)) >>> t2 = Triangle(Point(0, 0), Point(-4, 0), Point(-4, -3)) >>> t1.is_similar(t2) True >>> t2 = Triangle(Point(0, 0), Point(-4, 0), Point(-4, -4)) >>> t1.is_similar(t2) False Fct||z }t||z }t||z }t||k(xrt||k(Sr)rbool) u1u2u3v1v2v3rre3s rH _are_similarz)Triangle.is_similar.._are_similarsD"R%B"R%B"R%Bb>4d28n 4rQ)rr-rr)t1t2rs1_1s1_2s1_3s2rs rH is_similarzTriangle.is_similarsD"g&46HH=DDKK=dD&(hh /ddkk / / 5D$2r20 tT /B /0 tT /B /0 tT /B /0 tT /B / 0 tT /B /  0> /s B,B1c<td|jD S)abAre all the sides the same length? Returns ======= is_equilateral : boolean See Also ======== sympy.geometry.entity.GeometryEntity.is_similar, RegularPolygon is_isosceles, is_right, is_scalene Examples ======== >>> from sympy import Triangle, Point >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3)) >>> t1.is_equilateral() False >>> from sympy import sqrt >>> t2 = Triangle(Point(0, 0), Point(10, 0), Point(5, 5*sqrt(3))) >>> t2.is_equilateral() True c34K|]}|jywrrrrs rHrz*Triangle.is_equilateral..s>> from sympy import Triangle, Point >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(2, 4)) >>> t1.is_isosceles() True c34K|]}|jywrrrs rHrz(Triangle.is_isosceles..s5Q5rr rris rH is_isosceleszTriangle.is_isosceless,5$**555rQc<td|jD S)aAre all the sides of the triangle of different lengths? Returns ======= is_scalene : boolean See Also ======== is_equilateral, is_isosceles, is_right Examples ======== >>> from sympy import Triangle, Point >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(1, 4)) >>> t1.is_scalene() True c34K|]}|jywrrrs rHrz&Triangle.is_scalene.. s9AHH9rrris rH is_scalenezTriangle.is_scalenes,9djj9999rQc|j}tj|d|dxs:tj|d|dxstj|d|dS)aIs the triangle right-angled. Returns ======= is_right : boolean See Also ======== sympy.geometry.line.LinearEntity.is_perpendicular is_equilateral, is_isosceles, is_scalene Examples ======== >>> from sympy import Triangle, Point >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3)) >>> t1.is_right() True rrr1)rris_perpendicularrKrs rHis_rightzTriangle.is_right sd. JJ''!ad31  $ $QqT1Q4 01  $ $QqT1Q4 0 1rQc |j}|j}|d|dj|d|d|dj|d|d|dj|diS)a The altitudes of the triangle. An altitude of a triangle is a segment through a vertex, perpendicular to the opposite side, with length being the height of the vertex measured from the line containing the side. Returns ======= altitudes : dict The dictionary consists of keys which are vertices and values which are Segments. See Also ======== sympy.geometry.point.Point, sympy.geometry.line.Segment.length Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.altitudes[p1] Segment2D(Point2D(0, 0), Point2D(1/2, 1/2)) rrr1)rrBperpendicular_segmentrKrrts rH altitudeszTriangle.altitudes# st< JJ MM!ad0016!ad0016!ad00168 8rQc|j}|j}t||djt||ddS)aThe orthocenter of the triangle. The orthocenter is the intersection of the altitudes of a triangle. It may lie inside, outside or on the triangle. Returns ======= orthocenter : Point See Also ======== sympy.geometry.point.Point Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.orthocenter Point2D(0, 0) rr)rrBrr)rKrArts rH orthocenterzTriangle.orthocenterG sD6 NN MMAadG}))$q1w-8;;rQc|jDcgc]}|jc}\}}}|j|dScc}w)aThe circumcenter of the triangle The circumcenter is the center of the circumcircle. Returns ======= circumcenter : Point See Also ======== sympy.geometry.point.Point Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.circumcenter Point2D(1/2, 1/2) r)rperpendicular_bisectorr)rKrSrArFrGs rHrxzTriangle.circumcenterf sA28>> from sympy import Symbol >>> from sympy import Point, Triangle >>> a = Symbol('a') >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, a) >>> t = Triangle(p1, p2, p3) >>> t.circumradius sqrt(a**2/4 + 1/4) r)rrkrxrBris rHr{zTriangle.circumradius s$2~~d//q1ABBrQcBt|j|jS)aThe circle which passes through the three vertices of the triangle. Returns ======= circumcircle : Circle See Also ======== sympy.geometry.ellipse.Circle Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.circumcircle Circle(Point2D(1/2, 1/2), sqrt(2)/2) )rrxr{ris rHrzTriangle.circumcircle s0d''):):;;rQc|jDcgc] }t|}}|j}|j}t |dt|d|j |dd}t |dt|d|j |dd}t |dt|d|j |dd}|d||d||d|iScc}w)aThe angle bisectors of the triangle. An angle bisector of a triangle is a straight line through a vertex which cuts the corresponding angle in half. Returns ======= bisectors : dict Each key is a vertex (Point) and each value is the corresponding bisector (Segment). See Also ======== sympy.geometry.point.Point, sympy.geometry.line.Segment Examples ======== >>> from sympy import Point, Triangle, Segment >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> from sympy import sqrt >>> t.bisectors()[p2] == Segment(Point(1, 0), Point(0, sqrt(2) - 1)) True rrr1)rrrBincenterrr)rKlrrtrGrrl3s rHrRzTriangle.bisectors s@#jj )T!W ) ) MM MM QqT4!a=55ad;A> ? QqT4!a=55ad;A> ? QqT4!a=55ad;A> ?!b!A$AaD"-- *sCc |j}tdDcgc]}||jc}}t|}|j}t |j t|Dcgc]}|jc}|z }t |j t|Dcgc]}|jc}|z }t||Scc}wcc}wcc}w)aThe center of the incircle. The incircle is the circle which lies inside the triangle and touches all three sides. Returns ======= incenter : Point See Also ======== incircle, sympy.geometry.point.Point Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.incenter Point2D(1 - sqrt(2)/2, 1 - sqrt(2)/2) )rr1r) rrrr_rBrdotrSrTr) rKrrErrDrtvirSrTs rHrzTriangle.incenter s6 JJ 3AAaDKK3 4 F MM QUU6!"4B244"456q8 9 QUU6!"4B244"456q8 9Q{ 4#5"4sC#C!CcLtd|jz|jz S)aThe radius of the incircle. Returns ======= inradius : number of Basic instance See Also ======== incircle, sympy.geometry.ellipse.Circle.radius Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(4, 0), Point(0, 3) >>> t = Triangle(p1, p2, p3) >>> t.inradius 1 r1)rrLrlris rHrzTriangle.inradius s 0DII 677rQcBt|j|jS)a!The incircle of the triangle. The incircle is the circle which lies inside the triangle and touches all three sides. Returns ======= incircle : Circle See Also ======== sympy.geometry.ellipse.Circle Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(2, 0), Point(0, 2) >>> t = Triangle(p1, p2, p3) >>> t.incircle Circle(Point2D(2 - sqrt(2), 2 - sqrt(2)), 2 - sqrt(2)) )rrrris rHrzTriangle.incircle s6dmmT]]33rQc ^|j}|dj}|dj}|dj}||z|zdz }|j}|jdt|||z z |jdt|||z z |jdt|||z z i}|S)aoThe radius of excircles of a triangle. An excircle of the triangle is a circle lying outside the triangle, tangent to one of its sides and tangent to the extensions of the other two. Returns ======= exradii : dict See Also ======== sympy.geometry.polygon.Triangle.inradius Examples ======== The exradius touches the side of the triangle to which it is keyed, e.g. the exradius touching side 2 is: >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(6, 0), Point(0, 2) >>> t = Triangle(p1, p2, p3) >>> t.exradii[t.sides[2]] -2 + sqrt(10) References ========== .. [1] https://mathworld.wolfram.com/Exradius.html .. [2] https://mathworld.wolfram.com/Excircles.html rrr1)rrrLr)rKrrArFrGrrLexradiis rHrzTriangle.exradii9 sLzz GNN GNN GNN qSUAIyy::a=(41:"6::a=(41:"6::a=(41:"68rQc |j}|j}|dj}|dj}|dj}|dj|dj|djg}|dj|dj|djg}t | |dz||dzz||dz| |z|zz zt ||dz||dzz ||dz||z |zz zt ||dz||dzz||dz||z|z z z t | |dz||dzz||dz| |z|zz zt ||dz||dzz ||dz||z |zz zt ||dz||dzz||dz||z|z z z d}|dt |d|d|dt |d|d|dt |d |d i} | S) asExcenters of the triangle. An excenter is the center of a circle that is tangent to a side of the triangle and the extensions of the other two sides. Returns ======= excenters : dict Examples ======== The excenters are keyed to the side of the triangle to which their corresponding excircle is tangent: The center is keyed, e.g. the excenter of a circle touching side 0 is: >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(6, 0), Point(0, 2) >>> t = Triangle(p1, p2, p3) >>> t.excenters[t.sides[0]] Point2D(12*sqrt(10), 2/3 + sqrt(10)/3) See Also ======== sympy.geometry.polygon.Triangle.exradii References ========== .. [1] https://mathworld.wolfram.com/Excircles.html rrr1)rMrOx3rNrPy3rMrNrOrPrr)rrBrrSrTrr) rKrrtrArFrGrSrT exc_coords excenterss rHrzTriangle.excentersk s4L JJ MM aDKK aDKK aDKK qTVVQqTVVQqTVV $ qTVVQqTVVQqTVV $A2ad71QqT6>!AaD&1"Q$q&/9:1QqT6!AaD&=1Q41Q781QqT6!AaD&=1Q41Q78A2ad71QqT6>!AaD&1"Q$q&/9:1QqT6!AaD&=1Q41Q781QqT6!AaD&=1Q41Q78   aD% 4(*T*:; aD% 4(*T*:; aD% 4(*T*:; rQc |j}|j}|dt|d|dj|dt|d|dj|dt|d|djiS)aThe medians of the triangle. A median of a triangle is a straight line through a vertex and the midpoint of the opposite side, and divides the triangle into two equal areas. Returns ======= medians : dict Each key is a vertex (Point) and each value is the median (Segment) at that point. See Also ======== sympy.geometry.point.Point.midpoint, sympy.geometry.line.Segment.midpoint Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.medians[p1] Segment2D(Point2D(0, 0), Point2D(1/2, 1/2)) rrr1)rrBrmidpointrs rHmedianszTriangle.medians st< JJ MM!gadAaDMM2!gadAaDMM2!gadAaDMM24 4rQc|j}t|dj|dj|djS)aThe medial triangle of the triangle. The triangle which is formed from the midpoints of the three sides. Returns ======= medial : Triangle See Also ======== sympy.geometry.line.Segment.midpoint Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.medial Triangle(Point2D(1/2, 0), Point2D(1/2, 1/2), Point2D(0, 1/2)) rrr1)rr=rrs rHmedialzTriangle.medial s44 JJ! qt}}admmDDrQc:t|jjS)aThe nine-point circle of the triangle. Nine-point circle is the circumcircle of the medial triangle, which passes through the feet of altitudes and the middle points of segments connecting the vertices and the orthocenter. Returns ======= nine_point_circle : Circle See also ======== sympy.geometry.line.Segment.midpoint sympy.geometry.polygon.Triangle.medial sympy.geometry.polygon.Triangle.orthocenter Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.nine_point_circle Circle(Point2D(1/4, 1/4), sqrt(2)/4) )rrrBris rHnine_point_circlezTriangle.nine_point_circle s<t{{++,,rQcz|jr |jSt|j|jS)aThe Euler line of the triangle. The line which passes through circumcenter, centroid and orthocenter. Returns ======= eulerline : Line (or Point for equilateral triangles in which case all centers coincide) Examples ======== >>> from sympy import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.eulerline Line2D(Point2D(0, 0), Point2D(1/2, 1/2)) )rrrrxris rH eulerlinezTriangle.eulerline s5,    ## #D$$d&7&788rQN)rSrTrUrVr<rXrBrrrrrrrrxr{rrRrrrrrrrrrr.rQrHr=r=sc7r(.T240l=<60:018!8!8F<<<$$6CC4<<2&.P  D882448//b<<|!4!4FEE8-->99rQr=c|tzdz S)zAReturn the radian value for the given degrees (pi = 180 degrees).r)rs rHradr%  R48OrQc|tz dzS)zAReturn the degree value for the given radians (pi = 180 degrees).rr)rs rHdegr* rrQc.tt|}|Sr)rr)rrvs rH_sloper/ s SVB IrQc tdt|jt|dftd|z d}td|df|S)z8Return triangle having side with length l on the x-axis.rprrr)rrrr=)d1rd2xys rHrr4 sO fF2J ' 4 4 aV6#(+, ../ 1B FQFB ''rQctd|}t|df|}|j|Dcgc]}|jjs|}}|sy|d}t d|df|Scc}w)z7Return triangle having side of length l1 on the x-axis.rprN)rrrTis_nonnegativer=)rrrc1c2rAinterrs rHrr; sn  B Q B+ B1qss/A/AQ BE B  qB FRGR (( Cs A' A'ctdd}t|d}ttt||ztt||z}t |||S)z9Return triangle having side with length l2 on the x-axis.r)rrrrr=)rrrrrp3s rHrrF sK q!B r1B s3q6{2~s3q6{2~ .B BB rQ)E sympy.corerrrrrsympy.core.evalfrsympy.core.sortingr r sympy.core.symbolr r r $sympy.functions.elementary.complexesr$sympy.functions.elementary.piecewiser(sympy.functions.elementary.trigonometricrrrellipserrrr exceptionsrrrrrryr sympy.logicrsympy.matricesrsympy.simplify.simplifyrsympy.solvers.solversrsympy.utilities.iterablesr r!r"r#r$sympy.utilities.miscr%r&mpmath.libmp.libmpfr'r rJrSrTrr-r9r=rrrrrr)rEs0rHr"s//8445:BB/%$$!,'^^2+7