A.
import queue as que import math x=[] y=[] def koch(ax,ay,bx,by,c): dist=(bx-ax)*(bx-ax)+(by-ay)*(by-ay) if(dist<=c): x.append(ax) x.append(bx) y.append(ay) y.append(by) else: cx=ax+(bx-ax)/3 cy=ay+(by-ay)/3 ex=bx-(bx-ax)/3 ey=by-(by-ay)/3 l=math.sqrt(math.pow(ex-cx,2)+pow(ey-cy,2)) alpha=math.atan((ey-cy)/(ex-cx)) if (alpha>=0 and (ex-cx)<0) or (alpha<=0 and (ex-cx)<0): alpha=alpha+math.pi dy=cy+math.sin(alpha+math.pi/3)*l dx=cx+math.cos(alpha+math.pi/3)*l koch(ax,ay,bx,by,c) koch(bx,by,cx,cy,c) koch(cx,cy,dx,dy,c) koch(dx,dy,ex,ey,c)
B.
import queue as que import math x=[] y=[] def koch(ax,ay,bx,by,c): dist=(bx-ax)*(bx-ax)+(by-ay)*(by-ay) if(dist<=c*c): x.append(ax) x.append(bx) y.append(ay) y.append(by) else: cx=ax+(bx-ax)/3 cy=ay+(by-ay)/3 ex=bx-(bx-ax)/3 ey=by-(by-ay)/3 l=math.sqrt(d/3) alpha=math.atan((ey-cy)/(ex-cx)) if (alpha>=0 and (ex-cx)<0) or (alpha<=0 and (ex-cx)<0): alpha=alpha+math.pi dy=cy+math.sin(alpha+math.pi/3)*l dx=cx+math.cos(alpha+math.pi/3)*l koch(ax,ay,cx,cy,c) koch(cx,cy,dx,dy,c) koch(cx,cy,ex,ey,c) koch(ex,ey,dx,dy,c)
C.
import queue as que import math import matplotlib.pyplot as plt x=[] y=[] def koch(ax,ay,bx,by,c): dist=(bx-ax)*(bx-ax)+(by-ay)*(by-ay) if(dist<=c*c): x.append(ax) x.append(bx) y.append(ay) y.append(by) else: cx=ax+(bx-ax)/3 cy=ay+(by-ay)/3 ex=bx-(bx-ax)/3 ey=by-(by-ay)/3 l=math.sqrt(math.pow(ex-cx,2)+pow(ey-cy,2)) alpha=math.atan((ey-cy)/(ex-cx)) if (alpha>=0 and (ex-cx)<0) or (alpha<=0 and (ex-cx)<0): alpha=alpha+math.pi dy=cy+math.sin(alpha+math.pi/3)*l dx=cx+math.cos(alpha+math.pi/3)*l koch(ax,ay,cx,cy,c) koch(cx,cy,dx,dy,c) koch(dx,dy,ex,ey,c) koch(ex,ey,bx,by,c)
D.
import queue as que import math x=[] y=[] def koch(ax,ay,bx,by,c): dist=(bx-ax)*(bx-ax)+(by-ay)*(by-ay) if(dist<=c*c): x.append(ax) x.append(bx) y.append(ay) y.append(by) else: cx=ax+(bx-ax)/3 cy=ay+(by-ay)/3 ex=bx-(bx-ax)/3 ey=by-(by-ay)/3 l=math.sqrt(d/3) alpha=math.atan((ey-cy)/(ex-cx)) if (alpha>=0 and (ex-cx)<0) or (alpha<=0 and (ex-cx)<0): alpha=alpha+math.pi dy=cy+math.sin(alpha+math.pi/3)*l dx=cx+math.cos(alpha+math.pi/3)*l koch(ax,ay,cx,cy,c) koch(ex,ey,dx,dy,c) koch(cx,cy,ex,ey,c) koch(dx,dy,bx,by,c)