本文整理汇总了Python中polygon.Polygon.create_random方法的典型用法代码示例。如果您正苦于以下问题:Python Polygon.create_random方法的具体用法?Python Polygon.create_random怎么用?Python Polygon.create_random使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类polygon.Polygon
的用法示例。
在下文中一共展示了Polygon.create_random方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_random
# 需要导入模块: from polygon import Polygon [as 别名]
# 或者: from polygon.Polygon import create_random [as 别名]
def create_random(self, n):
""" Create n polygons with random vertices. The dimensions of each
picture should be specified with max_x and may_y.
"""
self.polygons = []
for i in xrange(0, n):
p = Polygon()
# 3 to 5 vertices
Polygon.create_random(p, random.randint(3,5))
self.polygons.append(p)
return self
示例2: mutate
# 需要导入模块: from polygon import Polygon [as 别名]
# 或者: from polygon.Polygon import create_random [as 别名]
def mutate(self):
""" Create a new picture by mutating a polygon, adding a polygon,
or removing a polygon.
Return a new picture
"""
new_picture = Picture()
# Buffer changes so we iterate correctly
to_add = []
for polygon in self.polygons:
mutation_type = random.random()
if (mutation_type < 0.1):
# Stay the same
new_polygon = Polygon()
new_polygon.vertices = list(polygon.vertices)
to_add.append(new_polygon)
if (mutation_type < 0.8):
# Do some mutation
to_add.append(polygon.mutate())
elif (mutation_type >= 0.8 and mutation_type < 0.9):
# Add a polygon
new_polygon = Polygon()
new_polygon = new_polygon.create_random(3)
to_add.append(new_polygon)
elif (mutation_type >= 0.9):
# Remove a polygon by not adding it to the new picture
if (len(self.polygons) <= 1):
# Mutate instead if there is only one polygon left
to_add.append(polygon.mutate())
else:
pass
# Add polygons here so that we don't iterate over new polygons
for p in to_add:
new_picture.polygons.append(p)
return new_picture