3次ベジェ曲線を描く

2018/12/21

Python 2.7.8

特に関数はないので自前で描く。3次のベジエ曲線。

# -*- coding: utf-8 -*-

import Tkinter

window = Tkinter.Tk()
canvas = Tkinter.Canvas(window,width=300,height=200)
canvas.pack()

p0_x,p0_y = [10,10]
p1_x,p1_y = [100,180]
p2_x,p2_y = [200,10]
p3_x,p3_y = [290,190]

ls = []
n = 20
for i in range(n+1):
    t = i/float(n)
    p_x = (1-t)*(1-t)*(1-t)*p0_x + 3*(1-t)*(1-t)*t*p1_x + 3*(1-t)*t*t*p2_x + t*t*t*p3_x;
    p_y = (1-t)*(1-t)*(1-t)*p0_y + 3*(1-t)*(1-t)*t*p1_y + 3*(1-t)*t*t*p2_y + t*t*t*p3_y;
    ls.append([p_x,p_y])

canvas.create_line(ls,fill='red')
canvas.create_line(p0_x,p0_y,p1_x,p1_y,p2_x,p2_y,p3_x,p3_y)

window.mainloop()

関数化 ``` def bezier_curve(p0,p1,p2,p3,n=20): points = [] for i in range(n+1): t = i/float(n) x = (1-t)**3*p0[0]+3*(1-t)**2*t*p1[0]+3*(1-t)*t**2*p2[0]+t**3*p3[0] y = (1-t)**3*p0[1]+3*(1-t)**2*t*p1[1]+3*(1-t)*t**2*p2[1]+t**3*p3[1] points.append([x,y]) return points ```

ベジエ曲線を使ってみよう
https://qiita.com/hart_edsf/items/cec5af01a70b62ca93f2