In a previous tutorial, we explained how to draw an oval shape. The method starts drawing from an awkward position (the left end of the first arc). In this tutorial, we are going to show to how draw any oval shape by specifying the center of the oval. Let’s look at the picture (also drawn in Python Turtle) below:
The purple dot is where we started drawing the oval in the previous tutorial. Two red dots are the centers of two red arcs; two blue dots are the centers of two blue arcs. If we draw two lines connecting two red dots and two blue dots, they will intersect at the black point, which is the center of the oval.
The problem we are trying to solve is getting to the purple point from the black point. We could use mathematical formula to figure it out. Since, we have using Python Turtle, let’s try using Turtle to make this work. The following is the idea:
- Start from center black point, setheading to 90 degree and forward to the red point.
- Turn left 135 degrees to face toward the purple point and forward to the purple point.
Although we are using Turtle, we still have to solve one simple math problem: how much should Turtle forward from black to red point? Let’s look the square from at the center with two red dots and two blue dots. What is the side length of this square? It is radius of red arc minus the radius of blue arc. Knowing it, we need to divide its value by square root of 2, which is the value the Turtle should forward from black dot to red dot.
The following is the code snippet for the step above:
import math
import turtle
radius_red = 300
radius_blue = 200
turtle.up()
turtle.goto(0,0)
turtle.dot(15,'black')
turtle.seth(90)
turtle.down()
turtle.fd((radius_red-radius_blue)/math.sqrt(2))
turtle.dot(10,'red')
turtle.left(135)
turtle.fd(radius_red)
turtle.dot(15,'purple')
From this point on, you just need to set heading to -45 degree and draw four arcs. The following is the code snippet:
turtle.seth(-45)
turtle.circle(radius_red,90)
turtle.circle(radius_blue,90)
turtle.circle(radius_red,90)
turtle.circle(radius_blue,90)
Let’s generalize what we learned by making a draw_oval() function that allows us draw ovals with any center, any radius, and any tilt. The following is the complete code for this function:
import turtle
import math
screen = turtle.Screen()
screen.setup(1000,1000)
screen.title("Oval by Center - PythonTurtle.Academy")
turtle.speed(0)
turtle.hideturtle()
def draw_oval(x,y,radius_red,radius_blue,tilt):
turtle.up()
turtle.goto(x,y)
turtle.seth(90+tilt)
turtle.fd((radius_red-radius_blue)*math.sqrt(2)/2)
turtle.left(135)
turtle.fd(radius_red)
turtle.down()
turtle.seth(-45+tilt)
turtle.circle(radius_red,90,100)
turtle.circle(radius_blue,90,100)
turtle.circle(radius_red,90,100)
turtle.circle(radius_blue,90,100)
r1 = 500
r2 = 250
tilt = 0
for i in range(15):
draw_oval(0,0,r1,r2,tilt)
r1 *= 0.7
r2 *= 0.7
tilt += 45
It should draw the following shape: