Python Turtle Workshop: How to draw ellipse and egg shape

How to draw ellipse

The ellipse shape could be viewed as four separated arcs. The two facing each other have the same radius while the two next to each other have different radius, shown at below.

The python code for above graph is as follows:

import turtle as t
t.pu()
t.goto(150, -60)
t.pd()
t.ht()
t.speed(0)
t.bgcolor(0,0,0)
t.seth(45)
t.pensize(10)
for n in range(2):
t.color(1,0,0)
t.circle(120,90)
t.color(0,0,1)
t.circle(250, 90)
t.done()

How to draw egg shape

An egg shape is made up of four arcs with 3 different radius, as shown in the graph below.

import turtle as t
import math
RADIUS = 150
OFFSET = 2 - math.sqrt(2) # offset is around 0.586
screen = t.Screen()
screen.setup(600,600, 0, 0)
t.speed(0)
t.pensize(10)
t.bgcolor(0,0,0)
t.ht()
t.up()
t.goto(-50,200)
t.down()
t.seth(180)
t.color(1,0,0)
t.circle(RADIUS,180)
t.color(0,0,1)
t.circle(2*RADIUS,45)
t.color(0,1,0)
t.circle(OFFSET * RADIUS,90)
t.color(0,0,1)
t.circle(2*RADIUS,45)
t.done()

Using math sin/cos method

The sin/cos method in math module could be used to draw ellipse shape or egg shape too. Any point on a ellipse/egg shape, it’s X cooridnate is mapped by math.cos method and Y cooridnate mapped by math.sin method. Shown in the graph below. For the angle α, the sine function gives the ratio of the length of the opposite side to the length of the hypotenuse.

To define the sine function of an acute angle α, start with a right triangle that contains an angle of measure α; in the accompanying figure, angle α in triangle ABC is the angle of interest. The three sides of the triangle are named as follows:

  • The opposite side is the side opposite to the angle of interest, in this case side a.
  • The hypotenuse is the side opposite the right angle, in this case side h. The hypotenuse is always the longest side of a right-angled triangle.
  • The adjacent side is the remaining side, in this case side b. It forms a side of (and is adjacent to) both the angle of interest (angle A) and the right angle.

Once such a triangle is chosen, the sine of the angle is equal to the length of the opposite side, divided by the length of the hypotenuse:

Mapped to a circle, the sin/cos could be displayed as follows:

To show the ellipse and circle angles in one graph:

Some common angles (θ) shown on the unit circle. The angles are given in degrees and radians, together with the corresponding intersection point on the unit circle, (cos(θ), sin(θ)).

Draw Ellipse with Math Function

import turtle as t
import math

def jump(pos):
t.pu()
t.goto(pos)
t.pd()

jump((100,0))
r1, r2= 100, 180
for n in range(361):
rad = math.pi / 180
sin = math.sin(n * rad)
cos = math.cos(n * rad)
x = r2 * cos
y = r1 * sin
    if n == 0:
jump((x,y))
else:
t.goto(x,y)

t.done()