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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)])
        #print(initialx,endx,onex,oney,secondx,secondy,thirdx,thirdy)
        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")

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

Submission status: Accepted

Solution code:
#coding:UTF-8
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')))

Submission status: Accepted

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_C&lang=jp
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]))

Submission status: Accepted

Solution code:
# encoding: utf-8


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:
        # print('end')
        return None

    # s, t, u = Point(), Point, Point()
    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'))

Submission status: Accepted

Solution code:
# encoding: utf-8


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:
        # print('end')
        return None

    # why can't it be like below?
    # s, t, u = Point(), Point, Point()
    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'))

Submission status: Accepted

Solution code:
# encoding: utf-8


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:
        # print('end')
        return None

    # why can't it be like below?
    # s, t, u = Point(), Point, Point()
    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'))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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 = open("test.txt", "r")
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]))

Submission status: Accepted

Solution code:
import math

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

    #p1, p2??????s, t, u????¨????
    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")