turtle.shapetransform() function in Python

The turtle.shapetransform() function sets or returns the current transformation matrix of the turtle’s shape. Allows for advanced affine transformations (shear, scale, rotate).

Syntax: turtle.shapetransform(t11=None, t12=None, t21=None, t22=None)
Parameters: t11, t12, t21, t22 – numbers (elements of the transformation matrix).

Let us see an example to implement the turtle.shapetransform() function in Python:

Demo44.py

# turtle.shapetransform() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

# Apply transformation matrix to turtle shape
t.shapetransform(2, 0, 0, 1)  # Stretch width by 2, height by 1
t.forward(100)

window.exitonclick()

The following is the output:

turtle.shapetransform() function in Python

In the above code, we followed the below steps:

  • Import module: import turtle loads the turtle graphics library.
  • Create screen: window = turtle.Screen() opens the drawing window.
  • Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
  • Apply shape transform: t.shapetransform(2, 0, 0, 1) sets a 2×2 matrix [[2, 0], [0, 1]] on the turtle’s shape.
    • Effect: Horizontally stretches the shape by 2×, keeps vertical scale at 1×, with no skew.
    • Note: This changes only the shape’s appearance, not the turtle’s heading, speed, or movement.
  • Move forward: t.forward(100) moves 100 units in the current heading (drawing if the pen is down).
  • Close on click: window.exitonclick() waits for a mouse click, then closes the window.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.tilt() function in Python
turtle.shearfactor() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment