赞
踩
偶尔从翻到的一个github我实现了下感觉效果超漂亮,分享给大家!
成品图:
绘制过程:
代码:
import turtle as te import time WriteStep = 15 # 贝塞尔函数的取样次数 Speed = 5 Width = 600 # 界面宽度 Height = 500 # 界面高度 Xh = 0 # 记录前一个贝塞尔函数的手柄 Yh = 0 def Bezier(p1, p2, t): # 一阶贝塞尔函数 return p1 * (1 - t) + p2 * t def Bezier_2(x1, y1, x2, y2, x3, y3): # 二阶贝塞尔函数 te.goto(x1, y1) te.pendown() for t in range(0, WriteStep + 1): x = Bezier(Bezier(x1, x2, t / WriteStep), Bezier(x2, x3, t / WriteStep), t / WriteStep) y = Bezier(Bezier(y1, y2, t / WriteStep), Bezier(y2, y3, t / WriteStep), t / WriteStep) te.goto(x, y) te.penup() def Bezier_3(x1, y1, x2, y2, x3, y3, x4, y4): # 三阶贝塞尔函数 x1 = -Width / 2 + x1 y1 = Height / 2 - y1 x2 = -Width / 2 + x2 y2 = Height / 2 - y2 x3 = -Width / 2 + x3 y3 = Height / 2 - y3 x4 = -Width / 2 + x4 y4 = Height / 2 - y4 # 坐标变换 te.goto(x1, y1) te.pendown() for t in range(0, WriteStep + 1): x = Bezier(Bezier(Bezier(x1, x2, t / WriteStep), Bezier(x2, x3, t / WriteStep), t / WriteStep), Bezier(Bezier(x2, x3, t / WriteStep), Bezier(x3, x4, t / WriteStep), t / WriteStep), t / WriteStep) y = Bezier(Bezier(Bezier(y1, y2, t / WriteStep), Bezier(y2, y3, t / WriteStep), t / WriteStep), Bezier(Bezier(y2, y3, t / WriteStep), Bezier(y3, y4, t / WriteStep), t / WriteStep), t / WriteStep) te.goto(x, y) te.penup() def Moveto(x, y): # 移动到svg坐标下(x,y) te.penup() te.goto(-Width / 2 + x, Height / 2 - y) def line(x1, y1, x2, y2): # 连接svg坐标下两点 te.penup() te.goto(-Width / 2 + x1, Height / 2 - y1) te.pendown() te.goto(-Width / 2 + x2, Height / 2 - y2) te.penup() def lineto(dx, dy): # 连接当前点和相对坐标(dx,dy)的点 te.pendown() te.goto(te.xcor() + dx, te.ycor() - dy) te.penup() def Lineto(x, y): # 连接当前点和svg坐标下(x,y) te.pendown() te.goto(-Width / 2 + x, Height / 2 - y) te.penup() def Horizontal(x): # 做到svg坐标下横坐标为x的水平线 te.pendown() te.setx(x - Width / 2) te.penup() def horizontal(dx): # 做到相对横坐标为dx的水平线 te.seth(0) te.pendown() te.fd(dx) te.penup() def vertical(dy): # 做到相对纵坐标为dy的垂直线 te.seth(-90) te.pendown() te.fd(dy) te.penup() te.seth(0) def polyline(x1, y1, x2, y2, x3, y3): # 做svg坐标下的折线 te.penup() te.goto(-Width / 2 + x1, Height / 2 - y1) te.pendown() te.goto(-Width / 2 + x2, Height / 2 - y2) te.goto(-Width / 2 + x3, Height / 2 - y3) te.penup() def Curveto(x1, y1, x2, y2, x, y): # 三阶贝塞尔曲线到(x,y) te.penup() X_now = te.xcor() + Width / 2 Y_now = Height / 2 - te.ycor() Bezier_3(X_now, Y_now, x1, y1, x2, y2, x, y) global Xh global Yh Xh = x - x2 Yh = y - y2 def curveto_r(x1, y1, x2, y2, x, y): # 三阶贝塞尔曲线到相对坐标(x,y) te.penup() X_now = te.xcor() + Width / 2 Y_now = Height / 2 - te.ycor() Bezier_3(X_now, Y_now, X_now + x1, Y_now + y1, X_now + x2, Y_now + y2, X_now + x, Y_now + y) global Xh global Yh Xh = x - x2 Yh = y - y2 def Smooth(x2, y2, x, y): # 平滑三阶贝塞尔曲线到(x,y) global Xh global Yh te.penup() X_now = te.xcor() + Width / 2 Y_now = Height / 2 - te.ycor() Bezier_3(X_now, Y_now, X_now + Xh, Y_now + Yh, x2, y2, x, y) Xh = x - x2 Yh = y - y2 def smooth_r(x2, y2, x, y): # 平滑三阶贝塞尔曲线到相对坐标(x,y) global Xh global Yh te.penup() X_now = te.xcor() + Width / 2 Y_now = Height / 2 - te.ycor() Bezier_3(X_now, Y_now, X_now + Xh, Y_now + Yh, X_now + x2, Y_now + y2, X_now + x, Y_now + y) Xh = x - x2 Yh = y - y2 te.tracer(10) te.setup(Width, Height, 0, 0) te.pensize(1) te.speed(Speed) te.penup() # 图层_2 time.sleep(20) te.color("black", "#F2F2F2") # 外套 Moveto(61, 462) te.begin_fill() smooth_r(12, -41, 27, -58) curveto_r(-6, -36, 6, -118, 9, -132) curveto_r(-15, -27, -23
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。