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

def KC(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.pi/3)-(t[1]-s[1])*math.sin(math.pi/3)+s[0],(t[0]-s[0])*math.sin(math.pi/3)+(t[1]-s[1])*math.cos(math.pi/3)+s[1]]
    KC(n-1,p1,s)
    print(str(format(s[0],'.8f'))+" "+str(format(s[1],'.8f')))
    KC(n-1,s,u)
    print(str(format(u[0],'.8f'))+" "+str(format(u[1],'.8f')))
    KC(n-1,u,t)
    print(str(format(t[0],'.8f'))+" "+str(format(t[1],'.8f')))
    KC(n-1,t,p2)
    
  
if __name__=="__main__":
    n=int(input())
    p1=[0,0]
    p2=[100,0]
    print(str(format(p1[0],'.8f'))+" "+str(format(p1[1],'.8f')))
    KC(n,p1,p2)
    print(str(format(p2[0],'.8f'))+" "+str(format(p2[1],'.8f')))

Solution (Python):

import math
def get_koch_curve_points(curve_num, points):

    result = points
    for i in range(curve_num):
        next_list = []
        for j in range(len(result) - 1):
            next_list.append(result[j])
            next_list.extend(koch_curve(result[j], result[j + 1]))
        next_list.append(result[-1])
        result = next_list
        
    return result

def koch_curve(left_points, right_points):
    len_x = right_points[0] - left_points[0]
    len_y = right_points[1] - left_points[1]

    distance_x = len_x / 3
    distance_y = len_y / 3

    s = (left_points[0] + distance_x, left_points[1] + distance_y)
    t = (right_points[0] - distance_x, right_points[1] - distance_y)

    cos60 = math.cos(math.radians(60))
    sin60 = math.sin(math.radians(60))
    u = (s[0] + distance_x * cos60 - distance_y * sin60, s[1] + distance_x * sin60 + distance_y * cos60)
    return [s, u, t]

if __name__ == "__main__":

    curve_num = int(input())
    res = get_koch_curve_points(curve_num, [(0,0),(100,0)])
    print("\n".join([str(x) + " " + str(y) for x,y in res]))

Solution (Python):



import sys
import math

_input = sys.stdin.readlines()
depth = int(_input[0])
sin_60, cos_60 = math.sin(math.pi / 3), math.cos(math.pi / 3)


class Point(object):
    def __init__(self, x=0.0, y=0.0):
        """
        initialize the point
        """
        self.x = float(x)
        self.y = float(y)


def koch_curve(d, p1, p2):
    if not d:
        
        return None

    
    s = Point()
    t = Point()
    u = Point()
    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_60 - (t.y - s.y) * sin_60 + s.x
    u.y = (t.x - s.x) * sin_60 + (t.y - s.y) * cos_60 + s.y

    koch_curve(d - 1, p1, s)
    print(format(s.x, '.8f'), format(s.y, '.8f'))

    koch_curve(d - 1, s, u)
    print(format(u.x, '.8f'), format(u.y, '.8f'))

    koch_curve(d - 1, u, t)
    print(format(t.x, '.8f'), format(t.y, '.8f'))

    koch_curve(d - 1, t, p2)


if __name__ == '__main__':
    p1_start, p2_end = Point(x=0, y=0), Point(x=100, y=0)
    print(format(p1_start.x, '.8f'), '', format(p1_start.y, '.8f'))
    koch_curve(depth, p1_start, p2_end)
    print(format(p2_end.x, '.8f'), '', format(p2_end.y, '.8f'))

Solution (Python):



import sys
import math

_input = sys.stdin.readlines()
depth = int(_input[0])
sin_60, cos_60 = math.sin(math.pi / 3), math.cos(math.pi / 3)


class Point(object):
    def __init__(self, x=0.0, y=0.0):
        """
        initialize the point
        """
        self.x = float(x)
        self.y = float(y)


def koch_curve(d, p1, p2):
    if not d:
        
        return None

    
    
    s, t, u = (Point() for _ in range(3))

    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_60 - (t.y - s.y) * sin_60 + s.x
    u.y = (t.x - s.x) * sin_60 + (t.y - s.y) * cos_60 + s.y

    koch_curve(d - 1, p1, s)
    print(format(s.x, '.8f'), format(s.y, '.8f'))

    koch_curve(d - 1, s, u)
    print(format(u.x, '.8f'), format(u.y, '.8f'))

    koch_curve(d - 1, u, t)
    print(format(t.x, '.8f'), format(t.y, '.8f'))

    koch_curve(d - 1, t, p2)


if __name__ == '__main__':
    p1_start, p2_end = Point(x=0, y=0), Point(x=100, y=0)
    print(format(p1_start.x, '.8f'), '', format(p1_start.y, '.8f'))
    koch_curve(depth, p1_start, p2_end)
    print(format(p2_end.x, '.8f'), '', format(p2_end.y, '.8f'))

Solution (Python):



import sys
import math

_input = sys.stdin.readlines()
depth = int(_input[0])
sin_60, cos_60 = math.sin(math.pi / 3), math.cos(math.pi / 3)


class Point(object):
    def __init__(self, x=0.0, y=0.0):
        """
        initialize the point
        """
        self.x = float(x)
        self.y = float(y)


def koch_curve(d, p1, p2):
    if not d:
        
        return None

    
    
    s, t, u = (Point() for _ in range(3))

    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_60 - (t.y - s.y) * sin_60 + s.x
    u.y = (t.x - s.x) * sin_60 + (t.y - s.y) * cos_60 + s.y

    koch_curve(d - 1, p1, s)
    print(format(s.x, '.8f'), format(s.y, '.8f'))

    koch_curve(d - 1, s, u)
    print(format(u.x, '.8f'), format(u.y, '.8f'))

    koch_curve(d - 1, u, t)
    print(format(t.x, '.8f'), format(t.y, '.8f'))

    koch_curve(d - 1, t, p2)


if __name__ == '__main__':
    p1_start, p2_end = Point(x=0, y=0), Point(x=100, y=0)
    print(format(p1_start.x, '.8f'), format(p1_start.y, '.8f'))
    koch_curve(depth, p1_start, p2_end)
    print(format(p2_end.x, '.8f'), format(p2_end.y, '.8f'))

Solution (Python):
from math import sin, cos, pi
def koch(px, py, qx, qy, n):
    if n == 0:
        return True
    sx = (2 * px + qx) / 3
    sy = (2 * py + qy) / 3
    tx = (px + 2 * qx) / 3
    ty = (py + 2 * qy) / 3
    ux = sx + (tx - sx) * cos(pi / 3) - (ty - sy) * sin(pi / 3)
    uy = sy + (tx - sx) * sin(pi / 3) + (ty - sy) * cos(pi / 3)
    koch(px, py, sx, sy, n - 1)
    print("{:.8f} {:.8f}".format(sx, sy))
    koch(sx, sy, ux, uy, n - 1)
    print("{:.8f} {:.8f}".format(ux, uy))
    koch(ux, uy, tx, ty, n - 1)
    print("{:.8f} {:.8f}".format(tx, ty))
    koch(tx, ty, qx, qy, n - 1)
n = int(input())
print("{:.8f} {:.8f}".format(0, 0))
koch(0, 0, 100, 0, n)
print("{:.8f} {:.8f}".format(100, 0))

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


def calc_koch(depth, p1, p2):
    if depth == 0:
        return

    s = ((2 * p1[0] + 1 * p2[0]) / 3.0, (2 * p1[1] + 1 * p2[1]) / 3.0)
    t = ((1 * p1[0] + 2 * p2[0]) / 3.0, (1 * p1[1] + 2 * p2[1]) / 3.0)
    ux = (t[0] - s[0]) * cos(radians(60)) - (t[1] - s[1]) * sin(radians(60)) + s[0]
    uy = (t[0] - s[0]) * sin(radians(60)) + (t[1] - s[1]) * cos(radians(60)) + s[1]
    u = (ux, uy)

    calc_koch(depth-1, p1, s)
    print('{0:.8f} {1:.8f}'.format(*s))
    calc_koch(depth-1, s, u)
    print('{0:.8f} {1:.8f}'.format(*u))
    calc_koch(depth-1, u, t)
    print('{0:.8f} {1:.8f}'.format(*t))
    calc_koch(depth-1, t, p2)


if __name__ == '__main__':
    
    depth = int(input())

    
    p1 = (0.0, 0.0)
    p2 = (100.0, 0.0)

    print('{0:.8f} {1:.8f}'.format(*p1))
    result = calc_koch(depth, p1, p2)
    print('{0:.8f} {1:.8f}'.format(*p2))

Solution (Python):



"""
input:
2

output:
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
"""

import sys
import math


class Point(object):
    __slots__ = ('x', 'y')

    def __init__(self, x=-1, y=-1):
        """
        Initialize the point
        """
        self.x = float(x)
        self.y = float(y)


def koch_curve(d, p1, p2):
    if not d:
        return None

    s, t, u = (Point() for _ in range(3))

    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_60 - (t.y - s.y) * SIN_60 + s.x
    u.y = (t.x - s.x) * SIN_60 + (t.y - s.y) * COS_60 + s.y

    koch_curve(d - 1, p1, s)
    print('{0:.8f} {1:.8f}'.format(s.x, s.y))

    koch_curve(d - 1, s, u)
    print('{0:.8f} {1:.8f}'.format(u.x, u.y))

    koch_curve(d - 1, u, t)
    print('{0:.8f} {1:.8f}'.format(t.x, t.y))

    koch_curve(d - 1, t, p2)


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    depth = int(_input[0])
    SIN_60, COS_60 = math.sin(math.pi / 3), math.cos(math.pi / 3)

    p1_start, p2_end = Point(x=0, y=0), Point(x=100, y=0)
    print(format(p1_start.x, '.8f'), format(p1_start.y, '.8f'))
    koch_curve(depth, p1_start, p2_end)
    print(format(p2_end.x, '.8f'), format(p2_end.y, '.8f'))

Solution (Python):
import math

cos60 = math.cos(math.radians(60))
sin60 = math.sin(math.radians(60))

def kock(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)
    u = ((t[0] - s[0]) * cos60 - (t[1] - s[1]) * sin60 + s[0],
         (t[0] - s[0]) * sin60 + (t[1] - s[1]) * cos60 + 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:
    def __init__(self, a, b):
        self.x = a
        self.y = b

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

    s = Point(0, 0)
    t = Point(0, 0)
    u = Point(0, 0)
    th = math.pi * 60.0 / 180

    s.x = (2.0 * a.x + b.x) / 3
    s.y = (2.0 * a.y + b.y) / 3
    t.x = (a.x + 2.0 * b.x) / 3
    t.y = (a.y + 2.0 * 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("{:.6f} {:.6f}".format(s.x, s.y))
    koch(n - 1, s, u)
    print("{:.6f} {:.6f}".format(u.x, u.y))
    koch(n - 1, u, t)
    print("{:.6f} {:.6f}".format(t.x, t.y))
    koch(n - 1, t, b)

    return 0

a = Point(0.0, 0.0)
b = Point(100.0, 0.0)

n = int(input())

print("{:.6f} {:.6f}".format(a.x, a.y))
koch(n, a, b)
print("{:.6f} {:.6f}".format(b.x, b.y))

Solution (Python):
import math

def koch(d,x1,y1,x2,y2):
    if d == 0:
        return

    xs = (2*x1+x2)/3
    ys = (2*y1+y2)/3
    xt = (x1+2*x2)/3
    yt = (y1+2*y2)/3

    xu = (xt-xs)*math.cos(math.pi/3) - (yt-ys)*math.sin(math.pi/3) + xs
    yu = (xt-xs)*math.sin(math.pi/3) + (yt-ys)*math.cos(math.pi/3) + ys


    koch(d-1,x1,y1,xs,ys)
    print(xs,ys)
    koch(d-1,xs,ys,xu,yu)
    print(xu,yu)
    koch(d-1,xu,yu,xt,yt)
    print(xt,yt)
    koch(d-1,xt,yt,x2,y2)
    
n = int(input())
print(0.00000000,0.00000000)
koch(n,0.0,0.0,100.0,0.0)
print(100.00000000,0.00000000)

Solution (Python):
import math

def rotate(x, y):
  r3 = math.sqrt(3)
  x2 = x / 2 - y * r3 / 2
  y2 = x * r3 / 2 + y / 2
  return x2, y2

def divide(a, b):
  m1 = [(b[0] - a[0]) / 3 + a[0], (b[1] - a[1]) / 3 + a[1]]
  m2 = [(b[0] - a[0]) * 2 / 3 + a[0], (b[1] - a[1]) * 2 / 3 + a[1]]
  r = rotate(m2[0] - m1[0], m2[1] - m1[1])
  m = [m1[0] + r[0], m1[1] + r[1]]
  return (a, m1, m, m2, b)

n = int(input())
arr = ([0, 0], [100, 0])

for i in range(n):
  tmp = []
  for j in range(len(arr) - 1):
    r = divide(arr[j], arr[j + 1])
    if j > 0: r = r[1:]
    tmp += r
  arr = tmp

for a in arr:
  print(" ".join(map(lambda x: str(round(x, 8)), a)))

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 sys
import math

theta = math.pi / 3
cos_theta = math.cos(theta)
sin_theta = math.sin(theta)

def calc_pdr(pd):
	pdr_x = cos_theta * pd[0] - sin_theta * pd[1]
	pdr_y = sin_theta * pd[0] + cos_theta * pd[1]

	return [pdr_x, pdr_y]

def make_coch(l):
	num_points = len(l) 
	if num_points == 2:
		p0 = l[0]
		p1 = l[1]

		pd = [(x1 - x0) / 3 for (x1, x0) in zip(p1, p0)]
		pa = [x0 + xd for (x0, xd) in zip(p0, pd)]
		pc = [x0 + 2 * xd for (x0, xd) in zip(p0, pd)]
		pdr = calc_pdr(pd)

		pb = [xa + xdr for (xa, xdr) in zip(pa, pdr)]

		return [p0, pa, pb, pc, p1]
	else:
		l_ret = []
		
		l_ret += make_coch([l[0], l[1]])
		for i in range(1, num_points - 1):
			nl = make_coch([l[i], l[i + 1]])
			l_ret += nl[1:]
		
		return l_ret
	

fin = sys.stdin

n = int(fin.readline())

p0 = [0.0, 0.0]
p1 = [100.0, 0.0]	
l = [p0, p1]

for i in range(n):
	l = make_coch(l)

for p in l:
	print("{0:.5f} {1:.5f}".format(p[0], p[1]))

Solution (Python):
import math

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

p1 = [0, 0]
p2 = [100,0]
n = int(input())
print("0.00000 0.00000")
koch(n, p1, p2)
print("100.0000 0.0000")

Solution (Python):


import sys
import os
import math


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

def koch(depth, p0, p1):
    if depth == 0:
        return

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

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

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

    
    koch(depth - 1, p0, s)
    print(*s)
    koch(depth - 1, s, u)
    print(*u)
    koch(depth - 1, u, t)
    print(*t)
    koch(depth - 1, t, p1)

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

Solution (Python):

import math


def divide_segment(left, right):
    """divide segment

    Args:
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        divided points
    """
    delta_x_quarter = (right[0] - left[0]) / 3.0
    delta_y_quarter = (right[1] - left[1]) / 3.0

    delta_x_quarter_rotated = (delta_x_quarter - math.sqrt(3) * delta_y_quarter) / 2.0
    delta_y_quarter_rotated = (math.sqrt(3) * delta_x_quarter + delta_y_quarter) / 2.0

    s = [left[0] + delta_x_quarter, left[1] + delta_y_quarter]
    t = [left[0] + delta_x_quarter * 2.0, left[1] + delta_y_quarter * 2.0]
    u = [s[0] + delta_x_quarter_rotated, s[1] + delta_y_quarter_rotated]

    return s, t, u


def make_coch_curve(depth, left, right):
    """make coch curve with depth iteration_number between point left, right
    each vertices positions are printed

    Args:
        depth: depth
        left: left edge point
        right: right edge point

    Returns:
        None
    """
    print("{0:2.8f} {1:2.8f}".format(left[0], left[1]))
    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth-1, left, right)


def make_coch_curve_recursively(depth, left, right):
    """recursion part of coch curve

    Args:
        depth: depth
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        None
    """
    
    s, t, u = divide_segment(left, right)

    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(s[0], s[1]))
        print("{0:2.8f} {1:2.8f}".format(u[0], u[1]))
        print("{0:2.8f} {1:2.8f}".format(t[0], t[1]))
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth - 1, left, s)
        make_coch_curve_recursively(depth - 1, s, u)
        make_coch_curve_recursively(depth - 1, u, t)
        make_coch_curve_recursively(depth - 1, t, right)


def main():
    depth = int(input().strip())
    make_coch_curve(depth, left=[0.0, 0.0], right=[100.0, 0.0])


if __name__ == '__main__':
    main()

Solution (Python):

import math


def divide_segment(left, right):
    """divide segment

    Args:
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        divided points
    """
    delta_x_quarter = (right[0] - left[0]) / 3.0
    delta_y_quarter = (right[1] - left[1]) / 3.0

    delta_x_quarter_rotated = (delta_x_quarter - math.sqrt(3) * delta_y_quarter) / 2.0
    delta_y_quarter_rotated = (math.sqrt(3) * delta_x_quarter + delta_y_quarter) / 2.0

    s = [left[0] + delta_x_quarter, left[1] + delta_y_quarter]
    t = [left[0] + delta_x_quarter * 2.0, left[1] + delta_y_quarter * 2.0]
    u = [s[0] + delta_x_quarter_rotated, s[1] + delta_y_quarter_rotated]

    return s, t, u


def make_coch_curve(depth, left, right):
    """make coch curve with depth iteration_number between point left, right
    each vertices positions are printed

    Args:
        depth: depth
        left: left edge point
        right: right edge point

    Returns:
        None
    """
    print("{0:2.8f} {1:2.8f}".format(left[0], left[1]))
    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth-1, left, right)


def make_coch_curve_recursively(depth, left, right):
    """recursion part of coch curve

    Args:
        depth: depth
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        None
    """
    
    s, t, u = divide_segment(left, right)

    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(s[0], s[1]))
        print("{0:2.8f} {1:2.8f}".format(u[0], u[1]))
        print("{0:2.8f} {1:2.8f}".format(t[0], t[1]))
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth - 1, left, s)
        make_coch_curve_recursively(depth - 1, s, u)
        make_coch_curve_recursively(depth - 1, u, t)
        make_coch_curve_recursively(depth - 1, t, right)


def main():
    depth = int(input().strip())
    make_coch_curve(depth, left=[0.0, 0.0], right=[100.0, 0.0])


if __name__ == '__main__':
    main()

Solution (Python):

import math


def divide_segment(left, right):
    """divide segment

    Args:
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        divided points
    """
    delta_x_quarter = (right[0] - left[0]) / 3.0
    delta_y_quarter = (right[1] - left[1]) / 3.0

    delta_x_quarter_rotated = (delta_x_quarter - math.sqrt(3) * delta_y_quarter) / 2.0
    delta_y_quarter_rotated = (math.sqrt(3) * delta_x_quarter + delta_y_quarter) / 2.0

    s = [left[0] + delta_x_quarter, left[1] + delta_y_quarter]
    t = [left[0] + delta_x_quarter * 2.0, left[1] + delta_y_quarter * 2.0]
    u = [s[0] + delta_x_quarter_rotated, s[1] + delta_y_quarter_rotated]

    return s, t, u


def make_coch_curve(depth, left, right):
    """make coch curve with depth iteration_number between point left, right
    each vertices positions are printed

    Args:
        depth: depth
        left: left edge point
        right: right edge point

    Returns:
        None
    """
    print("{0:2.8f} {1:2.8f}".format(left[0], left[1]))
    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth-1, left, right)


def make_coch_curve_recursively(depth, left, right):
    """recursion part of coch curve

    Args:
        depth: depth
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        None
    """
    
    s, t, u = divide_segment(left, right)

    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(s[0], s[1]))
        print("{0:2.8f} {1:2.8f}".format(u[0], u[1]))
        print("{0:2.8f} {1:2.8f}".format(t[0], t[1]))
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth - 1, left, s)
        make_coch_curve_recursively(depth - 1, s, u)
        make_coch_curve_recursively(depth - 1, u, t)
        make_coch_curve_recursively(depth - 1, t, right)


def main():
    depth = int(input().strip())
    make_coch_curve(depth, left=[0.0, 0.0], right=[100.0, 0.0])


if __name__ == '__main__':
    main()

Solution (Python):

import math

root_three = math.sqrt(3)


def divide_segment(left, right):
    """divide segment

    Args:
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        divided points
    """
    delta_x_quarter = (right[0] - left[0]) / 3.0
    delta_y_quarter = (right[1] - left[1]) / 3.0

    delta_x_quarter_rotated = (delta_x_quarter - root_three * delta_y_quarter) / 2.0
    delta_y_quarter_rotated = (root_three * delta_x_quarter + delta_y_quarter) / 2.0

    s = [left[0] + delta_x_quarter, left[1] + delta_y_quarter]
    t = [left[0] + delta_x_quarter * 2.0, left[1] + delta_y_quarter * 2.0]
    u = [s[0] + delta_x_quarter_rotated, s[1] + delta_y_quarter_rotated]

    return s, t, u


def make_coch_curve(depth, left, right):
    """make coch curve with depth iteration_number between point left, right
    each vertices positions are printed

    Args:
        depth: depth
        left: left edge point
        right: right edge point

    Returns:
        None
    """
    print("{0:2.8f} {1:2.8f}".format(left[0], left[1]))
    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth - 1, left, right)


def make_coch_curve_recursively(depth, left, right):
    """recursion part of coch curve

    Args:
        depth: depth
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        None
    """
    
    s, t, u = divide_segment(left, right)

    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(s[0], s[1]))
        print("{0:2.8f} {1:2.8f}".format(u[0], u[1]))
        print("{0:2.8f} {1:2.8f}".format(t[0], t[1]))
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth - 1, left, s)
        make_coch_curve_recursively(depth - 1, s, u)
        make_coch_curve_recursively(depth - 1, u, t)
        make_coch_curve_recursively(depth - 1, t, right)


def main():
    depth = int(input().strip())
    make_coch_curve(depth, left=[0.0, 0.0], right=[100.0, 0.0])


if __name__ == '__main__':
    main()

Solution (Python):

import math

root_three = math.sqrt(3)


def divide_segment(left, right):
    """divide segment

    Args:
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        divided points
    """
    delta_x_quarter = (right[0] - left[0]) / 3.0
    delta_y_quarter = (right[1] - left[1]) / 3.0

    delta_x_quarter_rotated = (delta_x_quarter - root_three * delta_y_quarter) / 2.0
    delta_y_quarter_rotated = (root_three * delta_x_quarter + delta_y_quarter) / 2.0

    s = [left[0] + delta_x_quarter, left[1] + delta_y_quarter]
    t = [left[0] + delta_x_quarter * 2.0, left[1] + delta_y_quarter * 2.0]
    u = [s[0] + delta_x_quarter_rotated, s[1] + delta_y_quarter_rotated]

    return s, t, u


def make_coch_curve(depth, left, right):
    """make coch curve with depth iteration_number between point left, right
    each vertices positions are printed

    Args:
        depth: depth
        left: left edge point
        right: right edge point

    Returns:
        None
    """
    print("{0:2.8f} {1:2.8f}".format(left[0], left[1]))
    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth - 1, left, right)


def make_coch_curve_recursively(depth, left, right):
    """recursion part of coch curve

    Args:
        depth: depth
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        None
    """
    
    s, t, u = divide_segment(left, right)

    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(s[0], s[1]))
        print("{0:2.8f} {1:2.8f}".format(u[0], u[1]))
        print("{0:2.8f} {1:2.8f}".format(t[0], t[1]))
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth - 1, left, s)
        make_coch_curve_recursively(depth - 1, s, u)
        make_coch_curve_recursively(depth - 1, u, t)
        make_coch_curve_recursively(depth - 1, t, right)


def main():
    depth = int(input().strip())
    make_coch_curve(depth, left=[0.0, 0.0], right=[100.0, 0.0])


if __name__ == '__main__':
    main()

Solution (Python):

import math

root_three = math.sqrt(3)


def divide_segment(left, right):
    """divide segment

    Args:
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        divided points
    """
    delta_x_quarter = (right[0] - left[0]) / 3.0
    delta_y_quarter = (right[1] - left[1]) / 3.0

    delta_x_quarter_rotated = (delta_x_quarter - root_three * delta_y_quarter) / 2.0
    delta_y_quarter_rotated = (root_three * delta_x_quarter + delta_y_quarter) / 2.0

    s = [left[0] + delta_x_quarter, left[1] + delta_y_quarter]
    t = [left[0] + delta_x_quarter * 2.0, left[1] + delta_y_quarter * 2.0]
    u = [s[0] + delta_x_quarter_rotated, s[1] + delta_y_quarter_rotated]

    return s, t, u


def make_coch_curve(depth, left, right):
    """make coch curve with depth iteration_number between point left, right
    each vertices positions are printed

    Args:
        depth: depth
        left: left edge point
        right: right edge point

    Returns:
        None
    """
    print("{0:2.8f} {1:2.8f}".format(left[0], left[1]))
    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth - 1, left, right)


def make_coch_curve_recursively(depth, left, right):
    """recursion part of coch curve

    Args:
        depth: depth
        left: left edge coordinate
        right: right edge coordinate

    Returns:
        None
    """
    
    s, t, u = divide_segment(left, right)

    if depth == 0:
        print("{0:2.8f} {1:2.8f}".format(s[0], s[1]))
        print("{0:2.8f} {1:2.8f}".format(u[0], u[1]))
        print("{0:2.8f} {1:2.8f}".format(t[0], t[1]))
        print("{0:2.8f} {1:2.8f}".format(right[0], right[1]))
    else:
        make_coch_curve_recursively(depth - 1, left, s)
        make_coch_curve_recursively(depth - 1, s, u)
        make_coch_curve_recursively(depth - 1, u, t)
        make_coch_curve_recursively(depth - 1, t, right)


def main():
    depth = int(input().strip())
    make_coch_curve(depth, left=[0.0, 0.0], right=[100.0, 0.0])


if __name__ == '__main__':
    main()

Solution (Python):
def s(p,q):
    a=p[0]
    b=p[1]
    c=q[0]
    d=q[1]
    return [(2*a+c)/3,(2*b+d)/3]
def t(p,q):
    a=p[0]
    b=p[1]
    c=q[0]
    d=q[1]    
    return [(a+2*c)/3,(b+2*d)/3]
def u(p,q):
    a=p[0]
    b=p[1]
    c=q[0]
    d=q[1]
    return [(a+c)/2+(3**(1/2))*(b-d)/6,(b+d)/2+(3**(1/2))*(c-a)/6]

def koch(p1,p2,n):
    if n==0:
        return [p1]
    else:
        return koch(p1,s(p1,p2),n-1)+koch(s(p1,p2),u(p1,p2),n-1)+koch(u(p1,p2),t(p1,p2),n-1)+koch(t(p1,p2),p2,n-1)

n=int(input())
a=[0,0]
b=[100,0]
for i in koch(a,b,n):
    print(str(i[0])+" "+str(i[1]))

print(str(b[0])+" "+str(b[1]))

Solution (Python):
import math

def kochCurve(p1, p2, n):
  if n == 0:
    print(p2[0], p2[1])
    return

  v = [(p2[0]-p1[0])/3, (p2[1]-p1[1])/3]
  s = [p1[0]+v[0], p1[1]+v[1]]
  t = [s[0]+v[0], s[1]+v[1]]

  theta = math.atan2(v[1], v[0]) + math.pi/2

  a = math.sqrt((t[0]-s[0])**2+(t[1]-s[1])**2)
  h = 3**(1/2)*a/2
  u = [s[0]+(t[0]-s[0])/2+h*math.cos(theta), s[1]+(t[1]-s[1])/2+h*math.sin(theta)]

  n -= 1
  kochCurve(p1, s, n)
  kochCurve(s, u, n)
  kochCurve(u, t, n)
  kochCurve(t, p2, n)

if __name__ == "__main__":
  n = int(input())
  print(0, 0)
  kochCurve([0, 0], [100, 0], n)

Solution (Python):
import math
def kock(n, p1, p2):
 if n == 0:
  return

 
 sx = (2 * p1[0] + 1 * p2[0]) / 3
 sy = (2 * p1[1] + 1 * p2[1]) / 3
 s = [sx, sy]
 tx = (1 * p1[0] + 2 * p2[0]) / 3
 ty = (1 * p1[1] + 2 * p2[1]) / 3
 t = [tx, ty]
 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
 u = [ux, uy]

 kock(n-1,p1,s)
 print(" ".join(['{:.5f}'.format(i) for i in s]))
 kock(n-1,s,u)
 print(" ".join(['{:.5f}'.format(i) for i in u]))
 kock(n-1,u,t)
 print(" ".join(['{:.5f}'.format(i) for i in t]))
 kock(n-1,t,p2)

n = float(input())
print("0.00000 0.00000")
kock(n, [0,0], [100,0])
print("100.00000 0.00000")

Solution (Python):

import math

def koch(p1, p2, n):
    if n == 0:
        return None
    p1_x = p1[0]
    p1_y = p1[1]
    p2_x = p2[0]
    p2_y = p2[1]

    s_x = (1/3) * (2*p1_x + p2_x)
    s_y = (1/3) * (2*p1_y + p2_y)
    t_x = (1/3) * (p1_x + 2*p2_x)
    t_y = (1/3) * (p1_y + 2*p2_y)

    u_x = s_x + (1/2)*(t_x-s_x) + (-math.sqrt(3)/2)*(t_y-s_y)
    u_y = s_y + (math.sqrt(3)/2)*(t_x-s_x) + (1/2)*(t_y-s_y)

    koch([p1_x, p1_y], [s_x, s_y], n-1)
    print("%.8f %.8f" %(s_x, s_y))
    koch([s_x, s_y], [u_x, u_y], n-1)
    print("%.8f %.8f" %(u_x, u_y))
    koch([u_x, u_y], [t_x, t_y], n-1)
    print("%.8f %.8f" %(t_x, t_y))
    koch([t_x, t_y], [p2_x, p2_y], n-1)


n = int(input())
p1 = [0, 0]
p2 = [100, 0]
print("%.8f %.8f" %(p1[0], p1[1]))
koch(p1, p2, n)
print("%.8f %.8f" %(p2[0], p2[1]))