Unexpected results drawing ellipse using Python Turtle Module -
i'm trying draw ellipse using turtle module in python, plan follow:
- let starting point focal point of ellipse
- set initial theta value 0
- let turtle forward, let distance of forwarding a*(1-ee)/(1-emath.cos(theta))
- let turn around , original spot
- make small turn, update theta value
- repeat above process
here's actual code:
import turtle import math wn = turtle.getscreen() wn.bgcolor("red") my_turtle = turtle.turtle() my_turtle.penup() my_turtle.speed(9) i=0 j=0 a=200 e=0.5 x_0 = 20 theta = 0 while(i<5000): #plotting squares my_turtle.penup() ellipse = a*(1-e*e)/(1-e*math.cos(theta)) my_turtle.forward(ellipse) my_turtle.pendown() my_turtle.forward(1) my_turtle.left(180) my_turtle.penup() my_turtle.forward(ellipse+1)
however, results off this:(not complete image can see it's off)
can explain me wrong ? thank much!
i'm used drawing ellipses center, not 1 focal point read on ellipse math head around this. key formula appears correct:
ellipse = a*(1-e*e)/(1-e*math.cos(theta))
the issue how drawing. first need add setheading()
point turtle in correct direction. (remember default it's in degrees need either convert or change turtle's default). second, how bridge between steps in drawing isn't sufficient.
i've reworked code below, , have compared center-based solution confirm generates same ellipse:
import math turtle import turtle, screen my_screen = screen() my_turtle = turtle(visible=false) my_turtle.speed('fastest') my_turtle.radians() my_turtle.penup() e = 0.5 # linear eccentricity = 200 # semi major axis c = e * # distance center focal point my_turtle.goto(-c, 0) # starting @ focal point there = (2 * c, 0) # initialize previous point visited step = 0.1 theta = step # @ starting point, calculate next point while theta < math.pi * 2 + step: # overshoot close curve # draw ellipse 1 focus point my_turtle.setheading(theta) distance = * (1 - e * e) / (1 - e * math.cos(theta)) my_turtle.forward(distance) here = my_turtle.position() my_turtle.pendown() my_turtle.goto(there) # draw line last position 1 my_turtle.penup() # comment out demonstrate algorithm my_turtle.goto(-c, 0) # return focal point there = here theta += step my_screen.exitonclick()
output
i left pen down illustrution it's obvious it's forming ellipse 1 focal point.
Comments
Post a Comment