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: Runtime Error

Solution code:
import math
from matplotlib import pyplot
class Dot:
    x=0.0
    y=0.0
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __lt__(self,other):
        return self.x < other.x
    def print(self):
        x=self.x
        y=self.y
        print("{:.8f} {:.8f}".format(x,y))       
def kock(n,a,b,dots):
    if n == 0:
        return
    s=Dot((2*a.x+b.x)/3,(2*a.y+b.y)/3)
    t=Dot((a.x+2*b.x)/3,(a.y+2*b.y)/3)
    rad=math.radians(60)
    x=(t.x-s.x)*math.cos(rad)-(t.y-s.y)*math.sin(rad)+s.x
    y=(t.x-s.x)*math.sin(rad)+(t.y-s.y)*math.cos(rad)+s.y
    u=Dot(x,y)

    kock(n-1,a,s,dots)
    s.print()
    dots.append(s)
    kock(n-1,s,u,dots)
    u.print()
    dots.append(u)
    kock(n-1,u,t,dots)
    t.print()
    dots.append(t)
    kock(n-1,t,b,dots)

if __name__ == "__main__":
    n=int(input())
    dots=[]
    dot1=Dot(0,0)
    dot2=Dot(100,0)
    dots.append(dot1)
    dots.append(dot2)
    dot1.print()
    kock(n,dot1,dot2,dots)
    dot2.print()

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<cstdio>
#include<cmath>
int n;

Struct Point{
	double x;
	double y;
}

void calc(int m,Point p1,Point p2){
	if(m>=n){
		return;
	}
	Point s,t,u;
	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;
	s.y=(1*p1.y+2*p2.y)/3.0;
	u.x=(t.x-s.x)*(1/2.0)-(t.y-s.y)*(sqrt(3)/2.0)+s.x;
	u.x=(t.x-s.x)*(sqrt(3)/2.0)+(t.y-s.y)*(1/2.0)+s.y;
	calc(m+1,p1,s);
	printf("%.8f %.8f\n,s.x,s.y);
	calc(m+1,s,u);
	printf("%.8f %.8f\n,u.x,u.y);
	calc(m+1,u,t);
	printf("%.8f %.8f\n,t.x,t.y);
	calc(m+1,t,p2);
}

int main(){
	std::cin>>n;
	Point p1,p2;
	p1.x=0; p1.y=0; p2.x=100; p2.y=0;
	printf("%.8f %.8f\n,p1.x,p1.y);
	calc(0,p1,p2);
	printf("%.8f %.8f\n,p2.x,p2.y);
	
	retu

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<cstdio>
#include<cmath>
int n;

struct Point{
	double x;
	double y;
};

void calc(int m,Point p1,Point p2){
	if(m>=n){
		return;
	}
	Point s,t,u;
	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;
	s.y=(1*p1.y+2*p2.y)/3.0;
	u.x=(t.x-s.x)*(1/2.0)-(t.y-s.y)*(sqrt(3)/2.0)+s.x;
	u.x=(t.x-s.x)*(sqrt(3)/2.0)+(t.y-s.y)*(1/2.0)+s.y;
	calc(m+1,p1,s);
	printf("%.8f %.8f\n",s.x,s.y);
	calc(m+1,s,u);
	printf("%.8f %.8f\n",u.x,u.y);
	calc(m+1,u,t);
	printf("%.8f %.8f\n",t.x,t.y);
	calc(m+1,t,p2);
}

int main(){
	std::cin>>n;
	Point p1,p2;
	p1.x=0; p1.y=0; p2.x=100; p2.y=0;
	printf("%.8f %.8f\n",p1.x,p1.y);
	calc(0,p1,p2);
	printf("%.8f %.8f\n",p2.x,p2.y);
	
	return 0;
}

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<cstdio>
#include<cmath>
int n;

struct Point{
	double x;
	double y;
};

void calc(int m,Point p1,Point p2){
	if(m>=n){
		return;
	}
	Point s,t,u;
	s.x=(2.0*p1.x+p2.x)/3.0;
	s.y=(2.0*p1.y+p2.y)/3.0;
	t.x=(p1.x+2.0*p2.x)/3.0;
	t.y=(p1.y+2.0*p2.y)/3.0;
	u.x=(t.x-s.x)*(1.0/2.0)-(t.y-s.y)*(sqrt(3.0)/2.0)+s.x;
	u.x=(t.x-s.x)*(sqrt(3.0)/2.0)+(t.y-s.y)*(1.0/2.0)+s.y;
	calc(m+1,p1,s);
	printf("%.8f %.8f\n",s.x,s.y);
	calc(m+1,s,u);
	printf("%.8f %.8f\n",u.x,u.y);
	calc(m+1,u,t);
	printf("%.8f %.8f\n",t.x,t.y);
	calc(m+1,t,p2);
}

int main(){
	std::cin>>n;
	Point p1,p2;
	p1.x=0; p1.y=0; p2.x=100; p2.y=0;
	printf("%.8f %.8f\n",p1.x,p1.y);
	calc(0,p1,p2);
	printf("%.8f %.8f\n",p2.x,p2.y);
	
	return 0;
}

Submission status: Runtime Error

Solution code:
import math
  
n = int(input())
 
def koch(n,x_0,y_0,x_1,y_1):
    if n == 0:
        return  
    x_s = x_0 + (x_1- x_0)/3
    y_s = y_0 + (y_1 -y_0)/3
    x_t = x_0 + (x_1- x_0)* 2/3
    y_t = y_0 + (y_1 -y_0) *2/3
    x_u =  (x_t - x_s) * math.cos(math.pi/3) - (y_t - y_s) * math.sin(math.pi/3) + x_s
    y_u =  (x_t - x_s) * math.sin(math.pi/3) + (y_t - y_s) * math.cos(math.pi/3) + y_s
  
    koch(n-1,x_0,y_0,x_s,y_s)
    print(x_s,y_s)
    koch(n-1,x_s,y_s,x_u,y_u)
    print(x_u,y_u)
    koch(n-1,x_u,y_u,x_t,y_t)
    print(x_t,y_t)
    koch(n-1,x_t,y_t,x_1,y_1)
      
print(0,0)
koch(n,0,0,100,0)
print(100,0)

Submission status: Runtime Error

Solution code:
from math import sqrt
from numpy import array

def koch(n, start, end):
    if n != 0:
        vec = end - start
        normal_vec = array([-vec[1], vec[0]])
        a = start + vec/3
        b = start + vec/2 + sqrt(3)*normal_vec/6
        c = end - vec/3
        koch(n-1, start, a)
        print('{0:.5f} {1:.5f}'.format(a[0], a[1]))
        koch(n-1, a, b)
        print('{0:.5f} {1:.5f}'.format(b[0], b[1]))
        koch(n-1, b, c)
        print('{0:.5f} {1:.5f}'.format(c[0], c[1]))
        koch(n-1, c, end)


n = int(input())
print('0.0 100.0')
koch(n, array([0.0,0.0]), array([100.0,0.0]))
print('100.0 0.0')

Submission status: Runtime Error

Solution code:
from math import sqrt
from numpy import array

def koch(n, start, end):
    if n != 0:
        vec = end - start
        normal_vec = array([-vec[1], vec[0]])
        a = start + vec/3
        b = start + vec/2 + sqrt(3)*normal_vec/6
        c = end - vec/3
        koch(n-1, start, a)
        print('{0:.5f} {1:.5f}'.format(a[0], a[1]))
        koch(n-1, a, b)
        print('{0:.5f} {1:.5f}'.format(b[0], b[1]))
        koch(n-1, b, c)
        print('{0:.5f} {1:.5f}'.format(c[0], c[1]))
        koch(n-1, c, end)


n = int(input())
print('0.00000 100.00000')
koch(n, array([0.0,0.0]), array([100.0,0.0]))
print('100.00000 0.00000')

Submission status: Runtime Error

Solution code:
from math import sqrt
from numpy import array

def koch(n, start, end):
    if n != 0:
        vec = end - start
        normal_vec = array([-vec[1], vec[0]])
        a = start + vec/3
        b = start + vec/2 + sqrt(3)*normal_vec/6
        c = end - vec/3
        koch(n-1, start, a)
        print('{0:.5f} {1:.5f}'.format(a[0], a[1]))
        koch(n-1, a, b)
        print('{0:.5f} {1:.5f}'.format(b[0], b[1]))
        koch(n-1, b, c)
        print('{0:.5f} {1:.5f}'.format(c[0], c[1]))
        koch(n-1, c, end)


n = int(input())
print('0.00000 0.00000')
koch(n, array([0.0,0.0]), array([100.0,0.0]))
print('100.00000 0.00000')

Submission status: Runtime Error

Solution code:
from math import sqrt
from numpy import array

def koch(n, start, end):
    if n != 0:
        vec = end - start
        normal_vec = array([-vec[1], vec[0]])
        a = start + vec/3
        b = start + vec/2 + sqrt(3)*normal_vec/6
        c = end - vec/3
        koch(n-1, start, a)
        print(a[0], a[1])
        koch(n-1, a, b)
        print(b[0], b[1])
        koch(n-1, b, c)
        print(c[0], c[1])
        koch(n-1, c, end)


n = int(input())
start = array([0.0, 0.0])
end = array([100.0,0.0])
print(start[0], end[1])
koch(n, start, end)
print(start[0], end[1])

Submission status: Runtime Error

Solution code:
#coding:utf-8
import numpy as np
import math
pi = math.pi
cos = math.cos
sin = math.sin


n = int(input())
A, B = np.array([0,0]), np.array([100,0])
def inc(A, B):
    AB = B - A
    rot = np.array([[cos(pi/2), -sin(pi/2)],[sin(pi/2), cos(pi/2)]])
    convertedAB = np.dot(rot, AB)
    normAB = np.linalg.norm(AB)
    D = AB / 2 + (3**(1/2)/6) * convertedAB
    return D


def koch(A, B, i):
    AB = B - A
    if i >= n:
        return 1
    C = A + AB/3
    koch(A,C,i+1)
    print(C)
    D = inc(A, B)
    koch(C,D,i+1)
    print(D)
    E = A + 2*AB/3
    koch(D,E,i+1)
    print(E)
    koch(E,B,i+1)

    koch(A, C, i+1),koch(C, D, i+1), koch(D, E, i+1), koch(E,B,i+1)


print(A)
i = 0
koch(A,B,i)
print(B)

Submission status: Runtime Error

Solution code:
#coding:utf-8
import numpy as np
import math
pi = math.pi
cos = math.cos
sin = math.sin


n = int(input())
A, B = np.array([0,0]), np.array([100,0])
def inc(A, B):
    AB = B - A
    rot = np.array([[cos(pi/2), -sin(pi/2)],[sin(pi/2), cos(pi/2)]])
    convertedAB = np.dot(rot, AB)
    normAB = np.linalg.norm(AB)
    D = AB / 2 + (3**(1/2)/6) * convertedAB
    return D


def koch(A, B, i):
    AB = B - A
    if i >= n:
        return 1
    C = A + AB/3
    koch(A,C,i+1)
    print(" ".join([str(num) for num in C]))
    D = inc(A, B)
    koch(C,D,i+1)
    print(" ".join([str(num) for num in D]))
    E = A + 2*AB/3
    koch(D,E,i+1)
    print(" ".join([str(num) for num in E]))
    koch(E,B,i+1)

    koch(A, C, i+1),koch(C, D, i+1), koch(D, E, i+1), koch(E,B,i+1)


print(" ".join([str(num) for num in A]))
i = 0
koch(A,B,i)
print(" ".join([str(num) for num in B]))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import numpy as np
import math
pi = math.pi
cos = math.cos
sin = math.sin


n = int(input())
A, B = np.array([0,0]), np.array([100,0])
try:
    n2 = input()
except:
    pass


def inc(A, B):
    AB = B - A
    rot = np.array([[cos(pi/2), -sin(pi/2)],[sin(pi/2), cos(pi/2)]])
    convertedAB = np.dot(rot, AB)
    normAB = np.linalg.norm(AB)
    D = AB / 2 + (3**(1/2)/6) * convertedAB
    return D


def koch(A, B, i):
    AB = B - A
    if i >= n:
        return 1
    C = A + AB/3
    koch(A,C,i+1)
    print(" ".join([str(num) for num in C]))
    D = inc(A, B)
    koch(C,D,i+1)
    print(" ".join([str(num) for num in D]))
    E = A + 2*AB/3
    koch(D,E,i+1)
    print(" ".join([str(num) for num in E]))
    koch(E,B,i+1)

    koch(A, C, i+1),koch(C, D, i+1), koch(D, E, i+1), koch(E,B,i+1)


print(" ".join([str(num) for num in A]))
i = 0
koch(A,B,i)
print(" ".join([str(num) for num in B]))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import numpy as np
import math
pi = math.pi
cos = math.cos
sin = math.sin
n = int(input())
A, B = np.array([0,0]), np.array([100,0])

def inc(A, B):
    AB = B - A
    rot = np.array([[cos(pi/2), -sin(pi/2)],[sin(pi/2), cos(pi/2)]])
    convertedAB = np.dot(rot, AB)
    normAB = np.linalg.norm(AB)
    D = AB / 2 + (3**(1/2)/6) * convertedAB
    return D


def koch(A, B, i):
    AB = B - A
    if i >= n:
        return 1
    C = A + AB/3
    koch(A,C,i+1)
    print(" ".join([str(num) for num in C]))
    D = inc(A, B)
    koch(C,D,i+1)
    print(" ".join([str(num) for num in D]))
    E = A + 2*AB/3
    koch(D,E,i+1)
    print(" ".join([str(num) for num in E]))
    koch(E,B,i+1)

    koch(A, C, i+1),koch(C, D, i+1), koch(D, E, i+1), koch(E,B,i+1)


print(" ".join([str(num) for num in A]))
i = 0
koch(A,B,i)
print(" ".join([str(num) for num in B]))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import numpy as np
import math
pi = math.pi
cos = math.cos
sin = math.sin
n = int(input())
if n == 0:
    n = int(input())
A, B = np.array([0,0]), np.array([100,0])

def inc(A, B):
    AB = B - A
    rot = np.array([[cos(pi/2), -sin(pi/2)],[sin(pi/2), cos(pi/2)]])
    convertedAB = np.dot(rot, AB)
    normAB = np.linalg.norm(AB)
    D = AB / 2 + (3**(1/2)/6) * convertedAB
    return D


def koch(A, B, i):
    AB = B - A
    if i >= n:
        return 1
    C = A + AB/3
    koch(A,C,i+1)
    print(" ".join([str(num) for num in C]))
    D = inc(A, B)
    koch(C,D,i+1)
    print(" ".join([str(num) for num in D]))
    E = A + 2*AB/3
    koch(D,E,i+1)
    print(" ".join([str(num) for num in E]))
    koch(E,B,i+1)

    koch(A, C, i+1),koch(C, D, i+1), koch(D, E, i+1), koch(E,B,i+1)


print(" ".join([str(num) for num in A]))
i = 0
koch(A,B,i)
print(" ".join([str(num) for num in B]))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import numpy as np
import math
pi = math.pi
cos = math.cos
sin = math.sin
n = int(input())
if n == 0:
    n = int(input())
A, B = np.array([0,0]), np.array([100,0])

def inc(A, B):
    AB = B - A
    rot = np.array([[cos(pi/2), -sin(pi/2)],[sin(pi/2), cos(pi/2)]])
    convertedAB = np.dot(rot, AB)
    normAB = np.linalg.norm(AB)
    D = AB / 2 + (3**(1/2)/6) * convertedAB
    return D


def koch(A, B, i):
    AB = B - A
    if i >= n:
        return 1
    C = A + AB/3
    koch(A,C,i+1)
    print(" ".join([str(num) for num in C]))
    D = inc(A, B)
    koch(C,D,i+1)
    print(" ".join([str(num) for num in D]))
    E = A + 2*AB/3
    koch(D,E,i+1)
    print(" ".join([str(num) for num in E]))
    koch(E,B,i+1)

    koch(A, C, i+1),koch(C, D, i+1), koch(D, E, i+1), koch(E,B,i+1)


print(" ".join([str(flaot(num)) for num in A]))
i = 0
koch(A,B,i)
print(" ".join([str(float(num)) for num in B]))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import numpy as np
import math
pi = math.pi
cos = math.cos
sin = math.sin
n = int(input())
if n == 0:
    n = int(input())
A, B = np.array([0,0]), np.array([100,0])

def inc(A, B):
    AB = B - A
    rot = np.array([[cos(pi/2), -sin(pi/2)],[sin(pi/2), cos(pi/2)]])
    convertedAB = np.dot(rot, AB)
    normAB = np.linalg.norm(AB)
    D = AB / 2 + (3**(1/2)/6) * convertedAB
    return D


def koch(A, B, i):
    AB = B - A
    if i >= n:
        return 1
    C = A + AB/3
    koch(A,C,i+1)
    print(" ".join([str(num) for num in C]))
    D = inc(A, B)
    koch(C,D,i+1)
    print(" ".join([str(num) for num in D]))
    E = A + 2*AB/3
    koch(D,E,i+1)
    print(" ".join([str(num) for num in E]))
    koch(E,B,i+1)

    koch(A, C, i+1),koch(C, D, i+1), koch(D, E, i+1), koch(E,B,i+1)


print(" ".join([str(float(num)) for num in A]))
i = 0
koch(A,B,i)
print(" ".join([str(float(num)) for num in B]))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import numpy as np
import math
pi = math.pi
cos = math.cos
sin = math.sin
n = int(input())
A, B = np.array([0,0]), np.array([100,0])

def inc(A, B):
    AB = B - A
    rot = np.array([[cos(pi/2), -sin(pi/2)],[sin(pi/2), cos(pi/2)]])
    convertedAB = np.dot(rot, AB)
    normAB = np.linalg.norm(AB)
    D = AB / 2 + (3**(1/2)/6) * convertedAB
    return D


def koch(A, B, i):
    AB = B - A
    if i >= n:
        return 1
    C = A + AB/3
    koch(A,C,i+1)
    print(" ".join([str(num) for num in C]))
    D = inc(A, B)
    koch(C,D,i+1)
    print(" ".join([str(num) for num in D]))
    E = A + 2*AB/3
    koch(D,E,i+1)
    print(" ".join([str(num) for num in E]))
    koch(E,B,i+1)

    koch(A, C, i+1),koch(C, D, i+1), koch(D, E, i+1), koch(E,B,i+1)


print(" ".join([str(float(num)) for num in A]))
i = 0
koch(A,B,i)
print(" ".join([str(float(num)) for num in B]))

Submission status: Runtime Error

Solution code:
import math, cmath

def koch(p1, p2, n):

    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:-1]
        x2 = koch(q1,q2,n-1)[1:-1]
        x3 = koch(q2,q3,n-1)[1:-1]
        x4 = koch(q3,p2,n-1)[1:-1]
        x = [p1]+x1+[q1]+x2+[q2]+x3+[q3]+x4+[p2]
    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: Runtime Error

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