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):
from collections import deque
if __name__ == "__main__":
    new_lines = deque([[[0.0, 0.0], [100.0, 0.0]]])
    n = int(input())
    sin60 = 3**0.5/2.0
    cos60 = 1.0/2.0
    for _ in range(n):
        lines = new_lines
        new_lines = deque()
        while lines:
            t1, t2 = lines.popleft()
            n1 = [(2.0*t1[0]+t2[0])/3.0, (2.0*t1[1]+t2[1])/3.0]
            n3 = [(t1[0]+2.0*t2[0])/3.0, (t1[1]+2.0*t2[1])/3.0]
            x, y = [n3[0]-n1[0], n3[1]-n1[1]]
            x60, y60 = [cos60*x - sin60*y ,sin60*x + cos60*y]
            n2 = [n1[0]+x60, n1[1]+y60]
            new_lines.append([t1, n1])
            new_lines.append([n1, n2])
            new_lines.append([n2, n3])
            new_lines.append([n3, t2])
    coordinates = list(zip(*new_lines))[0] + ([100.0, 0.0],)
    for coordinate in coordinates:
        print(*coordinate)

Solution (Python):
import math
def koch(n, p1, p2):
    if n == 0:
        return
    sx = (2*p1[0]+p2[0])/3
    sy = (2*p1[1]+p2[1])/3
    s = (sx, sy)
    tx = (p1[0]+2*p2[0])/3
    ty = (p1[1]+2*p2[1])/3
    t = (tx, ty)
    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
    u = (ux, uy)
    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(0, 0)
koch(n, p1, p2)
print(100, 0)

Solution (Python):
n = int(input())
p1, p2 = (0, 0), (100, 0)
ans_ls = [p1, p2]

def koch(d, p1, p2):
    '''
    d...現在の深さ 0スタートにする
    p1,p2...端の座標。ここから途中の座標を計算しlistにappendする
    '''
    
    if d == n:
        return
    
    s = (2/3 * p1[0] + 1/3 * p2[0], 2/3 * p1[1] + 1/3 * p2[1])
    t = (1/3 * p1[0] + 2/3 * p2[0], 1/3 * p1[1] + 2/3 * p2[1])

    
    
    R = [[1/2, -0.86602540378], [0.86602540378, 1/2]]
    u = (R[0][0] * (t[0] - s[0]) + R[0][1] * (t[1] - s[1]) + s[0],
         R[1][0] * (t[0] - s[0]) + R[1][1] * (t[1] - s[1]) + s[1])
    ans_ls.extend([s, u, t])  

    
    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)

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

Solution (Python):
import math

def koch(start, end, n, r):
    if n > 0:
        
        a = [(start[0]*2 + end[0]*1) / 3, (start[1]*2 + end[1]*1) / 3]
        c = [(start[0]*1 + end[0]*2) / 3, (start[1]*1 + end[1]*2) / 3]

        x = c[0] - a[0]
        y = c[1] - a[1]

        b = [x * math.cos(math.pi/3) - y * math.sin(math.pi/3) + a[0], x * math.sin(math.pi/3) + y * math.cos(math.pi/3) + a[1]]

        koch(start, a, n-1, r)
        koch(a, b, n-1, r)
        koch(b, c, n-1, r)
        koch(c, end, n-1, r)

    else:
        r.append(start)

n = int(input())

r = []

koch([0, 0], [100, 0], n, r)
r.append([100, 0])

for rr in r:
    print(str(rr[0]) + ' ' + str(rr[1]))

Solution (Python):
import math
def rot60(s, t):
    v = t - s
    a = 1/2 + complex(0,(math.sqrt(3)/2))
    return v * a + s

def pr(p):
    x = p.real
    y = p.imag
    print('%.10f %.10f'%(x, y))

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

    s = (p2 - p1) * (1/3) + p1
    t = (p2 - p1) * (2/3) + p1
    u = rot60(s, t)
    dfs(p1, s, n - 1)
    pr(s)
    dfs(s, u, n - 1)
    pr(u)
    dfs(u, t, n - 1)
    pr(t)
    dfs(t, p2, n -1)
    
n = int(input())
s = (0 + 0j)
t = (100 + 0j)
pr(s)
dfs(s, t, n) 
pr(t)

Solution (Python):
"""
コッホ曲線を構成する
numpyは使えないっぽい
"""
import math

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


def outPut(point):
    print("%.8f %.8f"%(point.x, point.y))

def calc(depth, left, right):
    dif_x = right.x-left.x
    dif_y = right.y-left.y

    nleft_x = left.x+dif_x/3
    nleft_y = left.y+dif_y/3

    nright_x = right.x-dif_x/3
    nright_y = right.y-dif_y/3

    
    top_x = (dif_x*math.cos(math.pi/3)-dif_y*math.sin(math.pi/3))/3+nleft_x
    top_y = (dif_x*math.sin(math.pi/3)+dif_y*math.cos(math.pi/3))/3+nleft_y

    new_left = Point(nleft_x, nleft_y)
    new_right = Point(nright_x, nright_y)
    tmp_top = Point(top_x, top_y)

    if depth > 1:
        calc(depth-1, left, new_left)
        calc(depth-1, new_left, tmp_top)
        calc(depth-1, tmp_top, new_right)
        calc(depth-1, new_right, right)
    else: 
        outPut(left)
        outPut(new_left)
        outPut(tmp_top)
        outPut(new_right) 

n = int(input())
start = Point(0, 0)
end = Point(100, 0)

if n == 0:
    outPut(start)
    outPut(end)
else:
    calc(n, start, end)
    outPut(end)

Solution (Python):
import math

n = int(input())

def koch(c, px, py, qx, qy):
  if c > 0:
    sx = (2*px+qx)/3
    sy = (2*py+qy)/3
    tx = (px+2*qx)/3
    ty = (py+2*qy)/3
    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
    koch(c-1, px, py, sx, sy)
    S = [sx, sy]
    print(" ".join(map(str, S)))
    koch(c-1, sx, sy, ux, uy)
    U = [ux, uy]
    print(" ".join(map(str, U)))    
    koch(c-1, ux, uy, tx, ty)
    T = [tx, ty]
    print(" ".join(map(str, T)))
    koch(c-1, tx, ty, qx, qy)

print("0 0")
koch(n, 0, 0, 100, 0)
print("100 0")

Solution (Python):
import math

def koch(d, a, b):
    if d== 0:
        return
    s=[0,0]
    t=[0,0]
    u=[0,0]
    
    s[0] = (2.0*a[0]+1.0*b[0])/3.0
    s[1] = (2.0*a[1]+1.0*b[1])/3.0
    t[0] = (1.0*a[0]+2.0*b[0])/3.0
    t[1] = (1.0*a[1]+2.0*b[1])/3.0
    u[0] = (t[0] - s[0])*math.cos(math.radians(60))-(t[1] - s[1])*math.sin(math.radians(60)) + s[0]
    u[1] = (t[0] - s[0])*math.sin(math.radians(60))+(t[1] - s[1])*math.cos(math.radians(60)) + s[1]
    
    koch(d-1,a,s)
    print("{:.8f}".format(s[0]), "{:.8f}".format(s[1]))  
    koch(d-1,s,u)
    print("{:.8f}".format(u[0]), "{:.8f}".format(u[1]))  
    koch(d-1,u,t)      
    print("{:.8f}".format(t[0]), "{:.8f}".format(t[1]))  
    koch(d-1, t, b)

n = int(input())

p1=[0,0]
p2=[100,0]

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

Solution (Python):
import math

def rot60(s, t):
    v = t - s
    a = 1/2 + complex(0, (math.sqrt(3)/2))
    return v * a + s

def pr(p):
    x = p.real
    y = p.imag
    print('%.10f %.10f'%(x,y))

def dfs(p1, p2, n):
    if n == 0:
        return
    s = (p2-p1)*(1/3) + p1
    t = (p2-p1)*(2/3) + p1
    u = rot60(s, t)
    dfs(p1, s, n-1)
    pr(s)
    dfs(s, u, n-1)
    pr(u)
    dfs(u, t, n-1)
    pr(t)
    dfs(t, p2, n-1)

n = int(input())
s = (0 + 0j)
t = (100 + 0j)
pr(s)
dfs(s, t, n)
pr(t)

Solution (Python):
import math

def make_3_points(p1, p2):
    """p1, p2 を分割する３点を作成する。
    
    p1, p2 を両端の点とする線分を３等分する２点 s, t を
    頂点とする正三角形の頂点 (s, u, t) のタプルを返す。
    """
    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)

    theta = math.radians(60)

    x = t[0] - s[0]
    y = t[1] - s[1]

    u = (math.cos(theta) * x - math.sin(theta) * y, math.sin(theta) * x + math.cos(theta) * y)
    u = (u[0] + s[0], u[1] + s[1])

    return (s, u, t)

def make_koch(n, pl):
    if n == 0:
        return pl
    next_pl = [pl[0]]
    for i in range(1, len(pl)):
        s, u, t = make_3_points(next_pl[-1], pl[i])
        next_pl += [s, u, t, pl[i]]
    
    pl = None
    return make_koch(n - 1, next_pl)

n = int(input())
rl = make_koch(n, [(0, 0), (100, 0)])
for p1, p2 in rl:
    print(p1, p2)

Solution (Python):

import math

n = int(input())
def koch(d, p1, p2):
    if d == 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]) * 0.5 - (t[1] - s[1]) * 0.866025403 + s[0],
        (t[0] - s[0]) * 0.866025403 + (t[1] - s[1]) * 0.5 + 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)

print(0.0, 0.0)
koch(n, [0, 0], [100, 0])
print(100.0, 0.0)

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

A = []
def koch(d, p1, p2):
    rot = math.radians(60)
    if d == 0:
        return 0
    else:
        s = ((2.0 * p1[0] + p2[0]) / 3.0, (2.0 * p1[1] + p2[1]) / 3.0)
        t = ((2.0 * p2[0] + p1[0]) / 3.0, (2.0 * p2[1] + p1[1]) / 3.0)
        ux = (t[0] - s[0]) * math.cos(rot) - (t[1] - s[1]) * math.sin(rot) + s[0]
        uy = (t[0] - s[0]) * math.sin(rot) + (t[1] - s[1]) * math.cos(rot) + s[1]
        u = (ux, uy)
        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)

p1 = (0.0, 0.0)
p2 = (100.0, 0.0)
print(p1[0], p1[1])
koch(n, p1, p2)
print(p2[0], p2[1])

Solution (Python):
import math


class vec2d(object):
    def __init__(self, x, y) -> None:
        self.x = float(x)
        self.y = float(y)

    def rot(self, theta: float):
        newx = self.x * math.cos(theta) - self.y * math.sin(theta)
        newy = self.x * math.sin(theta) + self.y * math.cos(theta)
        return vec2d(newx, newy)

    def __str__(self):
        return ' '.join(map(str, [self.x, self.y]))

    def __format__(self, format_spec):
        return ' '.join(map(lambda x: format(x, format_spec), [self.x, self.y]))

    def __add__(self, other):
        return vec2d(self.x + other.x, self.y + other.y)

    def __sub__(self, other):
        return vec2d(self.x - other.x, self.y - other.y)

    def __mul__(self, k):
        return vec2d(self.x * k, self.y * k)

    def __truediv__(self, k):
        return self.__mul__(1 / k)

    def prod(self, other):
        return self.x * other.x + self.y * other.y


def cochcurve(p1, p2, n):
    if n > 0:
        pd = (p2 - p1) / 3
        ps = p1 + pd
        pu = ps + pd.rot(math.pi / 3)
        pt = ps + pd
        cochcurve(p1, ps, n-1)
        print(f'{ps:>12.6f}')
        cochcurve(ps, pu, n-1)
        print(f'{pu:>12.6f}')
        cochcurve(pu, pt, n-1)
        print(f'{pt:>12.6f}')
        cochcurve(pt, p2, n-1)


n = int(input())
S = vec2d(0, 0)
T = vec2d(100, 0)
print(f'{S:>12.6f}')
cochcurve(S, T, n)
print(f'{T:>12.6f}')

Solution (Python):
import math

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

def koch(d, p1, p2):
    

    if d == 0:
        return
    
    t = Point((p1.x + 2 * p2.x )/3,(p1.y + 2 * p2.y )/3 )
    s = Point((p2.x + 2 * p1.x )/3,(p2.y + 2 * p1.y )/3 )
    
    u = Point((t.x-s.x) * (math.cos(math.pi / 3)) - (t.y-s.y) * (math.sin(math.pi / 3)) + s.x, (t.x-s.x) * (math.sin(math.pi / 3)) + (t.y-s.y) * (math.cos(math.pi / 3)) + s.y)
    
    

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


n = int(input())
    
p1 = Point(0, 0)
p2 = Point(100, 0)

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

Solution (Python):
import math
n=int(input())
def koch(d,p1,p2):
	if d==0:
		return
	s=[0,0]
	t=[0,0]
	u=[0,0]
	s[0]=2/3*p1[0]+1/3*p2[0]
	s[1]=2/3*p1[1]+1/3*p2[1]
	t[0]=1/3*p1[0]+2/3*p2[0]
	t[1]=1/3*p1[1]+2/3*p2[1]
	u[0]=s[0]+(t[0]-s[0])*math.cos(math.pi/3)-(t[1]-s[1])*math.sin(math.pi/3)
	u[1]=s[1]+(t[0]-s[0])*math.sin(math.pi/3)+(t[1]-s[1])*math.cos(math.pi/3)
	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)
print(0,0)
koch(n,[0,0],[100,0])
print(100,0)

Solution (Python):
import math

def koch(start,end,n):
  if n==0:
    return
  else:
    point1=[0,0]
    point2=[0,0]
    point3=[0,0]
    
    point1[0]=(end[0]-start[0])/3 +start[0]
    point1[1]=(end[1]-start[1])/3 +start[1]
    
    point3[0]=(end[0]-start[0])*2/3+start[0]
    point3[1]=(end[1]-start[1])*2/3+start[1]
    
    rad60 = math.radians(60)
    point2[0]=(point3[0]-point1[0])* math.cos(rad60) -(point3[1]-point1[1])*math.sin(rad60) +point1[0]
    point2[1]=(point3[0]-point1[0])* math.sin(rad60) +(point3[1]-point1[1])*math.cos(rad60) +point1[1]
    
    koch(start,point1,n-1)
    print(*point1)
    koch(point1,point2,n-1)
    print(*point2)
    koch(point2,point3,n-1)
    print(*point3)
    koch(point3,end,n-1)

n=int(input())

start=[0,0]
end=[100,0]

print(*start)

if n>0:
  koch(start,end,n)
  

print(*end)

Solution (Python):
from math import*
a=(0,0)
b=(100,0)

def koch(n,ax,ay,bx,by):
    if n==0:
        return 0
    th=pi/3
    sx=(2*ax+bx)/3
    sy=(2*ay+by)/3
    tx=(ax+2*bx)/3
    ty=(ay+2*by)/3
    ux=(tx-sx)*cos(th)-(ty-sy)*sin(th)+sx
    uy=(tx-sx)*sin(th)+(ty-sy)*cos(th)+sy
    
    koch(n-1,ax,ay,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,bx,by)


n=int(input())
print(*a)
koch(n,*a,*b)
print(*b)

Solution (Python):
from math import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def div3(a,b):
    c1=(2*a+b)/3
    c2=(a+2*b)/3
    return [c1,c2]

def main():
    n=int(input())
    xy=[[0,0],[100,0]]
    
    for _ in range(n):
        nxy=[[0,0]]
        for [x0,y0],[x4,y4] in zip(xy,xy[1:]):
            x1,x3=div3(x0,x4)
            y1,y3=div3(y0,y4)
            a=((x3-x1)**2+(y3-y1)**2)**0.5
            rad=atan2(y3-y1,x3-x1)
            x2=x1+a*cos(rad+pi/3)
            y2=y1+a*sin(rad+pi/3)
            nxy+=[[x1,y1],[x2,y2],[x3,y3],[x4,y4]]
        xy=nxy
    for x,y in xy:
        print(x,y)

main()

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

start = (0,0)
end = (100,0)

def koch(n,p1,p2):
    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 )

    ux = (t[0] - s[0])/2 - (t[1] - s[1])*math.sqrt(3)/2 + s[0]
    uy = (t[0] - s[0])*math.sqrt(3)/2 + (t[1] - s[1])/2 + s[1]
    u = (ux, uy)
    koch(n-1,p1, s)
    ppp(s)
    koch(n-1, s, u)
    ppp(u)
    koch(n-1, u, t)
    ppp(t)
    koch(n-1, t,p2)

def ppp(t):
    print("{:.08f} {:.08f}".format(t[0],t[1]))

ppp(start)
koch(n,start,end)
ppp(end)

Solution (Python):

import math


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

    def print(self):
        print(round(self.x, 8), round(self.y, 8))


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

    s = Point()
    u = Point()
    t = Point()

    theta = math.radians(60.0)

    s.x = p1.x * 2.0/3.0 + p2.x * 1.0/3.0
    s.y = p1.y * 2.0/3.0 + p2.y * 1.0/3.0

    t.x = p1.x * 1.0/3.0 + p2.x * 2.0/3.0
    t.y = p1.y * 1.0/3.0 + p2.y * 2.0/3.0

    u.x = (t.x - s.x) * math.cos(theta) - (t.y - s.y) * math.sin(theta) + s.x
    u.y = (t.x - s.x) * math.sin(theta) + (t.y - s.y) * math.cos(theta) + s.y

    koch(d-1, p1, s)
    s.print()
    koch(d-1, s, u)
    u.print()
    koch(d-1, u, t)
    t.print()
    koch(d-1, t, p2)


N = int(input())

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

p1.print()
koch(N, p1, p2)
p2.print()

Solution (Python):
from math import cos,sin,radians

n=int(input())

def koch(n):
    if n==0:
        return [[0,0],[100,0]]
    points=koch(n-1)
    new_points=[]
    for i in range(len(points)-1):
        a_x=points[i][0]
        a_y=points[i][1]
        b_x=points[i+1][0]
        b_y=points[i+1][1]
        c_x,c_y=a_x*2/3+b_x/3,a_y*2/3+b_y/3
        d_x,d_y=a_x/3+b_x*2/3,a_y/3+b_y*2/3
        e_x=(d_x-c_x)*cos(radians(60))-(d_y-c_y)*sin(radians(60))+c_x
        e_y=(d_x-c_x)*sin(radians(60))+(d_y-c_y)*cos(radians(60))+c_y
        if [a_x,a_y] not in new_points:
            new_points.append([a_x,a_y])
        new_points.append([c_x,c_y])
        new_points.append([e_x,e_y])
        new_points.append([d_x,d_y])
        if [b_x,b_y] not in new_points:
            new_points.append([b_x,b_y])
    return new_points

for i in koch(n):
    print(*i)

Solution (Python):
import math
def kock(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]
    kock(n-1,p1,s)
    print(*s)
    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]]
    kock(n-1,s,u)
    print(*u)
    kock(n-1,u,t)
    print(*t)
    kock(n-1,t,p2)

n=int(input())
p1=[0,0]
p2=[100,0]
print(*p1)
kock(n,p1,p2)
print(*p2)

Solution (Python):
import math
class KochCurve(object):
	x = 0.0
	y = 0.0
	def koch(self, n, a, b):
		if n == 0:
			return 
		s = KochCurve()
		t = KochCurve()
		u = KochCurve()
		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	
		KochCurve().koch(n - 1, a, s)
		msg = "%.8f %.8f" % (s.x, s.y)
		print(msg)
		KochCurve().koch(n - 1, s, u)
		msg = "%.8f %.8f" % (u.x, u.y)
		print(msg)
		KochCurve().koch(n - 1, u, t)
		msg = "%.8f %.8f" % (t.x, t.y)
		print(msg)
		KochCurve().koch(n - 1, t, b)
a = KochCurve()
b = KochCurve()
n = int(input())
a.x = 0
a.y = 0
b.x = 100
b.y = 0
msg = "%.8f %.8f" % (a.x, a.y)
print(msg)
KochCurve().koch(n, a, b)
msg = "%.8f %.8f" % (b.x, b.y)
print(msg)

Solution (Python):

import math

class KochCurve(object):
	
	x = 0.0
	y = 0.0

	
	

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

		s = KochCurve()
		t = KochCurve()
		u = KochCurve()
		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
		
		KochCurve().koch(n - 1, a, s)
		msg = "%.8f %.8f" % (s.x, s.y)
		print(msg)
		KochCurve().koch(n - 1, s, u)
		msg = "%.8f %.8f" % (u.x, u.y)
		print(msg)
		KochCurve().koch(n - 1, u, t)
		msg = "%.8f %.8f" % (t.x, t.y)
		print(msg)
		KochCurve().koch(n - 1, t, b)


if __name__ == '__main__':
	a = KochCurve()
	b = KochCurve()

	n = int(input())

	a.x = 0
	a.y = 0
	b.x = 100
	b.y = 0

	msg = "%.8f %.8f" % (a.x, a.y)
	print(msg)
	KochCurve().koch(n, a, b)
	msg = "%.8f %.8f" % (b.x, b.y)
	print(msg)

Solution (Python):
def koch(n, left, right):
	vector = [(right[0] - left[0])/3, (right[1] - left[1])/3]
	vector_up = [-vector[1]*(3**0.5)/2, vector[0]*(3**0.5)/2]
	point1 = [left[0] + vector[0], left[1] + vector[1]]
	point2 = [left[0] + 2*vector[0], left[1] + 2*vector[1]]
	middle = [left[0] + vector[0]*3/2 + vector_up[0],
			  left[1] + vector[1]*3/2 + vector_up[1]]

	if n == 0:
		return [right]

	if n == 1:
		return [point1, middle, point2, right]

	return koch(n-1, left, point1) + koch(n-1, point1, middle)\
			+ koch(n-1, middle, point2) + koch(n-1, point2, right)

if __name__ == "__main__":
	for point in [[0,0]] + koch(int(input()), [0,0], [100,0]):
		print(*point)

Solution (Python):
import math

pi=math.pi

def Koch(n,p0,p1):
        if n==0:
           return False

        s=[0 for i in range(2)]
        t=[0 for i in range(2)]
        u=[0 for i in range(2)]
        
        s =[(2*p0[0]+1*p1[0])/3.0,(2*p0[1]+1*p1[1])/3.0]
        t =[(1*p0[0]+2*p1[0])/3.0,(1*p0[1]+2*p1[1])/3.0]
        u =[(t[0]-s[0]) * math.cos (pi / 3) - (t[1]-s[1])* math.sin (pi / 3)+s[0],(t[0]-s[0]) * math.sin (pi / 3) + (t[1]-s[1])* math.cos (pi / 3)+s[1]]
        Koch(n-1,p0,s)
        print("{0:.8f} {1:.8f}".format(s[0],s[1]))
        Koch(n-1,s,u)
        print("{0:.8f} {1:.8f}".format(u[0],u[1]))
        Koch(n-1,u,t)
        print("{0:.8f} {1:.8f}".format(t[0],t[1]))
        Koch(n-1,t,p1)

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

print("{0:.8f} {1:.8f}".format(p0[0],p0[1]))
Koch(n,p0,p1)
print("{0:.8f} {1:.8f}".format(p1[0],p1[1]))

Solution (Python):
import math

def rot60(s,t):
    v=t-s
    a=1/2+complex(0,(math.sqrt(3)/2))
    return v*a+s

def puts(p):
    x=p.real
    y=p.imag
    print('{real} {imaginary}'.format(real=x,imaginary=y))

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

    s=(p2-p1)*(1/3)+p1
    t=(p2-p1)*(2/3)+p1
    u=rot60(s,t)
    koch(p1,s,n-1)
    puts(s)
    koch(s,u,n-1)
    puts(u)
    koch(u,t,n-1)
    puts(t)
    koch(t,p2,n-1)
    
n=int(input())
s=complex(0,0)
t=complex(100,0)
puts(s)
koch(s,t,n) 
puts(t)

Solution (Python):
import math

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

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

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

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

    k(n-1,p1,s)
    print(*s)
    k(n-1,s,u)
    print(*u)
    k(n-1,u,t)
    print(*t)
    k(n-1,t,p2)

n=int(input())
p1=(0,0)
p2=(100,0)

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

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("{:.8f} {:.8f}".format(sx, sy))
    koch(n-1, sx, sy, ux, uy)
    print("{:.8f} {:.8f}".format(ux, uy))
    koch(n-1, ux, uy, tx, ty)
    print("{:.8f} {:.8f}".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("{:.8f} {:.8f}".format(p1x, p1y))
koch(n, p1x, p1y, p2x, p2y)
print("{:.8f} {:.8f}".format(p2x, p2y))

Solution (Python):
from math import cos, sin, pi


def make_koch_curve(N, n1, n2):
    s = ((2 * n1[0] + n2[0]) / 3, (2 * n1[1] + n2[1]) / 3)
    t = ((n1[0] + 2 * n2[0]) / 3, (n1[1] + 2 * n2[1]) / 3)
    u = ((t[0] - s[0])*cos(pi/3) - (t[1] - s[1])*sin(pi/3) + s[0],
         (t[0] - s[0])*sin(pi/3) + (t[1] - s[1])*cos(pi/3) + s[1])
    if N == 0:
        return
    make_koch_curve(N-1, n1, s)
    print(*s)
    make_koch_curve(N-1, s, u)
    print(*u)
    make_koch_curve(N-1, u, t)
    print(*t)
    make_koch_curve(N-1, t, n2)


def main():
    N = int(input())
    n1 = (0., 0.)
    n2 = (100., 0.)
    print(*n1)
    make_koch_curve(N, n1, n2)
    print(*n2)


if __name__ == '__main__':
    main()

Solution (Python):
import math


def kock(n,p1,p2):
    
    
    if n==0:
        return
    s=[0,0]
    t=[0,0]
    u=[0,0]
    
    s[0]=(float((2*p1[0]+p2[0])/3))
    s[1]=(float((2*p1[1]+p2[1])/3))
    t[0]=(float((p1[0]+2*p2[0])/3))
    t[1]=(float((p1[1]+2*p2[1])/3))

    u[0]=((t[0]-s[0])*math.cos(math.radians(60))-(t[1]-s[1])*math.sin(math.radians(60))+s[0])
    u[1]=((t[0]-s[0])*math.sin(math.radians(60))+(t[1]-s[1])*math.cos(math.radians(60))+s[1])
    


    kock(n-1,p1,s)
    print(*s)
   
    
    kock(n-1,s,u)
    print(*u)
    
    
    kock(n-1,u,t)
    print(*t)
    
    
    kock(n-1,t,p2)



n=int(input())
p1=[0.0,0.0]
p2=[100.0,0.0]


print(*p1)


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

Solution (Python):
import math
class Point(object):
     def __init__(self, x,y):
          self.x = x*1.0
          self.y = y*1.0
def Koch(n,p1,p2):
    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((t.x-s.x)*math.cos(math.radians(60))-(t.y-s.y)*math.sin(math.radians(60))+s.x,(t.x-s.x)*math.sin(math.radians(60))+(t.y-s.y)*math.cos(math.radians(60))+s.y)
    if n==0:
        return
    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)
n=input()
p1=Point(0,0)
p2=Point(100,0)
print p1.x,p1.y
Koch(n,p1,p2)
print p2.x,p2.y

Solution (Python):
import math

n = int(input())

def koch(n,a,b): 
    if n==0:
        return
    th = math.pi*60/180
    s = ((2*a[0]+b[0])/3,(2*a[1]+b[1])/3)
    t = ((a[0]+2*b[0])/3,(a[1]+2*b[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])

    koch(n-1,a,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,b)

a = (0,0)
b = (100,0)
print(a[0],a[1])
koch(n,a,b)
print(b[0],b[1])

Solution (Python):
import math

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

    sx = (2*p1[0] + p2[0]) / 3
    sy = (2*p1[1] + p2[1]) / 3
    s = [sx,sy]
    tx = (p1[0] + 2*p2[0]) / 3
    ty = (p1[1] + 2*p2[1]) / 3
    t = [tx,ty]
    ux = (t[0] - s[0]) * 0.5 - (t[1] - s[1]) * math.sin(math.radians(60)) + s[0]
    uy = (t[0] - s[0]) * math.sin(math.radians(60)) + (t[1] - s[1]) * 0.5 + s[1]
    u = [ux,uy]
    
    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):
"""ICGNYN 17D8103016H 土屋　怜"""
import random
import math

def hura (a):
    ind=1
    for i in range(len(a)-1):
        c= [(a[i*4+1][0]-a[i*4][0])/3+a[i*4][0], (a[i*4+1][1]-a[i*4][1])/3+a[i*4][1]]
        d=[(a[i*4+1][0]-a[i*4][0])*2/3+a[i*4][0], (a[i*4+1][1]-a[i*4][1])*2/3+a[i*4][1]]
        cos=math.cos(math.pi/3)
        sin=math.sin(math.pi/3)
        f = [d[0]-c[0],d[1]-c[1]]
        b= [f[0]*cos-sin*f[1]+c[0],sin*f[0]+cos*f[1]+c[1]]
        a.insert(ind,c)
        a.insert(ind+1,b)
        a.insert(ind+2,d)
        ind=ind+4
        

N = int(input())
x=[[0,0],[100,0]]
for i in range(N):
    hura(x)
for i in range(len(x)):
  print(x[i][0],end="")
  print(" ",end="")
  print(x[i][1])

Solution (Python):
import math

def CalcCoordinate(xa, ya, xb, yb):
    arg = math.atan2(yb-ya, xb-xa) 
    d = math.sqrt((xb-xa)*(xb-xa) + (yb-ya)*(yb-ya)) 
    xc = d/2 
    
    
    yc = math.sqrt(3)*d/2

    
    ux = xc*math.cos(arg) - yc*math.sin(arg) + xa
    uy = xc*math.sin(arg) + yc*math.cos(arg) + ya
    return ux, uy
    """
    else:
        ux = xc*math.cos(arg) + yc*math.sin(arg) + xa
        uy = xc*math.sin(arg) - yc*math.cos(arg) + ya
        return ux, uy
"""

def MakeTriangle(x1, y1, x2, y2, n):
    if(n != 0):
        sx = (2*x1 + x2) / 3
        sy = (2*y1 + y2) / 3
        tx = (x1 + 2*x2) / 3
        ty = (y1 + 2*y2) / 3
        ux, uy = CalcCoordinate(sx, sy, tx, ty)
        MakeTriangle(x1, y1, sx, sy, n-1)
        print(sx, sy)
        MakeTriangle(sx, sy, ux, uy, n-1)
        print(ux, uy)
        MakeTriangle(ux, uy, tx, ty, n-1)
        print(tx, ty)
        MakeTriangle(tx, ty, x2, y2, n-1)
    else:
        return
        
  

x1 = 0
y1 = 0
x2 = 100
y2 = 0

n = int(input())

print(x1, y1)
MakeTriangle(x1, y1, x2, y2, n)
print(x2, y2)

Solution (Python):
import math


def koch(count, p1, p2):
    if count == 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]) * 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(count - 1, p1, s)
    print(s[0], s[1])

    koch(count - 1, s, u)
    print(u[0], u[1])

    koch(count - 1, u, t)
    print(t[0], t[1])

    koch(count - 1, t, p2)


if __name__ == '__main__':
    n = int(input())
    p1 = [0.0, 0.0]
    p2 = [100.0, 0.0]
    print(p1[0], p1[1])
    koch(n, p1, p2)
    print(p2[0], p2[1])

Solution (Python):
import math
def koch(left, right, n):
    if n < 1: print(" ".join(map(str, left))); return
    
    s, t, u = [0] * 2, [0] * 2, [0] * 2
    s[0] = (2 * left[0] + right[0]) / 3.0
    s[1] = (2 * left[1] + right[1]) / 3.0
    
    t[0] = (left[0] + 2 * right[0]) / 3.0
    t[1] = (left[1] + 2 * right[1]) / 3.0
    
    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(left, s, n-1)
    koch(s, u, n-1)
    koch(u, t, n-1)
    koch(t, right, n-1)
    
n = int(input())
left = [0, 0]
right = [100, 0]

koch(left, right, n)
print(right[0], right[1])