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
n = int(input())

def fractal(A,B):
    global a1
    global a2
    global a3
    x1 = A[0]
    y1 = A[1]
    x2 = B[0]
    y2 = B[1]
    a1 = [(2*x1 + x2)/3, (2*y1 + y2)/3]
    a3 = [(x1 + 2*x2)/3, (y1 + 2*y2)/3]
    X = a3[0] - a1[0]
    Y = a3[1] - a1[1]
    a2 = [a1[0] + X/2 - Y*math.sqrt(3)/2, a1[1] + X*math.sqrt(3)/2 + Y/2]
    
A = [0,0]
B = [100,0]
fract = [A,B]
for i in range(n):
    l = len(fract)
    f = list(fract)
    for k in range(l-1):
        fractal(f[k], f[k+1])
        fract.insert(4*k+1,a1)
        fract.insert(4*k+2,a2)
        fract.insert(4*k+3,a3)        
for i in fract:
    print('{0:.6f}'.format(i[0]) + ' {0:.6f}'.format(i[1]))

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

def koch(n, sx, sy, gx, gy):
    if n == 0:
        print gx, gy
        return
    vx = gx - sx
    vy = gy - sy
    x1 = sx + vx / 3
    y1 = sy + vy / 3
    x3 = gx - vx / 3
    y3 = gy - vy / 3
    vx60 = vx / 2 - math.sqrt(3) * vy / 2
    vy60 = math.sqrt(3) * vx / 2 + vy / 2
    x2 = x1 + vx60 / 3
    y2 = y1 + vy60 / 3
    koch(n - 1, sx, sy, x1, y1)
    koch(n - 1, x1, y1, x2, y2)
    koch(n - 1, x2, y2, x3, y3)
    koch(n - 1, x3, y3, gx, gy)

n = input()
print 0, 0
koch(n, 0.0, 0.0, 100.0, 0.0)

Solution (Python):
import math
import os
import sys


def kochCurve(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)

    angle = math.radians(60)

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

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


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

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

Solution (Python):
import math

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


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

    th = math.pi * 60 / 180
    s = Point((2*a.x + b.x) / 3, (2*a.y + b.y) / 3)
    t = Point((a.x + 2*b.x) / 3, (a.y + 2*b.y) / 3)
    u = Point((t.x-s.x) * math.cos(th) - (t.y-s.y) * math.sin(th) + s.x,
              (t.x-s.x) * math.sin(th) + (t.y-s.y) * math.cos(th) + s.y)

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

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

print('{0:.8f} {1:.8f}'.format(a.x, a.y))
koch(n, a, b)
print('{0:.8f} {1:.8f}'.format(b.x, b.y))

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):
def next_koch(n):
    turn60 = 0.5 + 3 ** 0.5 * 0.5j
    n_next = [n[0]]
    for i in range(1, len(n)):
        s = (n[i - 1] * 2 + n[i]) / 3
        t = (n[i - 1] + n[i] * 2) / 3
        u = (t - s) * turn60 + s
        n_next +=[s, u, t, n[i]]
    return n_next
 
p1 = 0 + 0j
p2 = 100 + 0j
koch = [p1, p2]
 
n = int(input())
for i in range(n):
    koch = next_koch(koch)
 
for z in koch:
    print("{:.8f} {:.8f}".format(z.real, z.imag))

Solution (Python):

import math
cos60, sin60 = math.cos(math.pi/3),math.sin(math.pi/3)
class Point():
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __str__(self):
        return str(self.x)+' '+str(self.y)

def koch_curve(n, p1,p2):
    if n <= 0:
        return
    
    s = Point((2*p1.x + p2.x) / 3, (2*p1.y + p2.y) / 3)
    t = Point((p1.x + 2*p2.x) / 3, (p1.y + 2*p2.y) / 3)
    
    
    
    
    
    ux = (t.x-s.x)*cos60 - (t.y-s.y)*sin60 + s.x
    uy = (t.x-s.x)*sin60 + (t.y-s.y)*cos60 + s.y
    u = Point(ux, uy)
    
    
    koch_curve(n-1,p1,s)
    print(s)
    koch_curve(n-1,s,u)
    print(u)
    koch_curve(n-1,u,t)
    print(t)
    koch_curve(n-1,t,p2)

n = int(input())
ps = Point(0,0)
pe = Point(100,0)
print(ps)
koch_curve(n,ps,pe)
print(pe)