[英]Making random polygons using python
I am a very biginner, and making a code to make a random polygon by python. 我是一个非常重要的人,正在编写代码以通过python创建一个随机多边形。 I cannot find any error in the code but I cannot print all vertices but only print a pair of x, y coordinate of one vertice.
我在代码中找不到任何错误,但我无法打印所有顶点,而只打印一个顶点的一对x,y坐标。 Could someone let me know?
有人可以让我知道吗? I think map(tuple, verts) is something problometic.
我认为map(元组,verts)有点问题。
Thank you very much. 非常感谢你。
import math, random
def generatePolygon( ctrX, ctrY, aveRadius, irregularity, spikeyness, numVerts ) :
irregularity = clip( irregularity, 0,1 ) * 2*math.pi / numVerts
spikeyness = clip( spikeyness, 0,1 ) * aveRadius
# generate n angle steps
angleSteps = []
lower = (2*math.pi / numVerts) - irregularity
upper = (2*math.pi / numVerts) + irregularity
sum = 0
for i in range(numVerts) :
tmp = random.uniform(lower, upper)
angleSteps.append( tmp )
sum = sum + tmp
# normalize the steps so that point 0 and point n+1 are the same
k = sum / (2*math.pi)
for i in range(numVerts) :
angleSteps[i] = angleSteps[i] / k
# now generate the points
points = []
angle = random.uniform(0, 2*math.pi)
for i in range(numVerts) :
r_i = clip( random.gauss(aveRadius, spikeyness), 0, 2*aveRadius )
x = ctrX + r_i*math.cos(angle)
y = ctrY + r_i*math.sin(angle)
points.append(( int(x),int(y) ))
angle = angle + angleSteps[i]
return points
def clip(x, min, max) :
if( min > max ) : return x
elif( x < min ) : return min
elif( x > max ) : return max
else : return x
verts = generatePolygon( ctrX=250, ctrY=250, aveRadius=100, irregularity=0, spikeyness=0, numVerts=16 )
tupVerts = list(map(tuple,verts))
print(tupVerts)
You returned too soon from your function - after calculating one point. 计算出一点后,您从函数返回的时间过早。 Here's the problem:
这是问题所在:
for i in range(numVerts) :
r_i = clip( random.gauss(aveRadius, spikeyness), 0, 2*aveRadius )
x = ctrX + r_i*math.cos(angle)
y = ctrY + r_i*math.sin(angle)
points.append(( int(x),int(y) ))
angle = angle + angleSteps[i]
return points # <--- here
for i in range(numVerts) :
r_i = clip( random.gauss(aveRadius, spikeyness), 0, 2*aveRadius )
x = ctrX + r_i*math.cos(angle)
y = ctrY + r_i*math.sin(angle)
points.append(( int(x),int(y) ))
angle = angle + angleSteps[i]
return points # <--- Should look like this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.