Problem:
Koch Curve Write a program which reads an integer n and draws a Koch curve based on recursive calles of depth n . The Koch curve is well known as a kind of fractals . You can draw a Koch curve in the following algorithm:  Divide a given segment (p1, p2) into three equal segments.  Replace the middle segment by the two sides of an equilateral triangle (s, u, t) of the same length as the segment.  Repeat this procedure recursively for new segments (p1, s), (s, u), (u, t), (t, p2).    You should start (0, 0), (100, 0) as the first segment. Input An integer n is given. Output Print each point (x, y) of the Koch curve. Print a point in a line. You should start the point(0, 0), which is the endpoint of the first segment and end with the point (100, 0), the other endpoint so that you can draw the Koch curve as an unbroken line. Each solution should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10-4. Constraints  0 ≤ n ≤ 6  Sample Input 1 1 Sample Output 1 0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000 Sample Input 2 2 Sample Output 2 0.00000000 0.00000000
11.11111111 0.00000000
16.66666667 9.62250449
22.22222222 0.00000000
33.33333333 0.00000000
38.88888889 9.62250449
33.33333333 19.24500897
44.44444444 19.24500897
50.00000000 28.86751346
55.55555556 19.24500897
66.66666667 19.24500897
61.11111111 9.62250449
66.66666667 0.00000000
77.77777778 0.00000000
83.33333333 9.62250449
88.88888889 0.00000000
100.00000000 0.00000000 Notes

Solution (Python):
d=[0j,100+0j]
for _ in[0]*int(input()):
 p=[d[0]]
 for i in range(len(d)-1):
  a,b=d[i],d[i+1]
  r=(b-a)/3
  p+=[a+r,a+r+r*(1+3**.5*1j)/2,b-r,b]
 d=p
for e in d:print(e.real,e.imag)

Solution (Python):
d=[0j,100+0j]
for _ in[0]*int(input()):
 p=[d[0]]
 for i in range(len(d)-1):
  a,b=d[i],d[i+1]
  r=(b-a)/3
  p+=[a+r,a+r+r*(1+3**.5*1j)/2,b-r,b]
 d=p
for e in d:print(e.real,e.imag)

Solution (Python):



import sys
import math

DEG60 = math.pi/3

def repl_edge(n, edges):
    
    if n == 0:
        return edges
    new_list = list()
    for edge in edges:  
        p1 = edge[0]
        p2 = edge[1]
        p_angle = edge[2]
        s = ((p1[0]*2+p2[0])/3, (p1[1]*2+p2[1])/3)
        t = ((p1[0]+p2[0]*2)/3, (p1[1]+p2[1]*2)/3)
        r = abs((t[0]-s[0])**2 + (t[1]-s[1])**2) ** 0.5
        angle = p_angle + DEG60
        u = (s[0] + math.cos(angle)*r, s[1] + math.sin(angle)*r)
        
        new_list += [(p1, s, p_angle), (s, u, p_angle+DEG60), \
                    (u, t, p_angle-DEG60), (t, p2, p_angle)]

    new_list = repl_edge(n-1, new_list)
    return new_list


n = int(sys.stdin.readline())
p1 = (0.0, 0.0)
p2 = (100.0, 0.0)
edges = [(p1, p2, 0.0)]

edges = repl_edge(n, edges)
for edge in edges:
    print(round(edge[0][0],6), round(edge[0][1],6))
print(round(edges[-1][1][0],6), round(edges[-1][1][1],6))

Solution (Python):
import math
angle = math.pi / 3 

def koch(p1, p2, n):
    if n == 0:
        return

    s = ((p1[0] * 2 + p2[0]) / 3, (p1[1] * 2 + p2[1]) / 3)
    t = ((p1[0] + p2[0] * 2) / 3, (p1[1] + p2[1] * 2) / 3)
    u = ((t[0] - s[0]) * math.cos(angle) - (t[1] - s[1]) * math.sin(angle) + s[0], \
        (t[0] - s[0]) * math.sin(angle) + (t[1] - s[1]) * math.cos(angle) + s[1])

    koch(p1, s, n-1)
    print("{0:.8f} {1:.8f}".format(s[0], s[1]))
    koch(s, u, n-1)
    print("{0:.8f} {1:.8f}".format(u[0], u[1]))
    koch(u, t, n-1)
    print("{0:.8f} {1:.8f}".format(t[0], t[1]))
    koch(t, p2, n-1)

n = int(input())
print("{0:.8f} {1:.8f}".format(0.0, 0.0))
koch((0.0, 0.0), (100.0, 0.0), n)
print("{0:.8f} {1:.8f}".format(100.0, 0.0))

Solution (Python):
import math


def koch(p1, p2, count):
    """Find points in koch curve after count times operations.

    >>> ps = koch((0, 0), (100, 0), 1)
    >>> cs = ["({:.4f}, {:.4f})".format(*p) for p in ps]
    >>> print("\\n".join(cs))
    (0.0000, 0.0000)
    (33.3333, 0.0000)
    (50.0000, 28.8675)
    (66.6667, 0.0000)
    (100.0000, 0.0000)
    """

    if count == 0:
        return []

    (x1, y1) = p1
    (x2, y2) = p2

    r, t = cartesian_to_polar(x2-x1, y2-y1)

    x3, y3 = polar_to_cartesian(r/3, t)
    p3 = (x3+x1, y3+y1)

    x4, y4 = polar_to_cartesian(r / (2*math.cos(math.pi/6)), t + math.pi/6)
    p4 = (x4+x1, y4+y1)

    x5, y5 = polar_to_cartesian(2*r/3, t)
    p5 = (x5+x1, y5+y1)

    return (koch(p1, p3, count-1)
            + [p3] + koch(p3, p4, count-1)
            + [p4] + koch(p4, p5, count-1)
            + [p5] + koch(p5, p2, count-1))


def polar_to_cartesian(x, y):
    return (x * math.cos(y), x * math.sin(y))


def cartesian_to_polar(x, y):
    return (math.sqrt(x**2 + y**2), math.atan2(y, x))


def run():
    n = int(input())
    p1 = (0.0, 0.0)
    p2 = (100.0, 0.0)
    ps = [p1] + koch(p1, p2, n) + [p2]

    
    print("\n".join(["{:.5f} {:.5f}".format(*p) for p in ps]))


def plot(ps):
    import pylab
    xs = [x for x, y in ps]
    ys = [y for x, y in ps]

    pylab.plot(xs, ys)
    pylab.xlim(0, 100)
    pylab.ylim(0, 100)
    pylab.show()


if __name__ == '__main__':
    run()

Solution (Python):
import math
def koch(d, p1, p2):
    if d == 0:
        return
    
    v = [p2[0] - p1[0], p2[1] - p1[1]]
    s = [p1[0] + v[0] / 3, p1[1] + v[1] / 3]
    t = [p1[0] + 2 * v[0] / 3, p1[1] + 2 * v[1] / 3]
    u = [p1[0] + v[0] / 2 - v[1] * math.sqrt(3) / 6, p1[1] + v[1] / 2 + v[0] * math.sqrt(3) / 6]

    koch(d - 1, p1, s)
    print(" ".join(map(str, s)))
    koch(d - 1, s, u)
    print(" ".join(map(str, u)))
    koch(d - 1, u, t)
    print(" ".join(map(str, t)))
    koch(d - 1, t, p2)

n = int(input())
print("0 0")
koch(n, [0, 0], [100, 0])
print("100 0")

Solution (Python):
import math

def rotate(x, y, theta):
    return (round(x*math.cos(theta) -1*y*math.sin(theta), 5), round(x*math.sin(theta) + y*math.cos(theta), 5))

def koch(p1, p2, n):
    s = (round((2*p1[0] + p2[0])/3, 5), round((2*p1[1] + p2[1])/3, 5))
    t = (round((p1[0] + 2*p2[0])/3, 5), round((p1[1] + 2*p2[1])/3, 5))
    rotated = rotate(t[0]-s[0], t[1]-s[1], math.pi/3)
    u = (s[0] + rotated[0], s[1] + rotated[1])

    if n == 0:
        print(round(p1[0], 5), round(p1[1], 5))
    elif n == -1:
        return None

    koch(p1, s, n-1)
    koch(s, u, n-1)
    koch(u, t, n-1)
    koch(t, p2, n-1)

n = int(input())
koch((0.0, 0.0), (100, 0.0), n)
print(100, 0.0)

Solution (Python):
import math

n = int(input())


def koch(d, p1, p2):
    """

    :param int d:
    :param list p1:
    :param list p2:
    :return:
    """
    if d == 0:
        return

    s, t, u = [0, 0], [0, 0], [0, 0]
    s[0] = (2*p1[0]+p2[0]) / 3
    s[1] = (2*p1[1]+p2[1]) / 3
    t[0] = (p1[0] + 2 * p2[0]) / 3
    t[1] = (p1[1]+2*p2[1]) / 3
    u[0] = (t[0]-s[0]) * math.cos(math.pi/3) - (t[1]-s[1]) * math.sin(math.pi/3) + s[0]
    u[1] = (t[0]-s[0]) * math.sin(math.pi/3) + (t[1]-s[1]) * math.cos(math.pi/3) + s[1]

    koch(d-1, p1, s)
    print(s[0], s[1])
    koch(d-1, s, u)
    print(u[0], u[1])
    koch(d-1, u, t)
    print(t[0], t[1])
    koch(d-1, t, p2)


def main():
    print(0.00000000, 0.00000000)
    koch(n, [0, 0], [100, 0])
    print(100.00000000, 0.00000000)


if __name__ == '__main__':
    main()

Solution (Python):
import math

def pr8(a):
	print("{:.10}".format(a[0]),end=" ")
	print("{:.10}".format(a[1]))

def koch(n,p1,p2):
	if n==0:
		return
	s=[0 for i in range(2)]
	t=[0 for i in range(2)]
	u=[0 for i in range(2)]
	
	s[0]=(2*p1[0]+1*p2[0])/3
	s[1]=(2*p1[1]+1*p2[1])/3
	t[0]=(1*p1[0]+2*p2[0])/3
	t[1]=(1*p1[1]+2*p2[1])/3
	
	rad=math.radians(60)
	
	u[0]=(t[0]-s[0])*math.cos(rad)-(t[1]-s[1])*math.sin(rad)+s[0]
	u[1]=(t[0]-s[0])*math.sin(rad)+(t[1]-s[1])*math.cos(rad)+s[1]
	
	koch(n-1,p1,s)
	
	print(*s)	
	koch(n-1,s,u)
	
	print(*u)
	koch(n-1,u,t)
	
	print(*t)
	koch(n-1,t,p2)
	
n=int(input())
p1=[0,0]
p2=[100,0]

print(*p1)
koch(n,p1,p2)
print(*p2)

Solution (Python):



import math

def koch(n,p1,p2):
	if n==0:
		return
	s=[0 for i in range(2)]
	t=[0 for i in range(2)]
	u=[0 for i in range(2)]
	
	s[0]=(2*p1[0]+1*p2[0])/3
	s[1]=(2*p1[1]+1*p2[1])/3
	t[0]=(1*p1[0]+2*p2[0])/3
	t[1]=(1*p1[1]+2*p2[1])/3
	
	rad=math.radians(60)
	
	u[0]=(t[0]-s[0])*math.cos(rad)-(t[1]-s[1])*math.sin(rad)+s[0]
	u[1]=(t[0]-s[0])*math.sin(rad)+(t[1]-s[1])*math.cos(rad)+s[1]
	
	koch(n-1,p1,s)
	
	print(*s)	
	koch(n-1,s,u)
	
	print(*u)
	koch(n-1,u,t)
	
	print(*t)
	koch(n-1,t,p2)
	
n=int(input())
p1=[0,0]
p2=[100,0]

print(*p1)
koch(n,p1,p2)
print(*p2)

Solution (Python):
import math

def koch(n,p1,p2):
	if n==0:
		return
	s=[0 for i in range(2)]
	t=[0 for i in range(2)]
	u=[0 for i in range(2)]
	
	s[0]=(2*p1[0]+1*p2[0])/3
	s[1]=(2*p1[1]+1*p2[1])/3
	t[0]=(1*p1[0]+2*p2[0])/3
	t[1]=(1*p1[1]+2*p2[1])/3
	
	rad=math.radians(60)
	
	u[0]=(t[0]-s[0])*math.cos(rad)-(t[1]-s[1])*math.sin(rad)+s[0]
	u[1]=(t[0]-s[0])*math.sin(rad)+(t[1]-s[1])*math.cos(rad)+s[1]
	
	koch(n-1,p1,s)
	print(*s)	
	
	koch(n-1,s,u)
	print(*u)
	
	koch(n-1,u,t)
	print(*t)
	
	koch(n-1,t,p2)
	
n=int(input())
p1=[0,0]
p2=[100,0]

print(*p1)
koch(n,p1,p2)
print(*p2)

Solution (Python):
import math
class Dot:
    x=0.0
    y=0.0
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __lt__(self,other):
        return self.x < other.x
    def print(self):
        x=self.x
        y=self.y
        print("{:.8f} {:.8f}".format(x,y))       
def kock(n,a,b,dots):
    if n == 0:
        return
    s=Dot((2*a.x+b.x)/3,(2*a.y+b.y)/3)
    t=Dot((a.x+2*b.x)/3,(a.y+2*b.y)/3)
    rad=math.radians(60)
    x=(t.x-s.x)*math.cos(rad)-(t.y-s.y)*math.sin(rad)+s.x
    y=(t.x-s.x)*math.sin(rad)+(t.y-s.y)*math.cos(rad)+s.y
    u=Dot(x,y)

    kock(n-1,a,s,dots)
    s.print()
    dots.append(s)
    kock(n-1,s,u,dots)
    u.print()
    dots.append(u)
    kock(n-1,u,t,dots)
    t.print()
    dots.append(t)
    kock(n-1,t,b,dots)

if __name__ == "__main__":
    n=int(input())
    dots=[]
    dot1=Dot(0,0)
    dot2=Dot(100,0)
    dots.append(dot1)
    dots.append(dot2)
    dot1.print()
    kock(n,dot1,dot2,dots)
    dot2.print()

Solution (Python):
from collections import namedtuple
import math
Co = namedtuple('Co', 'x y')

def f(i, n, l, r):
    if i >= n:
        return
    il = Co(x=(1/3 * r.x + (1 - 1/3) * l.x), y=(1/3 * r.y + (1 - 1/3) * l.y))
    ir = Co(x=(2/3 * r.x + (1 - 2/3) * l.x), y=(2/3 * r.y + (1 - 2/3) * l.y))

    trix = 1/2 * (ir.x + il.x - math.sqrt(3) * ir.y + math.sqrt(3) * il.y)
    triy = 1/2 * (math.sqrt(3) * ir.x - math.sqrt(3) * il.x + ir.y + il.y)
    trico = Co(trix, triy)

    f(i+1, n, l, il)
    print(il.x, il.y)
    f(i+1, n, il, trico)
    print(trico.x, trico.y)
    f(i+1, n, trico, ir)
    print(ir.x, ir.y)
    f(i+1, n, ir, r)

if __name__ == '__main__':
    n = int(input())
    print(0, 0)
    f(0, n, Co(0,0), Co(100,0))
    print(100, 0)

Solution (Python):
from collections import namedtuple
import math
Co = namedtuple('Co', 'x y')

def f(i, n, l, r):
    if i >= n:
        print(l.x, l.y)
        return
    il = Co(x=(1/3 * r.x + (1 - 1/3) * l.x), y=(1/3 * r.y + (1 - 1/3) * l.y))
    ir = Co(x=(2/3 * r.x + (1 - 2/3) * l.x), y=(2/3 * r.y + (1 - 2/3) * l.y))

    trix = 1/2 * (ir.x + il.x - math.sqrt(3) * ir.y + math.sqrt(3) * il.y)
    triy = 1/2 * (math.sqrt(3) * ir.x - math.sqrt(3) * il.x + ir.y + il.y)
    trico = Co(trix, triy)

    f(i+1, n, l, il)
    
    f(i+1, n, il, trico)
    
    f(i+1, n, trico, ir)
    
    f(i+1, n, ir, r)

if __name__ == '__main__':
    n = int(input())
    
    f(0, n, Co(0,0), Co(100,0))
    print(100, 0)

Solution (Python):
import math
def koch(d, p1, p2):
    if d == 0:
        return
    x=p2[0]-p1[0]
    y=p2[1]-p1[1]
    s = [(p1[0]*2+p2[0]*1)/3.0,(p1[1]*2+p2[1]*1)/3.0]
    t = [(p1[0]*1+p2[0]*2)/3.0,(p1[1]*1+p2[1]*2)/3.0]
    u = [p1[0]+x/2-y*math.sqrt(3)/6,p1[1]+y/2+x*math.sqrt(3)/6]
    koch(d - 1, p1, s)
    print(*s)
    koch(d - 1, s, u)
    print(*u)
    koch(d - 1, u, t)
    print(*t)
    koch(d - 1, t, p2)
 
n = int(input())
p1=[0,0]
p2=[100,0]
print(*p1)
koch(n,p1,p2)
print(*p2)

Solution (Python):
import math
n = int(input())
edge_s = (0.0, 0.0)
edge_e = (100.0, 0.0)

def print_tuple(tup):
    print("%.8f %.8f"%(tup[0],tup[1]))

def koch(start, end, n):
    if(n == 0):
        return
    s = ((2*start[0] + end[0]) / 3, (2*start[1] + end[1])/3)
    t = ((start[0] + 2*end[0]) / 3, (start[1] + 2*end[1])/3)
    u = (s[0] +  (t[0]-s[0])*math.cos(math.pi/3)-(t[1]-s[1])*math.sin(math.pi/3), s[1] + (t[0]-s[0]) * math.sin(math.pi/3) + (t[1]-s[1]) * math.cos(math.pi/3))
    n -= 1
    koch(start, s, n)
    print_tuple(s)
    koch(s, u, n)
    print_tuple(u)
    koch(u, t, n)
    print_tuple(t)
    koch(t, end, n)
print_tuple(edge_s)
koch(edge_s, edge_e, n)
print_tuple(edge_e)

Solution (Python):
import math

class dot():
    def __init__(self, x, y):
        self.x = x
        self.y = y


def koch(n, p1, p2):
    if n != 0:
        r = 60.0*math.pi/180.0

        x = (2.0*p1.x+1.0*p2.x)/3.0
        y = (2.0*p1.y+1.0*p2.y)/3.0
        s = dot(x, y)

        x = (1.0*p1.x+2.0*p2.x)/3.0
        y = (1.0*p1.y+2.0*p2.y)/3.0
        t = dot(x, y)

        x = (t.x-s.x)*math.cos(r) - (t.y-s.y)*math.sin(r) + s.x
        y = (t.x-s.x)*math.sin(r) + (t.y-s.y)*math.cos(r) + s.y
        u = dot(x, y)

        koch(n-1, p1, s)
        print(s.x, ' ', s.y)
        koch(n-1, s, u)
        print(u.x, ' ', u.y)
        koch(n-1, u, t)
        print(t.x, ' ', t.y)
        koch(n-1, t, p2)


p1 = dot(0.0, 0.0)
p2 = dot(100.0, 0.0)
n = int(input())

print(p1.x, ' ', p1.y)
koch(n, p1, p2)
print(p2.x, ' ', p2.y)

Solution (Python):
import math

n = int(input())

sin60 = (math.sqrt)(3)/2
cos60 = 1/2


def koch(n, p1_x, p2_x, p1_y, p2_y):
    if n == 0:
        return

    s_x = (2*p1_x + 1*p2_x) / 3
    s_y = (2*p1_y + 1*p2_y) / 3
    t_x = (1*p1_x + 2*p2_x) / 3
    t_y = (1*p1_y + 2*p2_y) / 3
    u_x = (t_x - s_x)*cos60 - (t_y - s_y)*sin60 + s_x
    u_y = (t_x - s_x)*sin60 + (t_y - s_y)*cos60 + s_y

    koch(n-1, p1_x, s_x, p1_y, s_y)
    print(s_x, s_y)
    koch(n-1, s_x, u_x, s_y, u_y)
    print(u_x, u_y)
    koch(n-1, u_x, t_x, u_y, t_y)
    print(t_x, t_y)
    koch(n-1, t_x, p2_x, t_y, p2_y)


print(0.00000000, 0.00000000)
koch(n, 0, 100, 0, 0)
print(100.00000000, 0.00000000)

Solution (Python):
import math

def kock(n,p1x,p1y,p2x,p2y):
    if(n==0):
        return

    sx = (2*p1x+p2x)/3.0
    sy = (2*p1y+p2y)/3.0
    tx = (p1x+2*p2x)/3.0
    ty = (p1y+2*p2y)/3.0

    ux = (tx-sx)*math.cos(math.radians(60)) - (ty-sy)*math.sin(math.radians(60)) + sx
    uy = (tx-sx)*math.sin(math.radians(60)) + (ty-sy)*math.cos(math.radians(60)) + sy

    kock(n-1,p1x,p1y,sx,sy)
    print(sx,sy)
    kock(n-1,sx,sy,ux,uy)
    print(ux,uy)
    kock(n-1,ux,uy,tx,ty)
    print(tx,ty)
    kock(n-1,tx,ty,p2x,p2y)


n = int(input())
print(0,0)
kock(n,0,0,100,0)
print(100,0)

Solution (Python):
import math
def f(n, p1, p2):
    if n == 0: return
    s = []
    t = []
    u = []
    s.append((2*p1[0] + p2[0])/3)
    s.append((2*p1[1] + p2[1])/3)
    t.append((p1[0] + 2*p2[0])/3)
    t.append((p1[1] + 2*p2[1])/3)
    u.append((t[0]-s[0])*math.cos(math.radians(60))
             - (t[1]-s[1])*math.sin(math.radians(60)) + s[0])
    u.append((t[0]-s[0])*math.sin(math.radians(60))
             + (t[1]-s[1])*math.cos(math.radians(60)) + s[1])
    f(n-1, p1, s)
    print(s[0],s[1])
    f(n-1, s, u)
    print(u[0],u[1])
    f(n-1, u, t)
    print(t[0],t[1])
    f(n-1, t, p2)


n = int(input())
p1 = [0,0]
p2 = [100,0]
print(p1[0],p1[1])
f(n, p1, p2)
print(p2[0],p2[1])

Solution (Python):
import math
def koch(a, p1, p2):
    if a == 0:
        return
     
    v = [p2[0] - p1[0], p2[1] - p1[1]]
    s = [p1[0] + v[0] / 3, p1[1] + v[1] / 3]
    t = [p1[0] + 2 * v[0] / 3, p1[1] + 2 * v[1] / 3]
    u = [p1[0] + v[0] / 2 - v[1] * math.sqrt(3) / 6, p1[1] + v[1] / 2 + v[0] * math.sqrt(3) / 6]
 
    koch(a-1,p1,s)
    print(" ".join(map(str, s)))
    koch(a-1,s,u)
    print(" ".join(map(str, u)))
    koch(a-1,u,t)
    print(" ".join(map(str, t)))
    koch(a-1,t,p2)
 
n = int(input())
print("0 0")
koch(n, [0, 0], [100, 0])
print("100 0")

Solution (Python):
import math

n = int(input())
p1 = (0, 0)
p2 = (100, 0)
points = []

def koch(n, p, q):
    if n == 0:
        return

    x1, y1 = p
    x2, y2 = q
    sx = (2 * x1 + 1 * x2) / 3
    sy = (2 * y1 + 1 * y2) / 3
    s = (sx, sy)
    tx = (1 * x1 + 2 * x2) / 3
    ty = (1 * y1 + 2 * y2) / 3
    t = (tx, ty)
    theta = math.pi / 3
    ux = (tx - sx) * math.cos(theta) - (ty - sy) * math.sin(theta) + sx
    uy = (tx - sx) * math.sin(theta) + (ty - sy) * math.cos(theta) + sy
    u = (ux, uy)

    koch(n - 1, p, s)
    print(*s)
    koch(n - 1, s, u)
    print(*u)
    koch(n - 1, u, t)
    print(*t)
    koch(n - 1, t, q)


print(*p1)
koch(n, p1, p2)
print(*p2)

Solution (Python):
import math
 
n=int(input())
 

a=math.sin(math.radians(60))
b=math.cos(math.radians(60))

 
def f(n,x1,x2,y1,y2):
    if n==0:
        return
 
    sx=(2*x1 + 1*x2) / 3
    sy=(2*y1 + 1*y2) / 3
    tx=(1*x1 + 2*x2) / 3
    ty=(1*y1 + 2*y2) / 3
    ux=(tx-sx)*b - (ty-sy)*a + sx
    uy=(tx-sx)*a + (ty-sy)*b + sy
 
    f(n-1,x1,sx,y1,sy)
    print(sx, sy)
    f(n-1,sx,ux,sy,uy)
    print(ux, uy)
    f(n-1,ux,tx,uy,ty)
    print(tx, ty)
    f(n-1,tx,x2,ty,y2)
 
 
print(0.00000000, 0.00000000)
f(n,0,100,0,0)
print(100.00000000, 0.00000000)

Solution (Python):
import math
 
n = int(input())
 
 
 
def koch(n,x_0,y_0,x_1,y_1):
    if n == 0:
        return   
    x_m1 = x_0 + (x_1- x_0)/3
    y_m1 = y_0 + (y_1 -y_0)/3
    x_m2 = x_0 + (x_1- x_0)* 2/3
    y_m2 = y_0 + (y_1 -y_0) *2/3
    x_m =  (x_m2 - x_m1) * math.cos(math.pi/3) - (y_m2 - y_m1) * math.sin(math.pi/3) + x_m1
    y_m =  (x_m2 - x_m1) * math.sin(math.pi/3) + (y_m2 - y_m1) * math.cos(math.pi/3) + y_m1
 
    koch(n-1,x_0,y_0,x_m1,y_m1)
    print(x_m1,y_m1)
    koch(n-1,x_m1,y_m1,x_m,y_m)
    print(x_m,y_m)
    koch(n-1,x_m,y_m,x_m2,y_m2)
    print(x_m2,y_m2)
    koch(n-1,x_m2,y_m2,x_1,y_1)
     
print(0,0)
koch(n,0,0,100,0)
print(100,0)

Solution (Python):
import math
  
n = int(input())
  
  
  
def koch(n,x_0,y_0,x_1,y_1):
    if n == 0:
        return  
    x_m1 = x_0 + (x_1- x_0)/3
    y_m1 = y_0 + (y_1 -y_0)/3
    x_m2 = x_0 + (x_1- x_0)* 2/3
    y_m2 = y_0 + (y_1 -y_0) *2/3
    x_m =  (x_m2 - x_m1) * math.cos(math.pi/3) - (y_m2 - y_m1) * math.sin(math.pi/3) + x_m1
    y_m =  (x_m2 - x_m1) * math.sin(math.pi/3) + (y_m2 - y_m1) * math.cos(math.pi/3) + y_m1
  
    koch(n-1,x_0,y_0,x_m1,y_m1)
    print(x_m1,y_m1)
    koch(n-1,x_m1,y_m1,x_m,y_m)
    print(x_m,y_m)
    koch(n-1,x_m,y_m,x_m2,y_m2)
    print(x_m2,y_m2)
    koch(n-1,x_m2,y_m2,x_1,y_1)
      
print(0,0)
koch(n,0,0,100,0)
print(100,0)

Solution (Python):
import math


def make_stu(p1, p2):
    s = [(2 * p1[0] + p2[0]) / 3, (2 * p1[1] + p2[1]) / 3]
    t = [(p1[0] + 2 * p2[0]) / 3, (p1[1] + 2 * p2[1]) / 3]
    u = [s[0] + (t[0] - s[0]) / 2 - math.sqrt(3) * (t[1] - s[1]) / 2,
         s[1] + (t[1] - s[1]) / 2 + math.sqrt(3) * (t[0] - s[0]) / 2]
    return s, t, u


def koch_curve(n, p1, p2):
    if n >= 1:
        s, t, u = make_stu(p1, p2)
        if n == 1:
            print(s[0], s[1])
            print(u[0], u[1])
            print(t[0], t[1])
        else:
            koch_curve(n - 1, p1, s)
            print(s[0], s[1])
            koch_curve(n - 1, s, u)
            print(u[0], u[1])
            koch_curve(n - 1, u, t)
            print(t[0], t[1])
            koch_curve(n - 1, t, p2)

n = int(input())

print(0.0, 0.0)
koch_curve(n, [0.0, 0.0], [100.0, 0.0])
print(100.0, 0.0)

Solution (Python):
n = int(input())

def three_points(p1,p2):
    q1 = ((2*p1[0]+p2[0])/3, (2*p1[1]+p2[1])/3)
    q2 = ((p1[0]+2*p2[0])/3, (p1[1]+2*p2[1])/3)
    dx,dy = p2[0]-p1[0], p2[1]-p1[1]
    q3 = (p1[0]+dx/2-3**0.5/6*dy, p1[1]+dy/2+3**0.5/6*dx)
    return [q1,q3,q2]

m = [(0,0),(100,0)]
for i in range(n):
    tmpm = []
    for j in range(len(m)-1):
        tmpm += [m[j]] + three_points(m[j],m[j+1])
    tmpm += [m[-1]]
    m = tmpm

for x in m:
    print(x[0], x[1])

Solution (Python):
import math
def stdinput():
    from sys import stdin
    return stdin.readline().strip()

def main():
    n = int(stdinput())
    nodes = [[0., 0.], [100., 0.]]

    for _ in range(n):
        next_nodes = []
        for s_node, e_node in zip(nodes[:], nodes[1:]):
            next_nodes.append(s_node)
            next_nodes += koch(s_node, e_node)
        next_nodes.append(nodes[-1])
        nodes = next_nodes

    for node in nodes:
        print(*map(lambda a: f'{round(a,8):.8f}', node))

def koch(start, end):
    nodes = []

    dx = (end[0] - start[0]) / 3
    dy = (end[1] - start[1]) / 3
    
    s = [start[0] + dx, start[1] + dy]
    t = [start[0] + 2 * dx, start[1] + 2 * dy]

    nodes.append(s)
    nodes.append(determine_koch(s, t))
    nodes.append(t)
    
    return nodes

def determine_koch(s, t):
    dx = (t[0] - s[0]) * math.sqrt(3) / 2
    dy = (t[1] - s[1]) * math.sqrt(3) / 2

    u0 = [s[0] - dy, s[1] + dx]
    u1 = [t[0] - dy, t[1] + dx]

    return [
        (u0[0] + u1[0]) / 2,
        (u0[1] + u1[1]) / 2
    ]

if __name__ == '__main__':
    main()

Solution (Python):
from math import sqrt
def koch(d, p1, p2):
    if d == 0:
        return
     
    v = [p2[0] - p1[0], p2[1] - p1[1]]
    s = [p1[0] + v[0] / 3, p1[1] + v[1] / 3]
    t = [p1[0] + 2 * v[0] / 3, p1[1] + 2 * v[1] / 3]
    u = [p1[0] + v[0] / 2 - v[1] *sqrt(3) / 6, p1[1] + v[1] / 2 + v[0] *sqrt(3) / 6]
 
    koch(d - 1, p1, s)
    print(" ".join(map(str, s)))
    koch(d - 1, s, u)
    print(" ".join(map(str, u)))
    koch(d - 1, u, t)
    print(" ".join(map(str, t)))
    koch(d - 1, t, p2)
 
n = int(input())
print("0 0")
koch(n, [0, 0], [100, 0])
print("100 0")

Solution (Python):
import math, cmath

def koch(p1, p2, n):
    if n == 0:
        return [p1, p2]
    
    rad60 = math.pi/3
    v1 = (p2 - p1)/3
    v2 = cmath.rect(abs(v1), cmath.phase(v1)+rad60)
    q1 = p1 + v1
    q2 = q1 + v2
    q3 = q1 + v1
    if n == 1:
        return [p1,q1,q2,q3,p2]
    else:
        x1 = koch(p1,q1,n-1)[1:-1]
        x2 = koch(q1,q2,n-1)[1:-1]
        x3 = koch(q2,q3,n-1)[1:-1]
        x4 = koch(q3,p2,n-1)[1:-1]
        x = [p1]+x1+[q1]+x2+[q2]+x3+[q3]+x4+[p2]
    return x

n = int(raw_input())
p1 = complex(0,0)
p2 = complex(100,0)

for e in koch(p1,p2,n):
    print e.real,e.imag

Solution (Python):
import math, cmath

def koch(p1, p2, n):
    if n == 0:
        return [p1, p2]
    
    rad60 = math.pi/3
    v1 = (p2 - p1)/3
    v2 = cmath.rect(abs(v1), cmath.phase(v1)+rad60)
    q1 = p1 + v1
    q2 = q1 + v2
    q3 = q1 + v1
    if n == 1:
        return [p1,q1,q2,q3,p2]
    else:
        x1 = koch(p1,q1,n-1)[:-1]
        x2 = koch(q1,q2,n-1)[:-1]
        x3 = koch(q2,q3,n-1)[:-1]
        x4 = koch(q3,p2,n-1)
        x = x1 + x2 + x3 + x4
    return x

n = int(raw_input())
p1 = complex(0,0)
p2 = complex(100,0)

for e in koch(p1,p2,n):
    print e.real,e.imag

Solution (Python):

from __future__ import division, print_function
from sys import stdin
from math import pi, sin, cos

COS_PI_3 = cos(pi / 3.0)
SIN_PI_3 = sin(pi / 3.0)


def koch_curve(s, e, n):
    if not n:
        return (s, e)
    x = (e[0]-s[0]) / 3.0
    y = (e[1]-s[1]) / 3.0
    p1 = (s[0] + x, s[1] + y)
    p2 = (x*COS_PI_3 - y*SIN_PI_3 + p1[0], x*SIN_PI_3 + y*COS_PI_3 + p1[1])
    p3 = (e[0] - x, e[1] - y)
    return (koch_curve(s, p1, n-1) + koch_curve(p1, p2, n-1)[1:] +
            koch_curve(p2, p3, n-1)[1:] + koch_curve(p3, e, n-1)[1:])


n = int(stdin.readline())
for x, y in koch_curve((0, 0), (100, 0), n):
    print('{:.8f} {:.8f}'.format(x, y))

Solution (Python):
from math import sqrt
from collections import namedtuple

Point = namedtuple('Point', 'x y')

def rot60(p):
    return Point(C60 * p.x - S60 * p.y, S60 * p.x + C60 * p.y)

def koch(n, p1, p2):
    if n == 0: return
    s = Point((2*p1.x + p2.x)/3, (2*p1.y + p2.y)/3)
    t = Point((p1.x + 2*p2.x)/3, (p1.y + 2*p2.y)/3)
    q = rot60(Point((p2.x - p1.x)/3, (p2.y - p1.y)/3))
    u = Point(s.x + q.x, s.y + q.y)

    koch(n - 1, p1, s)
    print(f.format(s.x, s.y))

    koch(n - 1, s, u)
    print(f.format(u.x, u.y))

    koch(n - 1, u, t)
    print(f.format(t.x, t.y))

    koch(n - 1, t, p2)


C60 = 1 / 2
S60 = sqrt(3) / 2
f = '{:8f} {:8f}'

n = int(input())
print(f.format(0, 0))
koch(n, Point(0, 0), Point(100, 0))
print(f.format(100, 0))

Solution (Python):
import math

class Point():
    def __init__(self, x, y):
        self.x = x
        self.y = y


def koch(n, a, b):
    if n == 0:
        return
    th = math.radians(60)
    s = Point((2*a.x+b.x)/3, (2*a.y+b.y)/3)
    t = Point((a.x+2*b.x)/3, (a.y+2*b.y)/3)
    u = Point((t.x-s.x)* math.cos(th) - (t.y-s.y)*math.sin(th)+s.x,
                (t.x-s.x)* math.sin(th) + (t.y-s.y)*math.cos(th)+s.y)

    koch(n-1, a, s)
    print(s.x, s.y)
    koch(n-1, s, u)
    print(u.x, u.y)
    koch(n-1, u, t)
    print(t.x, t.y)
    koch(n-1, t, b)

if __name__ == '__main__':
    import sys
    input = sys.stdin.readline

    n = int(input())

    a = Point(0, 0)
    b = Point(100, 0)

    print(a.x, a.y)
    koch(n, a, b)
    print(b.x, b.y)

Solution (Python):
import math

def triangle(co1, co2):
    ans = []
    x1 = co1[0]
    y1 = co1[1]
    x2 = co2[0]
    y2 = co2[1]
    part1 = [(2 * x1 + x2) /3, (2 * y1 + y2) / 3]
    part2 = [(x1 + 2 * x2) /3, (y1 + 2 * y2) / 3]
    
    edge = [(part2[0] - part1[0])/2 - (part2[1] - part1[1])/ 2 * math.sqrt(3) + part1[0], (part2[0] - part1[0])/2 * math.sqrt(3) + (part2[1] - part1[1])/ 2 + part1[1]]
    ans.extend([part1, edge, part2, co2])
    return ans
def rec(A, n):
    if n == 0:
        return
    
    rec(A, n-1)
    for i in range(4 ** (n-1)):
        A[n].extend(triangle(A[n-1][i], A[n-1][i+1]))

n = int(input())
koch = [[[0.0, 0.0]] for i in range(n + 1)]
koch[0].append([100.0, 0.0])
rec(koch, n)
for i in range(4 ** n + 1):
    print(*koch[n][i])

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n, x1,y1,x2,y2):
    if n == 0: 
        plot(x1,y1)        
        return
    sx = (2 * x1 + x2) / 3
    sy = (2 * y1 + y2) / 3
    tx = (x1 + 2 * x2) / 3
    ty = (y1 + 2 * y2) / 3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1, y1, sx, sy)
    Koch(n-1, sx, sy, ux, uy)
    Koch(n-1, ux, uy, tx, ty)
    Koch(n-1, tx, ty, x2, y2)

n = int(input())
Koch(n, 0, 0, 100, 0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f"{x:.8f} {y:.8f}")

def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    ax=(2*x1+x2)/3
    ay=(2*y1+y2)/3
    bx=(x1+2*x2)/3
    by=(y1+2*y2)/3
    cx=(bx-ax)*(1/2)-(by-ay)*(math.sqrt(3)/2)+ax
    cy=(bx-ax)*(math.sqrt(3)/2)+(by-ay)*(1/2)+ay
    koch(n-1,x1,y1,ax,ay)
    koch(n-1,ax,ay,cx,cy)
    koch(n-1,cx,cy,bx,by)
    koch(n-1,bx,by,x2,y2)
    
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math
def plot(x,y):
	print(f'{x:.8f} {y:.8f}')
def koch(n,x1,y1,x2,y2):
	if n==0:
		plot(x1,y1)
		return
	sx=(2*x1+x2)/3
	sy=(2*y1+y2)/3
	tx=(x1+2*x2)/3
	ty=(y1+2*y2)/3
	ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
	uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
	koch(n-1,x1,y1,sx,sy)
	koch(n-1,sx,sy,ux,uy)
	koch(n-1,ux,uy,tx,ty)
	koch(n-1,tx,ty,x2,y2)
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math as m
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*m.cos(m.radians(60))-(ty-sy)*m.sin(m.radians(60))+sx
    uy=(tx-sx)*m.sin(m.radians(60))+(ty-sy)*m.cos(m.radians(60))+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)

n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f"{x:.8f} {y:.8f}")
def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import sys
from math import sin, cos, radians
input = sys.stdin.readline
deg = radians(60)

def Koch(n, p1_x, p1_y, p2_x, p2_y):
    if n != 0:
        s_x = (2 * p1_x + p2_x) / 3
        s_y = (2 * p1_y + p2_y) / 3
        t_x = (p1_x + 2 * p2_x) / 3
        t_y = (p1_y + 2 * p2_y) / 3
        u_x = (t_x - s_x) * cos(deg) - (t_y - s_y) * sin(deg) + s_x
        u_y = (t_x - s_x) * sin(deg) + (t_y - s_y) * cos(deg) + s_y
        Koch(n - 1, p1_x, p1_y, s_x, s_y)
        print('{:.8f} {:.8f}'.format(s_x, s_y))
        Koch(n - 1, s_x, s_y, u_x, u_y)
        print('{:.8f} {:.8f}'.format(u_x, u_y))
        Koch(n - 1, u_x, u_y, t_x, t_y)
        print('{:.8f} {:.8f}'.format(t_x, t_y))
        Koch(n - 1, t_x, t_y, p2_x, p2_y)
    else:
        return

n = int(input())
p1_x, p1_y = 0, 0
p2_x, p2_y = 100, 0
print('{:.8f} {:.8f}'.format(p1_x, p1_y))
Koch(n, p1_x, p1_y, p2_x, p2_y)
print('{:.8f} {:.8f}'.format(p2_x, p2_y))

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)

n=int(input())
Koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
N = int(input())
from math import sin, cos, pi
th = pi/3
def koch(d, p1, p2):
    if d == 0:
        return
    s = [0, 0]
    t = [0, 0]
    u = [0, 0]
    
    s[0] = (2 * p1[0] + p2[0])/3
    s[1] = (2 * p1[1] + p2[1])/3

    t[0] = (2 * p2[0] + p1[0])/3
    t[1] = (2 * p2[1] + p1[1])/3

    u[0] = (t[0] - s[0]) * cos(th) - (t[1] - s[1]) * sin(th) + s[0]
    u[1] = (t[0] - s[0]) * sin(th) + (t[1] - s[1]) * cos(th) + s[1]

    koch(d-1, p1, s)
    print(*s)

    koch(d-1, s, u)
    print(*u)

    koch(d-1, u, t)
    print(*t)

    koch(d-1, t, p2)

p_start = [0.0, 0.0]
p_end = [100.0, 0.0]
print(*p_start)
koch(N, p_start, p_end)
print(*p_end)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n, x1,y1,x2,y2):
    if n == 0: 
        plot(x1,y1)        
        return
    sx = (2 * x1 + x2) / 3
    sy = (2 * y1 + y2) / 3
    tx = (x1 + 2 * x2) / 3
    ty = (y1 + 2 * y2) / 3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1, y1, sx, sy)
    Koch(n-1, sx, sy, ux, uy)
    Koch(n-1, ux, uy, tx, ty)
    Koch(n-1, tx, ty, x2, y2)

n = int(input())
Koch(n, 0, 0, 100, 0)
plot(100,0)

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
    
n=int(input())
koch(n,0,0,100,0)
plot(100,0)