Python中的 turtle.shapetransform()函数
turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 Tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。
海龟.shapetransform()
该函数用于设置或返回海龟形状的当前变换矩阵。如果没有给出任何矩阵元素,则返回变换矩阵。否则,设置给定的元素并根据由第一行 t11、t12 和第二行 t21、22 组成的矩阵变换海龟形状。
Syntax : turtle.shapetransform(t11=None, t12=None, t21=None, t22=None)
Parameters:
t11, t12, t21, t22 (optional): The determinant t11 * t22 – t12 * t21 must not be zero, otherwise an error is raised.
以下是上述方法的实现以及一些示例:
示例 1:
Python3
# import package
import turtle
# check the default value
print(turtle.shapetransform())
Python3
# import package
import turtle
# change shapetransform to 2,0,2,0
# as determinant of the matrix
# [[2,0],[0,2]] is 0.
# so, raised an error
turtle.shapetransform(2,0,2,0)
Python3
# import package
import turtle
# loop for pattern
for i in range(5):
for j in range(10):
# motion
turtle.forward(5+5*(i+j))
turtle.left(45)
# transform the shape
turtle.shapetransform(i+1,0,0,i+1)
输出 :
(1.0, 0.0, 0.0, 1.0)
示例 2:
Python3
# import package
import turtle
# change shapetransform to 2,0,2,0
# as determinant of the matrix
# [[2,0],[0,2]] is 0.
# so, raised an error
turtle.shapetransform(2,0,2,0)
输出 :
turtle.TurtleGraphicsError: Bad shape transform matrix: must not be singular
示例 3:
Python3
# import package
import turtle
# loop for pattern
for i in range(5):
for j in range(10):
# motion
turtle.forward(5+5*(i+j))
turtle.left(45)
# transform the shape
turtle.shapetransform(i+1,0,0,i+1)
输出 :