Python and Turtle Difficulty Level 5,math,python,Tutorial Tutorial: Rounding Any Corner with Python Turtle

Tutorial: Rounding Any Corner with Python Turtle

In a previous tutorial we explained how to round a rectangle with Python Turtle. In this tutorial, we are going to show you how to make any corner round. Knowledge in trigonometry will be very helpful understanding this tutorial.

We will try to round a 40 degree corner as shown in the following figure:

The first step is to decide where the rounding starts and where it ends. The closer the starting point to the corner, the smaller the rounded corner will become. Let’s choose distance 100 for this example and mark the starting and end points with red dots and the corner in green dot. The following is the code snippet for drawing to dots:

turtle.up()
turtle.goto(0,-300)
turtle.seth(65)
turtle.down()
turtle.fd(500)
turtle.dot(10,'red')
turtle.fd(100)
turtle.dot(10,'green')
turtle.left(140)
turtle.fd(100)
turtle.dot(10,'red')
turtle.fd(500)

The code should draw the following shape:

Because the angle of the corner is 40 degrees, we need to turn left 140 (180 – 40) degrees to draw the second line. Therefore, the arc we draw should also have 140 degrees of extent. The question is: What is the radius of the arc? The following figure will help us figure it out:

The distance between the blue dot and a red dot is the radius of the arc. The red, green, and blue dots form a right triangle. Since we know the distance between red dot to green dot (100), and the angle (20 degrees: half of 40 degrees) of the green dot, we can apply trigonometry to figure out the distance between the blue dot to a red dot: 100*math.tan(math.radians(20)). The following is the complete code for drawing a rounded corner:

import turtle
import math

turtle.setup(1000,1000)
turtle.title('Making Any Corner Round - PythonTurtle.Academy')
turtle.speed(0)
turtle.up()
turtle.hideturtle()

turtle.up()
turtle.goto(0,-300)
turtle.seth(65)
turtle.down()
turtle.fd(500)
turtle.circle(100*math.tan(math.radians(20)),140)
turtle.fd(500)

You can experiment with different angles and different length.

Related Post