Parametric Surface

이때, 일 때와 일 때의 곡선을 같이 그리면 다음과 같다.


import numpy as np
import matplotlib.pyplot as plt
import math
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

u_range=np.linspace(0,math.pi*2,60)
v_range=np.linspace(-3,3,60)

U,V =np.meshgrid(u_range,v_range)

fig=plt.figure()
ax=fig.gca(projection='3d')
X=2*np.cos(U)
Y=V
Z=2*np.sin(U)

x1=2*np.cos(u_range)
y1=0*v_range+1
z1=2*np.sin(u_range)

x2=2*np.cos(u_range*0+1)
y2=v_range
z2=2*np.sin(u_range*0+1)

ax.plot_surface(X,Y,Z,rstride=1, cstride=1,cmap=cm.Blues)
ax.plot(x1,y1,z1,color="red")
ax.plot(x2,y2,z2,color="yellow")
# ax.set_zlim3d(0,1)

plt.show()

para

시어핀스키 삼각형

import random
import matplotlib.pyplot as plt

def transformation_1(p):
    x=p[0]
    y=p[1]
    x1=0.5*x
    y1=0.5*y
    return x1,y1

def transformation_2(p):
    x=p[0]
    y=p[1]
    x1 = 0.5 * x +0.5
    y1 = 0.5 * y + 0.5
    return x1,y1

def transformation_3(p):
    x=p[0]
    y=p[1]
    x1 = 0.5 * x + 1
    y1 = 0.5 * y
    return x1,y1

def get_index(probability):
    r=random.random()
    c_probability=0
    sum_probability=[]
    for p in probability:
        c_probability += p
        sum_probability.append(c_probability)
    for item,sp in enumerate(sum_probability):
        if r<=sp:
            return item
    return len(probability)-1

def transform(p):
    transformations=[transformation_1,transformation_2,transformation_3]
    probability=[1/3,1/3,1/3]
    tindex=get_index(probability)
    t=transformations[tindex]
    x,y=t(p)
    return x,y

def draw_fern(n):
    x=[0]
    y=[0]

    x1,y1=0,0
    for i in range(n):
        x1,y1=transform((x1,y1))
        x.append(x1)
        y.append(y1)
    return x,y

n=int(input("Enter the number of points:"))

x,y=draw_fern(n)
plt.plot(x,y,'o')
plt.title('{0} points'.format(n))
plt.show()

10000개의 점에 대하여 실행하면 다음과 같다.

sier