Python and Turtle Difficulty Level 3,math,python,Tutorial Tutorial: How to Draw a Simple Heart Shape with Python and Turtle

Tutorial: How to Draw a Simple Heart Shape with Python and Turtle

Simple Heart Shape with 4 Segments

The figure above shows, we can draw a simple heart shape with 4 segments: 2 lines and 2 arcs. We can continuously draw these 4 segments without lifting up the pen.

We start from the bottom tip of the heart. The heading for the blue line is 45 degrees. The second segment is 225 degree arc. After initial 45 degree heading and 225 degree turn, the heading of the turtle will be a 45+225=270 degrees, which is facing down perfectly. The rest of the two segments are symmetric to the first two. All we need to do is just turn the Turtle by 180 degrees before drawing the 3rd segment.

The next step is to figure out the ratio of blue segment and the radius of the arc. In the figure above, a triangle formed by two black lines and the blue line is a right triangle. The angle between two black lines is (360-225)/2 = 67.5 degrees, where 225 is the degree of the arc. Therefore, the ratio of blue line segment and the radius equals to tangent(67.5).

Here is the code:

import turtle
import math

turtle.color('red')
d = 800
r = d/math.tan(math.radians(67.5))
turtle.up()
turtle.goto(0,-600)
turtle.seth(45)
turtle.down()
turtle.fd(d)
turtle.circle(r,225)
turtle.left(180)
turtle.circle(r,225)
turtle.fd(d)

You can easily draw a filled heart by calling begin_fill() and end_fill() functions:

turtle.color('red')
d = 800
r = d/math.tan(math.radians(67.5))
turtle.up()
turtle.goto(0,-600)
turtle.seth(45)
turtle.begin_fill()
turtle.down()
turtle.fd(d)
turtle.circle(r,225)
turtle.left(180)
turtle.circle(r,225)
turtle.fd(d)
turtle.end_fill()

Related Projects:

Related Post