Python Turtle Vertical Oval

102 Views Asked by At

I am playing around with Python's turtle module.

I want to draw a vertical oval, half of which is crossed by the y axis, when the user enters only the radius of a small arc (you can try 120).

Here is an approximate diagram of what it should look like.

Here is the code snippet of how I usually draw an ellipse (you can draw a large arc first, then a small one).:

import turtle

r=int(input())  # smaller radius
t=turtle.Turtle()
for i in range(2):
    t.circle(r, 90)
    t.circle(r*2, 90)
turtle.done()
2

There are 2 best solutions below

1
pythoneer On

This code should work:

import turtle

r = 120

t = turtle.Turtle()

t.penup()
t.goto(r - r / 1.5, 0)
t.pendown()

t.left(45)
for i in range(2):
    t.circle(r, 90)
    t.circle(r / 2, 90)

I modified your original code so that there's a horizontal offset that centers the oval to x = 0. It also rotates the turtle before drawing, so that the oval gets rotated by the same amount. It doesn't use any extra libraries either.

0
cdlane On

Here's my solution using your oringinal ellipse algorithm, plus the angle fix of @Corralien (+1). It's overly complicated code as it draws the y-axis at the same time and centers the ellipse on the window:

from turtle import Screen, Turtle

small_radius = int(input())
small_radius_versine = small_radius * (1 - 2**0.5/2)
small_radius_chord_length = small_radius * 2**0.5

large_radius = 2 * small_radius
large_radius_chord_length = large_radius * 2**0.5

distance_to_center = large_radius_chord_length/2 + small_radius_versine

turtle = Turtle()
turtle.penup()
turtle.goto(-small_radius_chord_length/2, small_radius_versine - distance_to_center)
turtle.pendown()

turtle.setheading(-45)

for _ in range(2):
    turtle.circle(small_radius, 45)

    turtle.left(90)
    turtle.forward(distance_to_center)
    turtle.backward(distance_to_center)
    turtle.right(90)

    turtle.circle(small_radius, 45)
    turtle.circle(large_radius, 90)

turtle.hideturtle()

screen = Screen()
screen.exitonclick()

enter image description here