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 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))

Solution (Python):
import math
class point:
    def __init__(self):
        self.x = 0.0
        self.y = 0.0
 
def koch(n,a,b):
    if n == 0 :
        return
 
    rad = 1.0471975511965976
    s = point()
    t = point()
    u = point()
    tmp = point()
 
    s.x = (2 * a.x + b.x) / 3.0
    s.y = (2 * a.y + b.y) / 3.0
 
    t.x = (2 * b.x + a.x) / 3.0
    t.y = (2 * b.y + a.y) / 3.0
 
    tmp.x, tmp.y = s.x-t.x ,s.y-t.y
    u.x = (tmp.x * math.cos(rad) + tmp.y * math.sin(rad)) + t.x
    u.y = (-tmp.x * math.sin(rad) + tmp.y * math.cos(rad)) + t.y
 
 
    koch(n-1, a, s)
    print(s.x, s.y)
 
    koch(n-1, s, u)
    print(u.x, u.y)
 
    koch(n-1, u, t)
    print(t.x, t.y)
 
    koch(n-1, t, b)
 
    return
 
a = point()
b = point()
a.x, a.y =   0.0, 0.0
b.x, b.y = 100.0, 0.0
n = int(input())

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

Solution (Python):
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

Solution (Python):
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)

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

Solution (Python):
import math
n = int(raw_input())
 
p1_x = float(0)
p1_y = float(0)
p2_x = float(100)
p2_y = float(0)
 
def KC(i, n, x1, y1, x2, y2):
    v = [0] * 5
    w = [0] * 5
    if i < n:
        v[0] = x1
        w[0] = y1
        v[1] = (2 * x1 + x2) / 3.0
        w[1] = (2 * y1 + y2) / 3.0
        v[2] = (x1 + 2 * x2) / 3.0
        w[2] = (y1 + 2 * y2) / 3.0
        v[3] = (v[2] - v[1]) * math.cos(math.pi/3) - (w[2] - w[1]) * math.sin(math.pi/3) + v[1]
        w[3] = (w[2] - w[1]) * math.cos(math.pi/3) + (v[2] - v[1]) * math.sin(math.pi/3) + w[1]
        v[4] = x2
        w[4] = y2
        KC(i+1, n, v[0], w[0], v[1], w[1])
        print v[1], w[1]
        KC(i+1, n, v[1], w[1], v[3], w[3])
        print v[3], w[3]
        KC(i+1, n, v[3], w[3], v[2], w[2])
        print v[2], w[2]
        KC(i+1, n, v[2], w[2], v[4], w[4])
 
print p1_x, p1_y
KC(0, n, p1_x, p1_y, p2_x, p2_y)
print p2_x, p2_y

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

Solution (Python):
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

Solution (Python):
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

Solution (Python):

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])

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

Solution (Python):
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

Solution (Python):
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

Solution (Python):

 
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

Solution (Python):
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

Solution (Python):
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

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

Solution (Python):
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

Solution (Python):
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)

Solution (Python):
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]))

Solution (Python):
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]))

Solution (Python):
import math
n = int(input())
 
def koch(n, p1_x, p2_x, p1_y, p2_y):
  if n == 0:
    return
 
  s_x = (2*p1_x + p2_x) / 3
  s_y = (2*p1_y + p2_y) / 3
  t_x = (p1_x + 2*p2_x) / 3
  t_y = (p1_y + 2*p2_y) / 3
 
  u_x = (t_x - s_x)/2 - (t_y - s_y) * math.sqrt(3)/2 + s_x
  u_y = (t_x - s_x) * math.sqrt(3)/2 + (t_y - s_y)/2 + s_y
 
  koch(n-1, p1_x, s_x, p1_y, s_y)
  print (s_x, s_y)
  koch(n-1, s_x, u_x, s_y, u_y)
  print (u_x, u_y)
  koch(n-1, u_x, t_x, u_y, t_y)
  print (t_x, t_y)
  koch(n-1, t_x, p2_x, t_y, p2_y)
 
p1_x = 0.0
p1_y = 0.0
p2_x = 100.0
p2_y = 0.0
 
print (p1_x, p1_y)
koch(n, 0, 100, 0, 0)
print (p2_x, p2_y)

Solution (Python):
import math

n = int(raw_input())

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

th = math.pi/3.0

def koch(n, p1, p2):
    if n == 0:
        return
    s = Point()
    t = Point()
    u = Point()

    s.x = (2*p1.x + 1*p2.x) / 3.0
    s.y = (2*p1.y + 1*p2.y) / 3.0
    t.x = (1*p1.x + 2*p2.x) / 3.0
    t.y = (1*p1.y + 2*p2.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,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)
    
def main():
    p1 = Point()
    p2 = Point()

    p1.x = 0.0
    p1.y = 0.0
    p2.x = 100.0
    p2.y = 0.0

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

    return 0

main()

Solution (Python):
import math
 
rad = math.radians(60)
 
class coordinates:
  def __init__(self, x, y):
    self.x = x
    self.y = y
  
def koch(n, a, b):
  if n == 0:
    return
  s = coordinates((2.0*a.x + b.x)/3.0, (2.0*a.y + b.y)/3.0)
  t = coordinates((a.x + 2.0*b.x)/3.0, (a.y + 2.0*b.y)/3.0)
  ux = (t.x - s.x)*math.cos(rad) - (t.y - s.y)*math.sin(rad) + s.x
  uy = (t.x - s.x)*math.sin(rad) + (t.y - s.y)*math.cos(rad) + s.y
  u = coordinates(ux, uy)
   
  koch(n-1, a, s)
  print  s.x, s.y
  koch(n-1, s, u)
  print  u.x, u.y
  koch(n-1, u, t)
  print  t.x, t.y
  koch(n-1, t, b)
   
n = input()
   
p1 = coordinates(0.0, 0.0)
p2 = coordinates(100.0, 0.0)
   
print p1.x, p1.y
koch(n, p1, p2)
print p2.x, p2.y

Solution (Python):
def koch_curve(points, n):
    if n == 0:
        return points
    result = []
    for i in range(len(points) - 1):
        x = points[i+1][0] - points[i][0]
        y = points[i+1][1] - points[i][1]
        s = [points[i][0]+x/3, points[i][1]+y/3]
        t = [points[i][0]+2*x/3, points[i][1]+2*y/3]
        u_x = t[0]/2 - t[1]*(3**0.5/2) + s[0]/2+(3**0.5)*s[1]/2
        u_y = t[0]*3**0.5/2 + t[1]/2 + s[1]/2-(3**0.5)*s[0]/2
        u = [u_x, u_y]
        result.extend([points[i], s, u, t])
    result.append(points.pop())
    return koch_curve(result, n - 1)

n = int(input())
for point in koch_curve([[0.0, 0.0], [100.0, 0.0]], n):
    print(*map(str, point))

Solution (Python):
import math
def kochcurve(n,p1x,p1y,p2x,p2y):
    if n==0:
        print("%5f %5f"%(p2x,p2y))
        return
    sx=(p2x-p1x)/3.0+p1x
    sy=(p2y-p1y)/3.0+p1y
    tx=(p2x-p1x)*2.0/3+p1x
    ty=(p2y-p1y)*2.0/3+p1y
    ux=math.cos(math.pi/3)*(tx-sx)-math.sin(math.pi/3)*(ty-sy)+sx
    uy=math.sin(math.pi/3)*(tx-sx)+math.cos(math.pi/3)*(ty-sy)+sy
    kochcurve(n-1,p1x,p1y,sx,sy)
    kochcurve(n-1,sx,sy,ux,uy)
    kochcurve(n-1,ux,uy,tx,ty)
    kochcurve(n-1,tx,ty,p2x,p2y)
    

n=input()
p1x=0.0
p1y=0.0
p2x=100.0
p2y=0.0
print("%5f %5f"%(p1x,p1y))
kochcurve(n,p1x,p1y,p2x,p2y)

Solution (Python):
import math

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

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

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

if __name__ == "__main__":
    n = int(input())
    p1 = (0, 0)
    p2 = (100, 0)
    print "%5f %5f" % (p1[0], p1[1])
    kochCurve(p1, p2, n)

Solution (Python):
import math

def koch(a1, a2, b1, b2, n):
    if n <= 0:
        return 0

    s1 = 2.0/3.0*a1 + 1.0/3.0*b1
    s2 = 2.0/3.0*a2 + 1.0/3.0*b2
    t1 = 1.0/3.0*a1 + 2.0/3.0*b1
    t2 = 1.0/3.0*a2 + 2.0/3.0*b2
    u1 = 1.0/6.0*(3.0*a1 + 3.0*b1 + math.sqrt(3.0)*a2 - math.sqrt(3.0)*b2)
    u2 = 1.0/6.0*(-1.0*math.sqrt(3.0)*a1 + math.sqrt(3.0)*b1 + 3.0*a2 + 3.0*b2)
    
    koch(a1, a2, s1, s2, n-1)
    print s1, s2
    koch(s1, s2, u1, u2, n-1)
    print u1, u2
    koch(u1, u2, t1, t2, n-1)
    print t1, t2
    koch(t1, t2, b1, b2, n-1)
    
if __name__ == '__main__':
    n = int(raw_input())
    a1, a2, b1, b2 = 0.0, 0.0, 100.0, 0.0
    print a1, a2
    koch(a1, a2, b1, b2, n)
    print b1, b2

Solution (Python):
import math

def koch(a1, a2, b1, b2, n):
    if n <= 0:
        return 0

    s1 = 2.0/3.0*a1 + 1.0/3.0*b1
    s2 = 2.0/3.0*a2 + 1.0/3.0*b2
    t1 = 1.0/3.0*a1 + 2.0/3.0*b1
    t2 = 1.0/3.0*a2 + 2.0/3.0*b2
    u1 = 1.0/6.0*(3.0*a1 + 3.0*b1 + math.sqrt(3.0)*a2 - math.sqrt(3.0)*b2)
    u2 = 1.0/6.0*(-1.0*math.sqrt(3.0)*a1 + math.sqrt(3.0)*b1 + 3.0*a2 + 3.0*b2)
    
    koch(a1, a2, s1, s2, n-1)
    print s1, s2
    koch(s1, s2, u1, u2, n-1)
    print u1, u2
    koch(u1, u2, t1, t2, n-1)
    print t1, t2
    koch(t1, t2, b1, b2, n-1)
    
if __name__ == '__main__':
    n = int(raw_input())
    a1, a2, b1, b2 = 0.0, 0.0, 100.0, 0.0
    print a1, a2
    koch(a1, a2, b1, b2, n)
    print b1, b2

Solution (Python):
import math

n = int(input())
theta = math.pi / 3
p1 = [0, 0]
p2 = [100, 0]


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

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

    ux = (t[0] - s[0]) * math.cos(theta) - (t[1] - s[1]) * math.sin(theta) + sx
    uy = (t[1] - s[1]) * math.cos(theta) + (t[0] - s[0]) * math.sin(theta) + sy
    u = [ux, uy]

    kock(d - 1, p1, s)
    print("{0:.8f} {1:.8f}".format(*s))
    kock(d - 1, s, u)
    print("{0:.8f} {1:.8f}".format(*u))
    kock(d -1 ,u, t)
    print("{0:.08f} {1:.8f}".format(*t))
    kock(d - 1, t, p2)

if __name__ == '__main__':
    print("0.00000000 0.00000000")
    kock(n, p1, p2)
    print("100.00000000 0.00000000")

Solution (Python):


import math

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

    def printPoint(self):
        print "%f %f" %(self.x, self.y)

def koch(n, p1, p2):
    if n == 0:
        return
    sin60 = math.sin(math.radians(60))
    cos60 = math.cos(math.radians(60))
    s = Point_Class((2*p1.x+p2.x)/3, (2*p1.y+p2.y)/3)
    t = Point_Class((p1.x+2*p2.x)/3, (p1.y+2*p2.y)/3)
    u = Point_Class((t.x-s.x)*cos60-(t.y-s.y)*sin60+s.x, (t.x-s.x)*sin60+(t.y-s.y)*cos60+s.y)
    koch(n-1, p1, s)
    s.printPoint()
    koch(n-1, s, u)
    u.printPoint()
    koch(n-1, u, t)
    t.printPoint()
    koch(n-1, t, p2)

n = int(raw_input())
p1 = Point_Class(0.0, 0.0)
p2 = Point_Class(100.0, 0.0)
p1.printPoint()
koch(n, p1, p2)
p2.printPoint()

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):
from math import pi,sin,cos

def print_p(p):
    print("{0:.8f} {1:.8f}".format(*p))

def koch(n,p1,p2):
    if n==0: return
    s = tuple(map(lambda z: (2*z[0]+  z[1])/3, zip(p1,p2)))
    t = tuple(map(lambda z: (  z[0]+2*z[1])/3, zip(p1,p2)))
    u = ( (t[0]-s[0])*cos(60*(pi/180)) - (t[1]-s[1])*sin(60*(pi/180)) + s[0] \
         ,(t[0]-s[0])*sin(60*(pi/180)) + (t[1]-s[1])*cos(60*(pi/180)) + s[1] )
    koch(n-1,p1,s)
    print_p(s)
    koch(n-1,s,u)
    print_p(u)
    koch(n-1,u,t)
    print_p(t)
    koch(n-1,t,p2)

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

Solution (Python):
import math


def koch(m, p1, p2):
    if m == 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]]

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


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

Solution (Python):
import math

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

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

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

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

    dx = tx - sx
    dy = ty - sy

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

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

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

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

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

n = int(input())

ax = 0
ay = 0

bx = 100
by = 0

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

Solution (Python):
import math
def Koch(initialx,endx,initialy,endy,repeat):
    if repeat > num:
        return 0
    else:
        if initialx >= endx:
            onex,secondx = abs(endx-initialx) * 2 / 3 + endx,abs(endx-initialx) / 3 + endx
        else:
            onex,secondx = abs(endx-initialx) / 3 + initialx,abs(endx-initialx) * 2 / 3 + initialx
        if initialy <= endy:
            oney,secondy = abs(endy-initialy) / 3 +initialy,abs(endy-initialy) * 2 / 3 + initialy
        else:
            oney,secondy = initialy - abs(endy-initialy) / 3 ,initialy - abs(endy-initialy) * 2 / 3

        if initialy == endy:
            thirdy = math.sqrt(3) / 2 * (secondx-onex) + oney
            thirdx = (onex + secondx) / 2
        else:
            if initialy > endy:
                if initialx > endx:
                    thirdx = initialx
                    thirdy = secondy
                else:
                    thirdx = endx
                    thirdy = oney
            else:
                if initialx > endx:
                    thirdx = endx
                    thirdy = oney
                else:
                    thirdx = initialx
                    thirdy = secondy
        if [round(initialx,8),round(initialy,8)] not in ans:
            ans.append([round(initialx,8),round(initialy,8)])
        
        Koch(initialx,onex,initialy,oney,repeat+1)
        Koch(onex,thirdx,oney,thirdy,repeat+1)
        Koch(thirdx,secondx,thirdy,secondy,repeat+1)
        Koch(secondx,endx,secondy,endy,repeat+1)
global num
num = int(input())
ans = []
Koch(0,100,0,0,0)
for n in range(len(ans)):
    print("%.8f %.8f" % (ans[n][0],ans[n][1]))
print("100.00000000 0.00000000")

Solution (Python):
import math
 
c60 = math.cos(math.radians(60))
s60 = math.sin(math.radians(60))
 
def koch(d, p1, p2):
    """ Print the coordinates of the vertices of Koch Curve.
    d is the depth of recursion.
    p1, p2 are coordinates of end point of the initial state.
    """
    if d == 0:
        return
 
    sx = (2 * p1[0] + p2[0]) / 3
    sy = (2 * p1[1] + p2[1]) / 3
 
    tx = (p1[0] + 2 * p2[0]) / 3
    ty = (p1[1] + 2 * p2[1]) / 3
 
    dx = tx - sx
    dy = ty - sy
 
    ux = dx * c60 - dy * s60 + sx
    uy = dx * s60 + dy * c60 + sy
 
    koch(d - 1, p1, (sx, sy))
    print("{0:.8f} {1:.8f}".format(sx, sy))
 
    koch(d - 1, (sx, sy), (ux, uy))
    print("{0:.8f} {1:.8f}".format(ux, uy))
 
    koch(d - 1, (ux, uy), (tx, ty))
    print("{0:.8f} {1:.8f}".format(tx, ty))
 
    koch(d - 1, (tx, ty), p2)
 
 
n = int(input())
 
ax = 0
ay = 0
 
bx = 100
by = 0
 
print("{0:.8f} {1:.8f}".format(ax, ay))
koch(n, (ax, ay), (bx, by))
print("{0:.8f} {1:.8f}".format(bx, by))

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

def three(p1x, p1y, p2x, p2y):
    dx = (p2x - p1x)
    dy = (p2y - p1y)
    sx = dx / 3 + p1x
    sy = dy / 3 + p1y
    tx = p2x - (dx / 3)
    ty = p2y - (dy / 3)

    mtx = tx - sx
    mty = ty - sy

    rad = math.radians(60)
    mux = mtx * math.cos(rad) - (mty * math.sin(rad))
    muy = mtx * math.sin(rad) + (mty * math.cos(rad))

    ux = mux + sx
    uy = muy + sy

    return [sx, sy, ux, uy, tx, ty]

def koch(p1x, p1y, p2x, p2y, n):
    if 0 == n:
        print('{:.8f} {:.8f}'.format(p1x, p1y))
    if 0 < n:
        sx, sy, ux, uy, tx, ty = three(p1x, p1y, p2x, p2y)
        koch(p1x, p1y, sx, sy, n - 1)
        koch(sx, sy, ux, uy, n - 1)
        koch(ux, uy, tx, ty, n - 1)
        koch(tx, ty, p2x, p2y, n - 1)

koch(0.00000000, 0.00000000, 100.00000000, 0.00000000, n)
print('{:.8f} {:.8f}'.format(100.00000000, 0.00000000))