Drawing General Ellipse with Python Turtle

We drew an ellipse centered at origin without tilt. In this project, we will draw a general ellipse that can be centered at any location and any tilt angle. The parametric equation for this general ellipse is as follows:

x = cx + a*math.cos(t)*math.cos(math.radians(angle))-b*math.sin(t)*math.sin(math.radians(angle))
y = cy + a*math.cos(t)*math.sin(math.radians(angle))+b*math.sin(t)*math.cos(math.radians(angle))

(cx, cy) is the coordinate of the center of the ellipse. angle is the tilt angle of the ellipse. Range parameter t from 0 to 2π gradually to draw general ellipses.

General Ellipses with Python Turtle

Solution:

import turtle
import math
screen = turtle.Screen()
screen.setup(1000,1000)
screen.title("General Ellipse with Parametric Equation - PythonTurtle.Academy")
screen.tracer(0,0)

turtle.speed(0)
turtle.hideturtle()
turtle.up()

n = 2000
# (cx,cy): center of ellipse, a: width, b:height, angle: tilt
def ellipse(cx,cy,a,b,angle):
    # draw the first point with pen up
    t = 0
    x = cx + a*math.cos(t)*math.cos(math.radians(angle))-b*math.sin(t)*math.sin(math.radians(angle))
    y = cy + a*math.cos(t)*math.sin(math.radians(angle))+b*math.sin(t)*math.cos(math.radians(angle))
    turtle.up()
    turtle.goto(x,y)
    turtle.down()
    turtle.color('red')
    # draw the rest with pen down
    for i in range(n):
        x = cx + a*math.cos(t)*math.cos(math.radians(angle))-b*math.sin(t)*math.sin(math.radians(angle))
        y = cy + a*math.cos(t)*math.sin(math.radians(angle))+b*math.sin(t)*math.cos(math.radians(angle))
        turtle.goto(x,y)    
        t += 2*math.pi/n

ellipse(-200,-100,200,100,45)
ellipse(200,100,40,200,80)
turtle.update()

Drawing Ellipse with Parametric Equation in Python Turtle

We have several projects drawing ovals. Ellipse is not the same oval. An ellipse is a curve surrounding two focal points such that the sum of the distances to the two focal points is constant for every point on the curve. Ellipse is a generalization of a circle (where two focal points at the same location). The parametric equations for an ellipse is as follows:

x = a*math.cos(t)
y = b*math.sin(t)

The equations are very similar to those of circle except that instead of one single radius, we have two: a, b. Use the equations to draw the following ellipse:

Ellipse with Python Turtle

Drawing Circle with Parametric Equation

Although there is built in function to draw a circle in Turtle, let’s draw circles in a different way with parametric equation. The parametric equations for a circle is very simple:

x = radius*math.cos(t)
y = radius*math.sin(t)

In the equations above t is the parameter, x, y are the coordinates of the points on the circle. Let t start from 0 and go up to 2π gradually.

Drawing Circle with Parametric Equation with Python Turtle

Lissajous Curve with Python Turtle

Lissajous Curve is a famous curve with both practical use and in art and design. It is generated by a simple parametric equation:

x = 300*math.cos(k1*t)
y = 300*math.sin(k2*t)

In the equation above t is the parameter. Let it range gradually from 0 to 4π. Set k1 to 3 and k2 to 2 and drawing the following shape. You can play with other numbers for k1 and k2 to see different curves.

Lissajous Curve with Python Turtle

Butterfly Curve with Python Turtle

Use the following parametric equations to draw a butterfly curve.

x = 100*(math.sin(t)*(math.exp(math.cos(t))-2*math.cos(4*t)-math.sin(t/12)**5))
y = 100*(math.cos(t)*(math.exp(math.cos(t))-2*math.cos(4*t)-math.sin(t/12)**5))

In the equation above t is the parameter of the equations. Let it range from 0 to 12π gradually with small increment.

Butterfly Curve with Python Turtle

Heart with Parametric Equation and Python Turtle

Use the following parametric equations to draw the following filled heart curve.

    x = 10*(16*math.sin(t)**3)
    y = 10*(13*math.cos(t)-5*math.cos(2*t)-2*math.cos(3*t)-math.cos(4*t))

The variable t is the parameter for the curve. Let t range from 0 to 2π with small increment.

Heart Curve with Python Turtle

If you want to draw a simpler heart refer to this tutorial on drawing a simple heart.

Drawing Parametric Curve with Python Turtle

Parametric equations provides a convenient way to draw curves. We will use the following parametric equation in this project.

x = 200*(math.cos(a*t) - math.cos(b*t)**j)
y = 200*(math.sin(c*t) - math.sin(d*t)**k)

In the equations above, t is the parameter. Let is range from 0 to 2π with very small increment. Use different values for a,b,c,d,j,k to draw the following shapes (you can see the their values in the title of the pictures):

4,1,4,1,3,3
80,1,1,80,3,3
80,1,80,1,3,3
9,199,200,9,3,4