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):
import math

class Pos:
	def __init__(self, x, y):
		self.x = x
		self.y = y
	def __str__(self):
		return str(self.x) + ' ' + str(self.y)

def koch_curve(i, n, start, end):
	if i < n:
		f = get_pos(start, end, 1/3)
		t = get_pos(start, end, 2/3)
		s = get_sec_pos(start, end, f, t)
		koch_curve(i + 1, n, start, f)
		print(f)
		koch_curve(i + 1, n, f, s)
		print(s)
		koch_curve(i + 1, n, s, t)
		print(t)
		koch_curve(i + 1, n, t, end)

def get_pos(start, end, m):
	dX = (end.x - start.x) * m
	dY = (end.y - start.y) * m
	return Pos(start.x + dX, start.y + dY)

def get_sec_pos(start, end, f, t):
	x = y = 0
	if f.y == t.y:
		x = start.x + (end.x - start.x) / 2
		len = math.fabs(t.x - f.x)
		h = math.sqrt(len**2 - (len/2)**2)
		if start.x < end.x:
			y = start.y + h
		else:
			y = start.y - h
	elif f.x < t.x and f.y < t.y:
		x = start.x
		y = t.y
	elif f.x > t.x and f.y < t.y:
		x = end.x
		y = f.y
	elif f.x > t.x and f.y > t.y:
		x = start.x
		y = t.y
	else:
		x = end.x
		y = f.y
	return Pos(x, y)
	
n = int(input())
start = Pos(0, 0)
end = Pos(100, 0)
print(start)
koch_curve(0, n, start, end)
print(end)

Solution (Python):
import math

class Pos:
	def __init__(self, x, y):
		self.x = x
		self.y = y
	def __str__(self):
		return str(self.x) + ' ' + str(self.y)

def koch_curve(i, n, start, end):
	if i < n:
		f = get_pos(start, end, 1/3)
		t = get_pos(start, end, 2/3)
		s = get_sec_pos(start, end, f, t)
		koch_curve(i + 1, n, start, f)
		print(f)
		koch_curve(i + 1, n, f, s)
		print(s)
		koch_curve(i + 1, n, s, t)
		print(t)
		koch_curve(i + 1, n, t, end)

def get_pos(start, end, m):
	dX = (end.x - start.x) * m
	dY = (end.y - start.y) * m
	return Pos(start.x + dX, start.y + dY)

def get_sec_pos(start, end, f, t):
	if f.y == t.y:
		x = start.x + (end.x - start.x) / 2
		len = math.fabs(t.x - f.x)
		h = math.sqrt(len**2 - (len/2)**2)
		if start.x < end.x:
			y = start.y + h
		else:
			y = start.y - h
	elif f.x < t.x and f.y < t.y:
		x = start.x
		y = t.y
	elif f.x > t.x and f.y < t.y:
		x = end.x
		y = f.y
	elif f.x > t.x and f.y > t.y:
		x = start.x
		y = t.y
	else:
		x = end.x
		y = f.y
	return Pos(x, y)
	
n = int(input())
start = Pos(0, 0)
end = Pos(100, 0)
print(start)
koch_curve(0, n, start, end)
print(end)

Solution (Python):
def recKF(p1,p2,n):
    if n != 0:
        a = 1/2
        b = 3**(1/2)/2
        s = Point( p1.x + (p2.x-p1.x)/3, p1.y + (p2.y-p1.y)/3 )
        t = Point( p1.x + (p2.x-p1.x)*2/3, p1.y + (p2.y-p1.y)*2/3 )
        v = Point( t.x-s.x, t.y-s.y )
        rot_v = Point( v.x*a - v.y*b, b*v.x + a*v.y )
        u = Point( rot_v.x + s.x, rot_v.y + s.y)
        recKF(p1,s,n-1)
        print(s.x,s.y)
        recKF(s,u,n-1)
        print(u.x,u.y)
        recKF(u,t,n-1)
        print(t.x,t.y)
        recKF(t,p2,n-1)
        
from collections import namedtuple
Point = namedtuple("Point", ["x","y"])
p1 = Point(0,0)
p2 = Point(100,0)
n = int(input())
print(p1.x,p1.y)
recKF(p1,p2,n)
print(p2.x,p2.y)

Solution (Python):
def koch_curve(n, x1, y1, x2, y2):
    if n == 0:
        return [(x1, y1)]
    s_x = (2 * x1 + x2) / 3
    s_y = (2 * y1 + y2) / 3
    t_x = (x1 + 2 * x2) / 3
    t_y = (y1 + 2 * y2) / 3
    
    u_x = (t_x - s_x) * .5 - (t_y - s_y) * (3 ** .5 / 2) + s_x
    u_y = (t_x - s_x) * (3 ** .5 / 2) + (t_y - s_y) * .5  + s_y
    ret = koch_curve(n - 1, x1, y1, s_x, s_y) + \
        koch_curve(n - 1, s_x, s_y, u_x, u_y) + \
        koch_curve(n - 1, u_x, u_y, t_x, t_y) + \
        koch_curve(n - 1, t_x, t_y, x2, y2)
    return ret
for x, y in koch_curve(int(input()), 0, 0, 100, 0):
    print('{:.8f} {:.8f}'.format(x, y))
print('{:.8f} {:.8f}'.format(100, 0))

Solution (Python):
import math

n = int(input())

def dividing_point(p1, p2, ratio):
    return ( p1[0]*(1-ratio) + p2[0]*ratio, p1[1]*(1-ratio) + p2[1]*ratio)

def rotate_vec_60deg(v):
    return (v[0]/2 - math.sqrt(3)*v[1]/2, math.sqrt(3)*v[0]/2 + v[1]/2)
    

def koch(depth, p_left, p_right):
    if depth == 0:
        return
    
    s = dividing_point(p_left, p_right, 1/3)
    t = dividing_point(p_left, p_right, 2/3)
    tmpvec = rotate_vec_60deg((s[0] - p_left[0], s[1] - p_left[1] ))
    u = (s[0] + tmpvec[0], s[1] + tmpvec[1])
    
    koch(depth - 1, p_left, s)
    print(s[0], s[1])
    koch(depth - 1, s, u)
    print(u[0], u[1])
    koch(depth - 1, u, t)
    print(t[0], t[1])
    koch(depth - 1, t, p_right)
    
    
print(0.0, 0.0)
koch(n, (0,0), (100, 0))
print(100.0, 0)

Solution (Python):
import math
class Point:
	def __init__(self,x,y):
		self.x=x
		self.y=y
	def __str__(self):
		return str(self.x)+' '+str(self.y)
	
def koch(n,a,b):
	if n==0: return
	s=Point(0,0)
	t=Point(0,0)
	u=Point(0,0)
	th=math.pi*60/180
	s.x=(2*a.x+1*b.x)/3
	s.y=(2*a.y+1*b.y)/3
	t.x=(1*a.x+2*b.x)/3
	t.y=(1*a.y+2*b.y)/3
	u.x=(t.x-s.x)*math.cos(th)-(t.y-s.y)*math.sin(th)+s.x
	u.y=(t.x-s.x)*math.sin(th)+(t.y-s.y)*math.cos(th)+s.y
	
	koch(n-1,a,s)
	print(s)
	koch(n-1,s,u)
	print(u)
	koch(n-1,u,t)
	print(t)
	koch(n-1,t,b)

n=int(input())
a=Point(0,0)
b=Point(100,0)
print(a)
koch(n,a,b)
print(b)

Solution (Python):
import math
sqrt3 = math.sqrt(3.0)

def koch_curve(n, i, pt1, pt2, pt_array):
    if i > n:
        return
    pt1x, pt1y = pt1
    pt2x, pt2y = pt2
    pts = (2/3 * pt1x + 1/3 * pt2x, 2/3 * pt1y + 1/3 * pt2y)
    ptt = (1/3 * pt1x + 2/3 * pt2x, 1/3 * pt1y + 2/3 * pt2y)
    ptx = ptt[0] - pts[0]
    pty = ptt[1] - pts[1]
    ptu = (1/2 * (ptx - sqrt3 * pty) + pts[0], 1/2 * (sqrt3 * ptx + pty) + pts[1])

    koch_curve(n, i+1, pt1, pts, pt_array)
    pt_array.append(pts)
    koch_curve(n, i+1, pts, ptu, pt_array)
    pt_array.append(ptu)
    koch_curve(n, i+1, ptu, ptt, pt_array)
    pt_array.append(ptt)
    koch_curve(n, i+1, ptt, pt2, pt_array)
    return

def main():
    n = int(input())
    pt_array = []
    begin = (0.0, 0.0)
    end = (100.0, 0.0)
    pt_array.append(begin)
    koch_curve(n, 1, begin, end, pt_array)
    pt_array.append(end)
    for pt in pt_array:
        print("{:.6f} {:.6f}".format(pt[0], pt[1]))

main()

Solution (Python):
import math

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

    s_x = (2 * p1[0] + 1 * p2[0])/3
    s_y = (2 * p1[1] + 1 * p2[1])/3
    s = [s_x, s_y]
    t_x = (1 * p1[0] + 2 * p2[0])/3
    t_y = (1 * p1[1] + 2 * p2[1])/3
    t = [t_x, t_y]
    rotated = math.pi * 60 / 180
    u_x = (t_x - s_x) * math.cos(rotated) - (t_y - s_y) * math.sin(rotated) + s_x
    u_y = (t_x - s_x) * math.sin(rotated) + (t_y - s_y) * math.cos(rotated) + s_y
    u = [u_x, u_y]

    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)
    

def main():
    n  = int(input())
    p1 = [0.00000000,0.00000000]
    p2 = [100.00000000,0.00000000]
    print(' '.join(map(str, p1)))
    koch(n, p1, p2)
    print(' '.join(map(str, p2)))
if __name__=='__main__':
    main()

Solution (Python):


"""
コッホ曲線
参考：https://www.mynote-jp.com/entry/2016/04/30/201249
　　　https://atarimae.biz/archives/23930
・回転行列を使うと、向きの変わった座標が出せる。
・そのために行列の計算方法も確認。
　→m行n列成分は、「左の行列のm行目」と「右の行列のn列目」の内積
"""

from math import sin, cos, radians

N = int(input())

def dfs(p1x, p1y, p2x, p2y, depth):
    if depth == N:
        return
    sx = p1x + (p2x-p1x)/3
    tx = p1x + (p2x-p1x)/3*2
    sy = p1y + (p2y-p1y)/3
    ty = p1y + (p2y-p1y)/3*2
    
    ux = sx + (tx-sx) * cos(radians(60)) - (ty-sy) * sin(radians(60))
    uy = sy + (tx-sx) * sin(radians(60)) + (ty-sy) * cos(radians(60))
    
    dfs(p1x, p1y, sx, sy, depth+1)
    print(sx, sy)
    dfs(sx, sy, ux, uy, depth+1)
    print(ux, uy)
    dfs(ux, uy, tx, ty, depth+1)
    print(tx, ty) 
    dfs(tx, ty, p2x, p2y, depth+1)

print(0, 0)
dfs(0, 0, 100, 0, 0)
print(100, 0)

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

class point:
    x = 0
    y = 0

def kock(k, p1, p2):
    s = point()
    t = point()
    u = point()
    if k == 0:
        return
    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)*math.cos(math.radians(60))-(t.y-s.y)*math.sin(math.radians(60))+s.x
    u.y = (t.x-s.x)*math.sin(math.radians(60))+(t.y-s.y)*math.cos(math.radians(60))+s.y
    kock(k-1, p1, s)
    print(s.x, s.y)
    kock(k-1, s, u)
    print(u.x, u.y)
    kock(k-1, u, t)
    print(t.x, t.y)
    kock(k-1, t, p2)


a = point()
b = point()
b.x = 100
print(a.x, a.y)
kock(n, a, b)
print(b.x, b.y)

Solution (Python):
from math import sqrt
n = int(input())
p1 = complex(0, 0)
p2 = complex(100, 0)
phi = complex(1/2, sqrt(3)/2)

def makeCurve(p1, p2, cnt):
    if cnt >= n:
        return
    s = p1 + (p2-p1)/3
    t = p1 + (p2-p1)*2/3
    u = (t-s)*phi+s
    
    
    makeCurve(p1, s, cnt + 1)
    print(s.real, s.imag)
    makeCurve(s, u, cnt + 1)
    print(u.real, u.imag)
    makeCurve(u, t, cnt + 1)
    print(t.real, t.imag)
    makeCurve(t, p2, cnt + 1)

print(p1.real, p1.imag)
makeCurve(p1, p2, 0)
print(p2.real, p2.imag)

Solution (Python):
import math
import collections

N = int(input())
Point = collections.namedtuple('Point', ['x', 'y'])


def koch(d, p1, p2):
    if d == 0:
        return
    th = math.pi * 60 / 180
    s = Point(x=(2 * p1.x + p2.x) / 3, y=(2 * p1.y + p2.y) / 3)
    t = Point(x=(p1.x + 2 * p2.x) / 3, y=(p1.y + 2 * p2.y) / 3)
    u = Point(x=(t.x - s.x) * math.cos(th) - (t.y - s.y) * math.sin(th) + s.x,
              y=(t.x - s.x) * math.sin(th) + (t.y - s.y) * math.cos(th) + s.y)
    koch(d - 1, p1, s)
    print('{:.8f} {:.8f}'.format(s.x, s.y))
    koch(d - 1, s, u)
    print('{:.8f} {:.8f}'.format(u.x, u.y))
    koch(d - 1, u, t)
    print('{:.8f} {:.8f}'.format(t.x, t.y))
    koch(d - 1, t, p2)


p1 = Point(x=0, y=0)
p2 = Point(x=100, y=0)
print('{:.8f} {:.8f}'.format(p1.x, p1.y))
koch(N, p1, p2)
print('{:.8f} {:.8f}'.format(p2.x, p2.y))

Solution (Python):
import math
N = int(input())
ans = [(0, 0)]
def rec(i, lx, ly, rx, ry):
    if i == N:
        return ans
    else:
        x1 = (2 * lx + rx) / 3
        y1 = (2 * ly + ry) / 3
        x3 = (2 * rx + lx) / 3
        y3 = (2 * ry + ly) / 3
        x2 = (x3 - x1) * math.cos(math.pi/3) \
             - (y3 - y1) * math.sin(math.pi/3) + x1
        y2 = (x3 - x1) * math.sin(math.pi/3) \
             + (y3 - y1) * math.cos(math.pi/3) + y1
        

        rec(i+1, lx, ly, x1, y1)
        ans.append((x1, y1))

        rec(i+1, x1, y1, x2, y2)
        ans.append((x2, y2))

        rec(i+1, x2, y2, x3, y3)
        ans.append((x3, y3))

        rec(i+1, x3, y3, rx, ry)

        return ans
        

answer = rec(0, 0, 0, 100, 0)
answer.append((100, 0))
for pair in answer:
    print(*pair)

Solution (Python):
l=map(complex,[0,1e2])
for _ in range(input()):
    t,l=l,[0]
    for i,j in zip(t[:-1],t[1:]):
        d=(j-i)/3
        l.extend((i+d,i+d+d*complex(1,3**0.5)/2,i+d*2,j))
for i in l:
    print i.real,i.imag

Solution (Python):
def next_koch(n):
    turn60 = 0.5 + 3 ** 0.5 * 0.5j
    n_next = [n[0]]
    for i in range(1, len(n)):
        s = (n[i - 1] * 2 + n[i]) / 3
        t = (n[i - 1] + n[i] * 2) / 3
        u = (t - s) * turn60 + s
        n_next +=[s, u, t, n[i]]
    return n_next

p1 = 0 + 0j
p2 = 100 + 0j
koch = [p1, p2]

n = int(input())
for i in range(n):
    koch = next_koch(koch)

for z in koch:
    print("{:.8f} {:.8f}".format(z.real, z.imag))

Solution (Python):
import math
sin60 = math.sin(60*math.pi/180)
def koch(n,p1,p2):
    if n==0:
        return
    s = [(2*p1[0]+1*p2[0])/3., (2*p1[1]+1*p2[1])/3.]
    t = [(1*p1[0]+2*p2[0])/3., (1*p1[1]+2*p2[1])/3.]
    u = [(t[0]-s[0])*0.5 - (t[1]-s[1])*sin60+ s[0],
        (t[0]-s[0])*sin60 + (t[1]-s[1])*0.5 + s[1]]
    koch(n-1,p1,s)
    print s[0],s[1]
    koch(n-1,s,u)
    print u[0],u[1]
    koch(n-1,u,t)
    print t[0],t[1]
    koch(n-1,t,p2)
       
n = int(raw_input())
p1 = (0,0)
p2 = (100,0)
print p1[0],p1[1]
koch(n,p1,p2)
print p2[0],p1[1]

Solution (Python):



import sys
import math


degree = math.pi / 3

def print_point(point):
    print '%f %f' % (point[0], point[1])

def koch(depth, p1, p2):
    if depth == 0:
        return
    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 = ((t[0] - s[0]) * math.cos(degree) - (t[1] - s[1]) * math.sin(degree) + s[0],
         (t[0] - s[0]) * math.sin(degree) + (t[1] - s[1]) * math.cos(degree) + s[1])
    depth -= 1
    koch(depth, p1, s)
    print_point(s)
    koch(depth, s, u)
    print_point(u)
    koch(depth, u, t)
    print_point(t)
    koch(depth, t, p2)

n_itr = int(sys.stdin.readline())
start = (0.0, 0.0)
end = (100.0, 0.0)
print_point(start)
koch(n_itr, start, end)
print_point(end)

Solution (Python):
import math


def koch(n, p1, p2):
    if n == 0:
        return
    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 = [(t[0] - s[0]) * math.cos(math.radians(60))
         - (t[1] - s[1]) * math.sin(math.radians(60)) + s[0],
         (t[0] - s[0]) * math.sin(math.radians(60))
         + (t[1] - s[1]) * math.cos(math.radians(60)) + s[1]]

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

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

Solution (Python):
import math
def kochCurve(A):
    B = []
    for i in xrange(len(A)-1):
        p1 = A[i]
        p2 = A[i+1]
        s = ((A[i][0] * 2.0 + A[i+1][0] * 1.0)/3, (A[i][1] * 2.0 + A[i+1][1] * 1.0)/3)
        t = ((A[i][0] * 1.0 + A[i+1][0] * 2.0)/3, (A[i][1] * 1.0 + A[i+1][1] * 2.0)/3)
        x = t[0] - s[0]
        y = t[1] - s[1]
        rad = math.radians(60)
        ux = x * math.cos(rad) - y * math.sin(rad)
        uy = x * math.sin(rad) + y * math.cos(rad)
        u = (ux + s[0], uy + s[1])
        B.extend([p1, s, u, t])
    B.append(A[len(A)-1])
    return B

n = int(raw_input())
A = [(0, 0), (100, 0)]
for i in xrange(n):
    A = kochCurve(A)
for x in A:
    print x[0], x[1]

Solution (Python):
import math
class point:
    def __init__(self):
        self.x = 0.0
        self.y = 0.0

def koch(n,a,b):
    if n == 0 :
        return

    rad = 1.0471975511965976 
    s = point()
    t = point()
    u = point()
    tmp = point()

    s.x = (2 * a.x + b.x)/3.0
    s.y = (2 * a.y + b.y)/3.0

    t.x = (2 * b.x + a.x)/3.0
    t.y = (2 * b.y + a.y)/3.0

    tmp.x,tmp.y = s.x-t.x ,s.y-t.y
    u.x = (tmp.x * math.cos(rad) + tmp.y * math.sin(rad)) + t.x
    u.y = (-tmp.x * math.sin(rad) + tmp.y * math.cos(rad)) + t.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)

    return

a = point()
b = point()
a.x,a.y = 0.0,0.0
b.x,b.y = 100.0,0.0
n = int(input())
print(a.x,a.y)
koch(n,a,b)
print(b.x,b.y)

Solution (Python):




SIN60 = 0.86602540378
COS60 = 0.5


class KochCurve(object):

    def __init__(self, n, p1=(0.0, 0.0), p2=(100.0, 0.0)):
        self.depth = n
        self.p1_init = p1
        self.p2_init = p2
        self.points = [p1]

    def koch(self, n=None, p1=None, p2=None):
        if n is None:
            n = self.depth
        if p1 is None:
            p1 = self.p1_init
        if p2 is None:
            p2 = self.p2_init
        if n != 0:
            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)
            ux = (t[0] - s[0]) * COS60 - (t[1] - s[1]) * SIN60 + s[0]
            uy = (t[0] - s[0]) * SIN60 + (t[1] - s[1]) * COS60 + s[1]
            u = (ux, uy)
            self.koch(n - 1, p1, s)
            self.points.append(s)
            self.koch(n - 1, s, u)
            self.points.append(u)
            self.koch(n - 1, u, t)
            self.points.append(t)
            self.koch(n - 1, t, p2)

    def draw_koch(self):
        self.koch()
        self.points.append(self.p2_init)


def main():
    k = KochCurve(n=int(input()))
    k.draw_koch()
    for p in k.points:
        print("{x:.8f} {y:.8f}".format(x=p[0], y=p[1]))


if __name__ == "__main__":
    main()

Solution (Python):


import math

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

    sx = (2*p1x + 1*p2x)/3
    sy = (2*p1y + 1*p2y)/3
    tx = (1*p1x + 2*p2x)/3
    ty = (1*p1y + 2*p2y)/3
    theta = math.pi/3
    ux = math.cos(theta)*(tx-sx) - math.sin(theta)*(ty-sy) + sx
    uy = math.sin(theta)*(tx-sx) + math.cos(theta)*(ty-sy) + sy

    koch(n-1, p1x, p1y, sx, sy)
    print("{:.5f} {:.5f}".format(sx, sy))
    koch(n-1, sx, sy, ux, uy)
    print("{:.5f} {:.5f}".format(ux, uy))
    koch(n-1, ux, uy, tx, ty)
    print("{:.5f} {:.5f}".format(tx, ty))
    koch(n-1, tx, ty, p2x, p2y)

n = int(input())
p1x, p1y = 0.0, 0.0
p2x, p2y = 100.0, 0.0
print("{:.5f} {:.5f}".format(p1x, p1y))
koch(n, p1x, p1y, p2x, p2y)
print("{:.5f} {:.5f}".format(p2x, p2y))

Solution (Python):
import math


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

    sx = (2*p1x + 1*p2x)/3
    sy = (2*p1y + 1*p2y)/3
    tx = (1*p1x + 2*p2x)/3
    ty = (1*p1y + 2*p2y)/3
    theta = math.pi/3
    ux = math.cos(theta)*(tx-sx) - math.sin(theta)*(ty-sy) + sx
    uy = math.sin(theta)*(tx-sx) + math.cos(theta)*(ty-sy) + sy

    koch(n-1, p1x, p1y, sx, sy)
    print("{:.5f} {:.5f}".format(sx, sy))
    koch(n-1, sx, sy, ux, uy)
    print("{:.5f} {:.5f}".format(ux, uy))
    koch(n-1, ux, uy, tx, ty)
    print("{:.5f} {:.5f}".format(tx, ty))
    koch(n-1, tx, ty, p2x, p2y)

n = int(input())

p1x, p1y = 0.0, 0.0
p2x, p2y = 100.0, 0.0

print("{:.5f} {:.5f}".format(p1x, p1y))
koch(n, p1x, p1y, p2x, p2y)
print("{:.5f} {:.5f}".format(p2x, p2y))

Solution (Python):
import math

n = int(raw_input())

th = math.pi * 60.0 / 180.0

def split(x,y):
    s = ((2*x[0]+y[0])/3, (2*x[1]+y[1])/3)
    t = ((x[0]+2*y[0])/3, (x[1]+2*y[1])/3)
    u = ((t[0]-s[0])*math.cos(th) - (t[1]-s[1])*math.sin(th) + s[0], (t[0]-s[0])*math.sin(th) + (t[1]-s[1])*math.cos(th) + s[1])
    return (s,u,t)


s = (0.,0.)
t = (100.,0.)
points = []
points.append(s)
points.append(t)
for i in range(n):
    new_points = []
    j = 0
    while True:
        s,u,t = split(points[j],points[j+1])
        new_points.extend((points[j],s,u,t))
        j += 1
        if j+1 >= len(points): break
    new_points.append(points[j])
    points = new_points
for p in points:
    print p[0],p[1]

Solution (Python):
import math

c60 = math.cos(math.radians(60))
s60 = math.sin(math.radians(60))

def koch(d, p1, p2):
    """ Print the coordinates of the vertices of Koch Curve.
    d is the depth of recursion.
    p1, p2 are coordinates of end point of the initial state.
    """
    if d == 0:
        return

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

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

    dx = tx - sx
    dy = ty - sy

    ux = dx * c60 - dy * s60 + sx
    uy = dx * s60 + dy * c60 + sy

    koch(d - 1, p1, (sx, sy))
    print("{0:.8f} {1:.8f}".format(sx, sy))

    koch(d - 1, (sx, sy), (ux, uy))
    print("{0:.8f} {1:.8f}".format(ux, uy))

    koch(d - 1, (ux, uy), (tx, ty))
    print("{0:.8f} {1:.8f}".format(tx, ty))

    koch(d - 1, (tx, ty), p2)


n = int(input())

ax = 0
ay = 0

bx = 100
by = 0

print("{0:.8f} {1:.8f}".format(ax, ay))
koch(n, (ax, ay), (bx, by))
print("{0:.8f} {1:.8f}".format(bx, by))

Solution (Python):

import math as m
N = int(input())



class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
def rotateX(s, t, theta):
    return m.cos(theta)*(t.x - s.x) - m.sin(theta)*(t.y - s.y) + s.x

def rotateY(s, t, theta):
    return m.sin(theta)*(t.x - s.x) + m.cos(theta)*(t.y - s.y) + s.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)
    u = Point(rotateX(s, t, m.pi/3), rotateY(s, t, m.pi/3))
    
    
    
    koch(n-1, p1, s)
    print("{0:.8f} {1:.8f}".format(s.x, s.y))
    koch(n-1, s, u)
    print("{0:.8f} {1:.8f}".format(u.x, u.y))
    koch(n-1, u, t)
    print("{0:.8f} {1:.8f}".format(t.x, t.y))
    koch(n-1, t, p2)

p1 = Point(0, 0)
p2 = Point(100, 0)
print("{0:.8f} {1:.8f}".format(p1.x, p1.y))
koch(N, p1, p2)
print("{0:.8f} {1:.8f}".format(p2.x, p2.y))

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

sin30 = 1 / 2
cos30 = sqrt(3) / 2
""" Direct length p1-u when p1-p2 is 1 """
p1_to_u = 1 / sqrt(3)


def koch(xy1, xy2):
    global sin30, cos30, p1_to_u
    (x1, y1) = xy1
    (x2, y2) = xy2
    dx, dy = x2 - x1, y2 - y1
    dx3, dy3 = dx / 3, dy / 3
    yield (x1 + dx3, y1 + dy3)
    xu, yu = dx * p1_to_u, dy * p1_to_u
    xu, yu = x1 + cos30 * xu - sin30 * yu, y1 + sin30 * xu + cos30 * yu
    yield (xu, yu)
    yield (x2 - dx3, y2 - dy3)


l = deque(((0.0, 0.0), (100.0, 0.0)))

for _ in range(int(input())):
    new_l = deque()
    prev_xy = l.popleft()
    for xy in l:
        new_l.append(prev_xy)
        new_l.extend(koch(prev_xy, xy))
        prev_xy = xy
    new_l.append(prev_xy)
    l = new_l

for xy in l:
    print(*xy)

Solution (Python):
from math import sqrt

n = int(input())
sin60 = sqrt(3) / 2
cos60 = 1 / 2


def koch(x1, y1, x2, y2, depth):
    global sin60, cos60, n
    if depth > n:
        return
    dx, dy = x2 - x1, y2 - y1
    dx3, dy3 = dx / 3, dy / 3
    xs, ys = x1 + dx3, y1 + dy3
    xt, yt = x2 - dx3, y2 - dy3
    xu, yu = xs + cos60 * dx3 - sin60 * dy3, ys + sin60 * dx3 + cos60 * dy3
    depth += 1
    for xy in koch(x1, y1, xs, ys, depth):
        yield xy
    yield (xs, ys)
    for xy in koch(xs, ys, xu, yu, depth):
        yield xy
    yield (xu, yu)
    for xy in koch(xu, yu, xt, yt, depth):
        yield xy
    yield (xt, yt)
    for xy in koch(xt, yt, x2, y2, depth):
        yield xy


print(0.0, 0.0)
for xy in koch(0.0, 0.0, 100.0, 0.0, 1):
    print(*xy)
print(100.0, 0.0)

Solution (Python):
import math

rad = math.radians(60)

class coordinates:
  def __init__(self, x, y):
    self.x = x
    self.y = y
 
def koch(n, a, b):
  if n == 0:
    return
  s = coordinates((2.0*a.x + b.x)/3.0, (2.0*a.y + b.y)/3.0)
  t = coordinates((a.x + 2.0*b.x)/3.0, (a.y + 2.0*b.y)/3.0)
  ux = (t.x - s.x)*math.cos(rad) - (t.y - s.y)*math.sin(rad) + s.x
  uy = (t.x - s.x)*math.sin(rad) + (t.y - s.y)*math.cos(rad) + s.y
  u = coordinates(ux, uy)
  
  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)
  
n = input()
  
p1 = coordinates(0.0, 0.0)
p2 = coordinates(100.0, 0.0)
  
print p1.x, p1.y
koch(n, p1, p2)
print p2.x, p2.y

Solution (Python):
import math
T = []

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


def hoge(p,q):
    ax = q.x-p.x
    ay = q.y-p.y
    bx = ax/2 - ay*math.sqrt(3)/2
    by = ax*math.sqrt(3)/2 + ay/2
    nb = Vect(p.x+bx,p.y+by)
    return nb

def koch(p,q,n):
    if n == 0:
        T.append(p)
    else:
        s = Vect(p.x+(q.x-p.x)/3,p.y+(q.y-p.y)/3)
        t = Vect(p.x+2*(q.x-p.x)/3, p.y+2*(q.y-p.y)/3)
        u = hoge(s,t)
        koch(p,s,n-1)
        koch(s,u,n-1)
        koch(u,t,n-1)
        koch(t,q,n-1)
        
n = int(input())

koch(Vect(0,0),Vect(100,0),n)
T.append(Vect(100,0))

for i in T:
    print("%.8f %.8f"%(i.x,i.y))

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

p1_x = float(0)
p1_y = float(0)
p2_x = float(100)
p2_y = float(0)

def KC(i, n, x1, y1, x2, y2):
	v = [0]*5
	w = [0]*5
	if i < n:
		v[0] = x1
		w[0] = y1
		v[1] = (2*x1+x2)/3.0
		w[1] = (2*y1+y2)/3.0
		v[2] = (x1+2*x2)/3.0
		w[2] = (y1+2*y2)/3.0
		v[3] = (v[2] - v[1])*math.cos(math.pi/3) - (w[2] - w[1])*math.sin(math.pi/3) + v[1]
		w[3] = (w[2] - w[1])*math.cos(math.pi/3) + (v[2] - v[1])*math.sin(math.pi/3) + w[1]
		v[4] = x2
		w[4] = y2
		KC(i+1, n, v[0], w[0], v[1], w[1])
		print v[1], w[1]
		KC(i+1, n, v[1], w[1], v[3], w[3])
		print v[3], w[3]
		KC(i+1, n, v[3], w[3], v[2], w[2])
		print v[2], w[2]
		KC(i+1, n, v[2], w[2], v[4], w[4])

print p1_x, p1_y
KC(0, n, p1_x, p1_y, p2_x, p2_y)
print p2_x, p2_y

Solution (Python):
import math

n = int(raw_input())

def func(i, ox1, oy1, ox2, oy2):
    if i != n:
        nx1 = ox1 + (ox2 - ox1) / 3
        ny1 = oy1 + (oy2 - oy1) / 3
        nx2 = ox1 + (ox2 - ox1) * 2 / 3
        ny2 = oy1 + (oy2 - oy1) * 2 / 3
        if nx1 < nx2:
            if ny1 == ny2:
                nx3 = nx1 + (nx2 - nx1) / 2
                ny3 = ny1 + (nx2 - nx1) * math.sqrt(3) / 2
            elif ny1 < ny2:
                nx3 = ox1
                ny3 = ny2
            elif ny1 > ny2:
                nx3 = ox2
                ny3 = ny1
        elif nx1 > nx2:
            if ny1 == ny2:
                nx3 = nx1 + (nx2 - nx1) / 2
                ny3 = ny1 - (nx1 - nx2) * math.sqrt(3) / 2
            elif ny1 < ny2:
                nx3 = ox2
                ny3 = ny1
            elif ny1 > ny2:
                nx3 = ox1
                ny3 = ny2
        func(i+1, ox1, oy1, nx1, ny1)
        func(i+1, nx1, ny1, nx3, ny3)
        func(i+1, nx3, ny3, nx2, ny2)
        func(i+1, nx2, ny2, ox2, oy2)
    elif i == n:
        print(str(ox1) + str(" ") +  str(oy1))

func(0, 0.0, 0.0, 100.0, 0.0)
print(str(100) + str(" ") + str(0))

Solution (Python):
import math

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

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

    s = Point(0, 0)
    t = Point(0, 0)
    u = Point(0, 0)

    s.x = (2 * p1.x + p2.x) / 3.0
    s.y = (2 * p1.y + p2.y) / 3.0
    t.x = (p1.x + p2.x * 2) / 3.0
    t.y = (p1.y + p2.y * 2) / 3.0
    u.x = (t.x - s.x) * math.cos(math.pi / 3) - (t.y - s.y) * math.sin(math.pi / 3) + s.x
    u.y = (t.x - s.x) * math.sin(math.pi / 3) + (t.y - s.y) * math.cos(math.pi / 3) + s.y

    kock(n - 1, p1, s)
    print "%.8f %.8f" % (s.x, s.y)
    kock(n - 1, s, u)
    print "%.8f %.8f" % (u.x, u.y)
    kock(n - 1, u, t)
    print "%.8f %.8f" % (t.x, t.y)
    kock(n - 1, t, p2)

n = input()

p1 = Point(0, 0)
p2 = Point(100, 0)

print "%.8f %.8f" % (p1.x, p1.y)
kock(n, p1, p2)
print "%.8f %.8f" % (p2.x, p2.y)

Solution (Python):
import math
          
def koch(n, h, i, j, k):
 if n == 0:
  return
           
 sx = (h*2+j)/3
 sy = (i*2+k)/3
 tx = (h+j*2)/3
 ty = (i+k*2)/3
 ux = (tx-sx)*math.cos(math.pi/3) - (ty-sy)*math.sin(math.pi/3) + sx
 uy = (tx-sx)*math.sin(math.pi/3) + (ty-sy)*math.cos(math.pi/3) + sy
          
 koch(n-1, h, i, sx, sy)
 print sx, sy
 koch(n-1, sx, sy, ux, uy)
 print ux, uy
 koch(n-1, ux, uy, tx, ty)
 print tx, ty
 koch(n-1, tx, ty, j, k)
   
n = input()
          
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
          
print px, py
koch(n, px, py, qx, qy)
print qx, qy

Solution (Python):
import math
n = input()
 
p1_x = float(0)
p1_y = float(0)
p2_x = float(100)
p2_y = float(0)
 

def kock(i, n, x1, y1, x2, y2):
    a = [0,0,0,0,0]
    b = [0,0,0,0,0]
    if i < n:
        a[0] = x1
        b[0] = y1
        a[1] = (2*x1+x2)/3.0
        b[1] = (2*y1+y2)/3.0
        a[2] = (x1+2*x2)/3.0
        b[2] = (y1+2*y2)/3.0
        a[3] = (a[2] - a[1])*math.cos(math.pi/3) - (b[2] - b[1])*math.sin(math.pi/3) + a[1]
        b[3] = (b[2] - b[1])*math.cos(math.pi/3) + (a[2] - a[1])*math.sin(math.pi/3) + b[1]
        a[4] = x2
        b[4] = y2
        kock(i+1, n, a[0], b[0], a[1], b[1])
        print a[1], b[1]
        kock(i+1, n, a[1], b[1], a[3], b[3])
        print a[3], b[3]
        kock(i+1, n, a[3], b[3], a[2], b[2])
        print a[2], b[2]
        kock(i+1, n, a[2], b[2], a[4], b[4])

print p1_x, p1_y
kock(0, n, p1_x, p1_y, p2_x, p2_y)
print p2_x, p2_y

Solution (Python):
def koch(n, a_x, a_y, b_x, b_y):
  if n == 0:
    return
 
  th = math.pi*60.0/180.0
 
  s_x = (2.0*a_x + 1.0*b_x)/3.0
  s_y = (2.0*a_y + 1.0*b_y)/3.0
  t_x = (1.0*a_x + 2.0*b_x)/3.0
  t_y = (1.0*a_y + 2.0*b_y)/3.0
  u_x = (t_x - s_x)*math.cos(th) - (t_y - s_y)*math.sin(th) + s_x
  u_y = (t_x - s_x)*math.sin(th) + (t_y - s_y)*math.cos(th) + s_y
 
  koch(n-1, a_x, a_y, s_x, s_y)
  print '%.8f %.8f' % (s_x, s_y)
  koch(n-1, s_x, s_y, u_x, u_y)
  print '%.8f %.8f' % (u_x, u_y)
  koch(n-1, u_x, u_y, t_x, t_y)
  print '%.8f %.8f' % (t_x, t_y)
  koch(n-1, t_x, t_y, b_x, b_y)
 
 

import math
n = input()
a_x = 0.0
a_y = 0.0
b_x = 100.0
b_y = 0.0
 
print '%.8f %.8f' % (a_x, a_y)
koch(n, a_x, a_y, b_x, b_y)
print '%.8f %.8f' % (b_x, b_y)

Solution (Python):
def koch(n, ax, ay, bx, by):
    if n == 0:
        return
    
    th = math.pi * 60.0 / 180.0
    
    xx = (2.0 * ax + bx)/3.0
    xy = (2.0 * ay + by)/3.0
    zx = (ax + 2.0 * bx)/3.0
    zy = (ay + 2.0 * by)/3.0
    yx = (zx - xx)*math.cos(th) - (zy - xy)*math.sin(th) + xx
    yy = (zx - xx)*math.sin(th) + (zy - xy)*math.cos(th) + xy
    
    koch(n-1, ax, ay, xx, xy)
    print xx,xy
    koch(n-1, xx, xy, yx, yy)
    print yx,yy
    koch(n-1, yx, yy, zx, zy)
    print zx,zy
    koch(n-1, zx, zy, bx, by)
    
import math
n = int(raw_input())
ax = 0.0
ay = 0.0
bx = 100.0
by = 0.0

print ax,ay
koch(n, ax, ay, bx, by)
print bx,by

Solution (Python):

def vector_u(vc_p1 = [], vc_p2 = []):
    vc_p1s = [(vc_p2[0] - vc_p1[0])/3.0, (vc_p2[1] - vc_p1[1])/3.0]
    vc_su  = [vc_p1s[0]*0.5 - vc_p1s[1]*0.866025, vc_p1s[0]*0.866025 + vc_p1s[1]*0.5]
    vc_u   = [vc_p1[0] + vc_p1s[0] + vc_su[0], vc_p1[1] + vc_p1s[1] + vc_su[1]]
    return vc_u

def vector_s(vc_p1 = [], vc_p2 = []):
    vc_s = [(2.0*vc_p1[0] + vc_p2[0])/3.0, (2.0*vc_p1[1] + vc_p2[1])/3.0]
    return vc_s

def vector_t(vc_p1 = [], vc_p2 = []):
    vc_t = [(vc_p1[0] + 2.0*vc_p2[0])/3.0, (vc_p1[1] + 2.0*vc_p2[1])/3.0]
    return vc_t

def koch_curve(p1 = [0.00000, 0.00000], p2 = [100.00000, 0.00000], n = 0):
    s = vector_s(p1, p2)
    u = vector_u(p1, p2)
    t = vector_t(p1, p2)
    
    if n == 0:
        print(str( s[0]) + " " + str( s[1]))
        print(str( u[0]) + " " + str( u[1]))
        print(str( t[0]) + " " + str( t[1]))
        print(str(p2[0]) + " " + str(p2[1]))
    else:
        koch_curve(p1,  s, n-1)
        koch_curve( s,  u, n-1)
        koch_curve( u,  t, n-1)
        koch_curve( t, p2, n-1)

n = int(raw_input())

p1 = [  0.0, 0.0]
p2 = [100.0, 0.0]

print(str(p1[0]) + " " + str(p1[1]))
if n == 0:
    print(str(p2[0]) + " " + str(p2[1]))
else:
    koch_curve(p1, p2, n-1)