Python Turtle – Morphing Algorithm

Tweening/Morphing effects are often used in Computer Animations to change the shape of an object by morphing an object from one shape into another.

In tweening, key frames are provided and “in-between” frames are calculated to make a smooth looking animation.

In this blog post we will implement a tweening algorithm to morph a letter of the alphabet (e.g. “A” into another letter “Z”) using a linear interpolation.

We will consider a letter as being a list of connected nodes (dots) where each dot has its own set of (x,y) coordinates.

character-A-dot-to-dot

Linear Interpolation Formulas:


tweening-linear-interpolation

Using the following linear interpolation formulas we can calculate the (x,y) coordinates of each dot for any “in-between” frame.

x(t) = xA + (xZ – xA) * t / 10
y(t) = yA + (yZ – yA) * t / 10
  • “t” represents the time: in other words the frame number (e.g. between 0 and 10)
  • (xA,yA) the coordinates of a node/dot from the starting letter (e.g. A)
  • (xZ,yZ) the coordinates of a node/dot from the ending letter (e.g. Z)

Using these formulas we can see that:

x(0) = xA
y(0) = yA
x(10) = xZ
y(10) = yZ

Our tweening algorithm will need to apply these formulas to each node of the letter.

Python Code (using Python Turtle)

Your Task #1


Tweak this code to create a tweening animation going through all the letters of your firstname.
(e.g. J -> A -> M -> E -> S).

Your Task #2


Tweak this code to create an animated count down timer counting from 9 to 0.
(e.g. 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0).
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 8

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!