![]() ![]() args area += x1 * y2 - x2 * y1 return simplify ( area ) / 2 def _isright ( a, b, c ): ba = b - a ca = c - a t_area = simplify ( ba. args for i in range ( len ( args )): x1, y1 = args. See Also = .area Examples = > from sympy import Point, Polygon > p1, p2, p3, p4 = map(Point, ) > poly = Polygon(p1, p2, p3, p4) > poly.area 3 """ area = 0 args = self. Notes = The area calculation can be positive or negative based on the orientation of the points. ![]() args ): raise GeometryError ( "Polygon has intersecting sides." ) return rv def area ( self ): """ The area of the polygon. # But we are about to raise an error anyway so it # won't matter too much. intersection ( sj ) if not hit : continue hit = hit # don't complain unless the intersection is definite # if there are symbols present then the intersection # might not occur this may not be necessary since if # the convex test passed, this will likely pass, too. args # exclude the sides connected to si for j in range ( 1 if i = len ( sides ) - 1 else 0, i - 1 ): sj = sides if sj. sides for i, si in enumerate ( sides ): pts = si. ![]() is_convex () except ValueError : convex = True if not convex : sides = rv. # A self-intersecting polygon is easier to detect than a # random set of segments since only those sides that are not # part of the convex hull can possibly intersect with other # sides of the polygon.but for now we use the n**2 algorithm # and check if any side intersects with any preceding side, # excluding the ones it is connected to try : convex = rv. _new_ ( cls, * vertices, ** kwargs ) elif len ( vertices ) = 3 : return Triangle ( * vertices, ** kwargs ) elif len ( vertices ) = 2 : return Segment ( * vertices, ** kwargs ) else : return Point ( * vertices, ** kwargs ) # reject polygons that have intersecting sides unless the # intersection is a shared point or a generalized intersection. pop ( i ) else : i += 1 vertices = list ( nodup ) if len ( vertices ) > 3 : rv = GeometryEntity. ![]() add ( p ) i = - 3 while i 2 : a, b, c = nodup, nodup, nodup if b not in shared and Point. pop () # last point was same as first # remove collinear points unless they are shared points got = set () shared = set () for p in nodup : if p in got : shared. append ( p ) if len ( nodup ) > 1 and nodup = nodup : nodup. insert ( 2, n ) return RegularPolygon ( * args, ** kwargs ) vertices = # remove consecutive duplicates nodup = for p in vertices : if nodup and p = nodup : continue nodup. append ( n ) elif len ( args ) = 3 : # center, radius, rotation args. pop ( 'n' ) args = list ( args ) # return a virtual polygon with n sides if len ( args ) = 2 : # center, radius args. Its method `spin` can be used to increment that angle. The unrotated RegularPolygon will always have a vertex at Point(r, 0) where `r` is the radius of the circle that circumscribes the RegularPolygon. For example, a polygon shaped like a Z with the top left connecting to the bottom right of the Z must have the point in the middle of the Z explicitly given: > mid = Point(1, 1) > Polygon((0, 2), (2, 2), mid, (0, 0), (2, 0), mid).area 0 > Polygon((0, 2), (2, 2), mid, (2, 0), (0, 0), mid).area -2 When the 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. Examples = > from sympy import Point, Polygon, pi > p1, p2, p3, p4, p5 = > Polygon(p1, p2, p3, p4) Polygon(Point2D(0, 0), Point2D(1, 0), Point2D(5, 1), Point2D(0, 1)) > Polygon(p1, p2) Segment(Point2D(0, 0), Point2D(1, 0)) > Polygon(p1, p2, p5) Segment(Point2D(0, 0), Point2D(3, 0)) While the sides of a polygon are not allowed to cross implicitly, they can do so explicitly. A Triangle, Segment or Point will be returned when there are 3 or fewer points provided. 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). See Also =, , 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. Parameters = vertices : sequence of Points Attributes = area angles perimeter vertices centroid sides Raises = GeometryError If all parameters are not Points. Can be constructed from a sequence of points or from a center, radius, number of sides and rotation angle. Class Polygon ( GeometrySet ): """A two-dimensional polygon. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |