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



Sample input:
1


Sample output:
0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000

Submission status: Accepted

Solution code:
#coding:utf-8
#1_5_C
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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]

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
# coding: utf-8
import math as m
N = int(input())

# フラクタルのステップをnとする
# 端点p1, p2を与えてs, t, uを計算
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
    # p1, p2からs, t, uを求める
    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))
    
    # 各辺に対しても同様にs, t, uを求める
    # printの順番に注意
    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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

Solution code:
from math import sin
from math import cos
from math import pi

class point():
   def __init__(self,_x,_y):
       self.x = _x
       self.y = _y

def kock (n, p1, p2):
    if n == 0:
        return
    s = point((2. * p1.x + 1. * p2.x) / 3., (2. * p1.y + 1. *p2.y) / 3.)
    t = point((1. * p1.x + 2. * p2.x) / 3., (1. * p1.y + 2. *p2.y) / 3.)
    u = point((t.x - s.x) * cos(pi / 3.) - (t.y - s.y) * sin(pi / 3.) + s.x, (t.x - s.x) * sin(pi / 3.) + (t.y - s.y) * cos(pi / 3) + s.y)

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

n = input();

p1 = point(0., 0.)
p2 = point(100., 0.)

print '%s %s' % (str(p1.x), str(p1.y))
kock(n, p1, p2)
print '%s %s' % (str(p2.x), str(p2.y))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import math
           
def koch(n, x1, y1, x2, y2):
  if n == 0:
    return
  
  theta60 = math.radians(60)
          
  sx = ( 2 * x1 + x2 ) /3
  sy = ( 2 * y1 + y2 ) /3
  tx = ( 2 * x2 + x1 ) /3
  ty = ( 2 * y2 + y1 ) /3
  ux = ( tx - sx ) * math.cos( theta60 ) - ( ty - sy ) * math.sin( theta60 ) + sx
  uy = ( tx - sx ) * math.sin( theta60 ) + ( ty - sy ) * math.cos( theta60 ) + sy
           
  koch(n-1, x1, y1, 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, x2, y2)
    
n = input()
           
p1x = float(0.)
p1y = float(0.)
p2x = float(100.)
p2y = float(0.)
           
print p1x, p1y
koch(n, p1x, p1y, p2x, p2y)
print p2x, p2y

Submission status: Accepted

Solution code:
import math
n = input()

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 float(0), float(0)
kock(0, n, float(0), float(0), float(100), float(0))
print float(100), float (0)

Submission status: Accepted

Solution code:
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]

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
import math
            
def koch(n, x1, y1, x2, y2):
  if n == 0:
    return
   
  theta60 = math.radians(60)
           
  sx = ( 2 * x1 + x2 ) /3
  sy = ( 2 * y1 + y2 ) /3
  tx = ( 2 * x2 + x1 ) /3
  ty = ( 2 * y2 + y1 ) /3
  ux = ( tx - sx ) * math.cos( theta60 ) - ( ty - sy ) * math.sin( theta60 ) + sx
  uy = ( tx - sx ) * math.sin( theta60 ) + ( ty - sy ) * math.cos( theta60 ) + sy
            
  koch(n-1, x1, y1, 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, x2, y2)
     
n = input()
            
p1x = float(0.)
p1y = float(0.)
p2x = float(100.)
p2y = float(0.)
            
print p1x, p1y
koch(n, p1x, p1y, p2x, p2y)
print p2x, p2y

Submission status: Accepted

Solution code:
import math

def kock(n, x1, y1, x2, y2):
    if n == 0:
        return

    angle = math.radians(60)

    sx = (2*x1 + x2)/3
    sy = (2*y1 + y2)/3
    tx = (2*x2 + x1)/3
    ty = (2*y2 + y1)/3
    ux = (tx - sx)*math.cos(angle) - (ty - sy)*math.sin(angle) + sx
    uy = (tx - sx)*math.sin(angle) + (ty - sy)*math.cos(angle) + sy

    kock(n-1, x1, y1, sx, sy)
    print sx, sy
    kock(n-1, sx, sy, ux, uy)
    print ux, uy
    kock(n-1, ux, uy, tx, ty)
    print tx, ty
    kock(n-1, tx, ty, x2, y2)

n = input()

p1x = float(0.)
p1y = float(0.)
p2x = float(100.)
p2y = float(0.)

print p1x, p1y
kock(n, p1x, p1y, p2x, p2y)
print p2x, p2y

Submission status: Accepted

Solution code:
import math

def kock(n, x1, y1, x2, y2):
    if n == 0:
        return

    angle = math.radians(60)

    sx = (2*x1 + x2)/3
    sy = (2*y1 + y2)/3
    tx = (2*x2 + x1)/3
    ty = (2*y2 + y1)/3
    ux = (tx - sx)*math.cos(angle) - (ty - sy)*math.sin(angle) + sx
    uy = (tx - sx)*math.sin(angle) + (ty - sy)*math.cos(angle) + sy

    kock(n-1, x1, y1, sx, sy)
    print sx, sy
    kock(n-1, sx, sy, ux, uy)
    print ux, uy
    kock(n-1, ux, uy, tx, ty)
    print tx, ty
    kock(n-1, tx, ty, x2, y2)

n = input()

p1x = 0.
p1y = 0.
p2x = 100.
p2y = 0.

print p1x, p1y
kock(n, p1x, p1y, p2x, p2y)
print p2x, p2y

Submission status: Accepted

Solution code:

n = input()

def koch(n,p1,p2):
    s = [0] * 2 
    t = [0] * 2 
    u = [0] * 2 
    if n == 0:
        return 0
    s[0] = (2.00 * p1[0] + 1.00 * p2[0]) / 3.00
    s[1] = (2.00 * p1[1] + 1.00 * p2[1]) / 3.00
    t[0] = (1.00 * p1[0] + 2.00 * p2[0]) / 3.00
    t[1] = (1.00 * p1[1] + 2.00 * p2[1]) / 3.00
    u[0] = (t[0] - s[0]) * (1.00 / 2.00) - (t[1] - s[1]) * (3.00**0.5 / 2.00) + s[0] 
    u[1] = (t[0] - s[0]) * (3.00**0.5 / 2.00) + (t[1] - s[1]) * (1.00 / 2.00) + s[1]    


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




start = [0.00,0.00]
goal = [100.00,0.00]
print "{0:.10f}".format(start[0]),"{0:.10f}".format(start[1])
koch(n,start,goal)
print "{0:.10f}".format(goal[0]),"{0:.10f}".format(goal[1])

Submission status: Accepted

Solution code:
import math, cmath
 
def koch(p1, p2, n):
    if n == 0:
        return [p1, p2]
     
    rad60 = math.pi/3
    v1 = (p2 - p1)/3
    v2 = cmath.rect(abs(v1), cmath.phase(v1)+rad60)
    q1 = p1 + v1
    q2 = q1 + v2
    q3 = q1 + v1
    if n == 1:
        return [p1,q1,q2,q3,p2]
    else:
        x1 = koch(p1,q1,n-1)[:-1]
        x2 = koch(q1,q2,n-1)[:-1]
        x3 = koch(q2,q3,n-1)[:-1]
        x4 = koch(q3,p2,n-1)
        x = x1 + x2 + x3 + x4
    return x
 
n = int(raw_input())
p1 = complex(0,0)
p2 = complex(100,0)
 
for e in koch(p1,p2,n):
    print e.real,e.imag

Submission status: Accepted

Solution code:
import math, cmath
 
def koch(v1, v2, n):
    if n == 0:
        return [v1, v2]
     
    rad60 = math.pi/3
    u1 = (v2 - v1)/3
    u2 = cmath.rect(abs(u1), cmath.phase(u1)+rad60)
    q1 = v1 + u1
    q2 = q1 + u2
    q3 = q1 + u1
    if n == 1:
        return [v1,q1,q2,q3,v2]
    else:
        x1 = koch(v1,q1,n-1)[:-1]
        x2 = koch(q1,q2,n-1)[:-1]
        x3 = koch(q2,q3,n-1)[:-1]
        x4 = koch(q3,v2,n-1)
        x = x1 + x2 + x3 + x4
    return x
 
n = int(raw_input())
p1 = complex(0,0)
p2 = complex(100,0)
 
for e in koch(p1,p2,n):
    print e.real,e.imag

Submission status: Accepted

Solution code:
import math, cmath
 
def koch(v1, v2, n):
    if n == 0:
        return [v1, v2]
     
    rad60 = math.pi/3
    u1 = (v2 - v1)/3
    u2 = cmath.rect(abs(u1), cmath.phase(u1)+rad60)
    q1 = v1 + u1
    q2 = q1 + u2
    q3 = q1 + u1
    if n == 1:
        return [v1,q1,q2,q3,v2]
    else:
        x1 = koch(v1,q1,n-1)[:-1]
        x2 = koch(q1,q2,n-1)[:-1]
        x3 = koch(q2,q3,n-1)[:-1]
        x4 = koch(q3,v2,n-1)
        x = x1 + x2 + x3 + x4
    return x
 
n = int(raw_input())
v1 = complex(0,0)
v2 = complex(100,0)
 
for e in koch(v1,v2,n):
    print e.real,e.imag

Submission status: Accepted

Solution code:

 
import math

 
def koch_curve(i, n, x1, y1, x2, y2):
    x = [0]*5
    y = [0]*5
    if i < n:
        x[0] = x1
        y[0] = y1
        x[1] = (2*x1+x2)/3.0
        y[1] = (2*y1+y2)/3.0
        x[2] = (x1+2*x2)/3.0
        y[2] = (y1+2*y2)/3.0
        x[3] = (x[2] - x[1])*math.cos(math.pi/3) - (y[2] - y[1])*math.sin(math.pi/3) + x[1]
        y[3] = (y[2] - y[1])*math.cos(math.pi/3) + (x[2] - x[1])*math.sin(math.pi/3) + y[1]
        x[4] = x2
        y[4] = y2
        koch_curve(i+1, n, x[0], y[0], x[1], y[1])
        print x[1], y[1]
        koch_curve(i+1, n, x[1], y[1], x[3], y[3])
        print x[3], y[3]
        koch_curve(i+1, n, x[3], y[3], x[2], y[2])
        print x[2], y[2]
        koch_curve(i+1, n, x[2], y[2], x[4], y[4])
        
n = int(input())
 
x1 = 0
y1 = 0
x2 = 100
y2 = 0 
print x1, y1
koch_curve(0, n, x1, y1, x2, y2)
print x2, y2

Submission status: Accepted

Solution code:
import math

n=input()
m=math.radians(60)

def kock(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

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


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

p1x = 0.00000000
p1y = 0.00000000
p2x = 100.00000000
p2y = 0.00000000
 
print p1x, p1y
kock(n, p1x, p1y, p2x, p2y)
print p2x, p2y

Submission status: Accepted

Solution code:
import math
 
def kock(n, x1, y1, x2, y2):
    if n == 0:
        return
 
    angle = math.radians(60)
 
    sx = (2*x1 + x2)/3
    sy = (2*y1 + y2)/3
    tx = (2*x2 + x1)/3
    ty = (2*y2 + y1)/3
    ux = (tx - sx)*math.cos(angle) - (ty - sy)*math.sin(angle) + sx
    uy = (tx - sx)*math.sin(angle) + (ty - sy)*math.cos(angle) + sy
 
    kock(n-1, x1, y1, sx, sy)
    print sx, sy
    kock(n-1, sx, sy, ux, uy)
    print ux, uy
    kock(n-1, ux, uy, tx, ty)
    print tx, ty
    kock(n-1, tx, ty, x2, y2)
 
n = input()
 
p1x = 0.
p1y = 0.
p2x = 100.
p2y = 0.
 
print p1x, p1y
kock(n, p1x, p1y, p2x, p2y)
print p2x, p2y

Submission status: Accepted

Solution code:
import math
 
def koch(n, x1, y1, x2, y2):
    if n == 0:
        return
   
    angle = math.radians(60)
   
    sx = (2*x1 + x2)/3
    sy = (2*y1 + y2)/3
    tx = (2*x2 + x1)/3
    ty = (2*y2 + y1)/3
    ux = (tx - sx)*math.cos(angle) - (ty - sy)*math.sin(angle) + sx
    uy = (tx - sx)*math.sin(angle) + (ty - sy)*math.cos(angle) + sy
   
    koch(n-1, x1, y1, 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, x2, y2)
   
n = input()
   
p1x = 0.0
p1y = 0.0
p2x = 100.0
p2y = 0.0
   
print p1x, p1y
koch(n, p1x, p1y, p2x, p2y)
print p2x, p2y

Submission status: Accepted

Solution code:
import math
n = int(raw_input())
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)
     
start = (0.0, 0.0)
end = (100.0, 0.0)
print_point(start)
koch(n, start, end)
print_point(end)

Submission status: Accepted

Solution code:
import math

class KochCurve:
    def koch(self, d, p1, p2): 
        if d == 0:
            return
        th = math.pi * 60.0 / 180.0
        sx = (2.0 * p1[0] + 1.0 * p2[0]) / 3
        sy = (2.0 * p1[1] + 1.0 * p2[1]) / 3
        s = [sx, sy]
        tx = (1.0 * p1[0] + 2.0 * p2[0]) / 3
        ty = (1.0 * p1[1] + 2.0 * p2[1]) / 3
        t = [tx, ty]
        ux = (tx - sx) * math.cos(th) - (ty - sy) * math.sin(th) + sx
        uy = (tx - sx) * math.sin(th) + (ty - sy) * math.cos(th) + sy
        u = [ux, uy]
        
        self.koch(d-1, p1, s)
        print("{} {}".format(sx, sy))
        
        self.koch(d-1, s, u)
        print("{} {}".format(ux, uy))

        self.koch(d-1, u, t)
        print("{} {}".format(tx, ty))
        
        self.koch(d-1, t, p2)

if __name__ == '__main__':
    n = int(input().rstrip())
    p1 = [0, 0]
    p2 = [100, 0]
    print("{} {}".format(p1[0], p1[1]))
    x = KochCurve()
    x.koch(n, p1, p2)
    print("{} {}".format(p2[0], p2[1]))