파이썬 barnsley 고사리 그리기
조회수 1068회
barnsley 고사리 그리기 입니다... 이게 오류가 생기네요 왜 그런지 알려주세요 ㅠ
'''
barnsley 고사리 그리기
'''
import random
import matplotlib.pyplot as plt
def transformation_1(p):
x=p[0]
y=p[1]
x1 = 0.85*x + 0.04*y
y1 = -0.04*x+0.85*y+1.6
return x1, y1
def transformation_2(p):
x=p[0]
y=p[1]
x1 = 0.2*x - 0.26*y
y1 = 0.23*x + 0.22*y + 1.6
return x1, y1
def transformation_3(p):
x=p[0]
y=p[1]
x1 = 0.15*x - 0.28*y
y1 = 0.26*x + 0.24*y + 0.44
return x1, y1
def transformation_4(p):
x=p[0]
y=p[1]
x1 = 0
y1 = 0.16*y
return x1, y1
def get_index(probability):
r=random.random()
c_probablity = 0
sum_probability = 0
for p in probability:
c_probability+=p
sum_probability.append(c_probability)
for item, sp in enumerate(sum_probability):
if r <= sp :
return item
return len(probability)-1
def transform(p):
transformations = [transformation_1, transformation_2, transformation_3, transformation_4]
probability=[0.85, 0.07, 0.07, 0.01]
tindex=get_index(probability)
t=transformaions[tindex]
x, y = t(p)
return x, y
def draw_fern(n):
x=[0]
y=[0]
x1, y1 = 0, 0
for i in range(n):
x1, y1 = transform((x1, y1))
x.append(x1)
y.append(y1)
return x, y
if __name__=='__main__':
n=int(input('Enter the number of points in the Fern: '))
x, y=draw_fern(n)
plt.plot(x,y,'o')
plt.title('Fern with {0} points'.format(n))
plt.show()
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
def get_index(probability): r=random.random() c_probability = 0 ################################### typing miss sum_probability = [] ####################### wriong type (int->list) for p in probability: c_probability+=p sum_probability.append(c_probability) for item, sp in enumerate(sum_probability): if r <= sp : return item return len(probability)-1 def transform(p): transformations = [transformation_1, transformation_2, transformation_3, transformation_4] probability=[0.85, 0.07, 0.07, 0.01] tindex=get_index(probability) t=transformations[tindex] ################################### typing miss x, y = t(p) return x, y
변수 이름에서 실수가 있었고, 타입을 잘못 설정하셨습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력