你如何在海龟图形(python)中绘制椭圆/椭圆形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29465666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:37:18  来源:igfitidea点击:

How do you draw an ellipse/oval in turtle graphics (python)?

pythonturtle-graphics

提问by Tristan

How do you draw an ellipse/oval in turtle graphics (python)? I want to be able to draw an ellipse and part of an ellipse using the circle() function or similar. I can stamp one using #turtlesize(stretch_wid=None, stretch_len=10, outline=None). But I don't want it to be color filled.

你如何在海龟图形(python)中绘制椭圆/椭圆形?我希望能够使用 circle() 函数或类似函数绘制椭圆和椭圆的一部分。我可以使用 #turtlesize(stretch_wid=None,stretch_len=10,outline=None) 标记一个。但我不希望它被颜色填充。

采纳答案by avinash pandey

You can use shapesize() function of turtle to make an ellipse.

您可以使用乌龟的 shapesize() 函数来制作椭圆。

shape("circle")
shapesize(5,4,1)
fillcolor("white")

回答by Hukmaram

#you can create circle or ellipse by changing value of parameter of oval=canvas.create_oval(10,15,200,150)
from Tkinter import *
top=Tk()`
canvas=Canvas(top,width=200,height=200)
canvas.pack()
oval=canvas.create_oval(10,15,200,150)
top.mainloop()

回答by Ollie

I made my own function for drawing ovals that I personally think is very useful:

我制作了自己的绘制椭圆的函数,我个人认为它非常有用:

def talloval(r):               # Verticle Oval
    turtle.left(45)
    for loop in range(2):      # Draws 2 halves of ellipse
        turtle.circle(r,90)    # Long curved part
        turtle.circle(r/2,90)  # Short curved part

def flatoval(r):               # Horizontal Oval
    turtle.right(45)
    for loop in range(2):
        turtle.circle(r,90)
        turtle.circle(r/2,90)

The ris the radius of the circle and it controls how big the ellipse is. The reason for the turn left/right is because without it, the ellipse is diagonal.

r是圆的半径和它控制椭圆有多大。左转/右转的原因是因为没有它,椭圆是对角线。

回答by Devansu Yadav

We can make an ellipse using its parametric equation in Turtle module. The code below might be a bit long but using this we can draw the ellipse in any orientation as required.You can edit it according to the requirement.We are basically changing the parametric angle of ellipse and plotting the curve.

我们可以使用 Turtle 模块中的参数方程制作椭圆。下面的代码可能有点长,但是使用它我们可以根据需要绘制任何方向的椭圆。您可以根据需要进行编辑。我们基本上是改变椭圆的参数角度并绘制曲线。

  import turtle
  import math
  def ellipse(a, b, h=None, k=None, angle=None, angle_unit=None):
     myturtle = turtle.Turtle()
     if h is None:
       h = 0
     if k is None:
       k = 0
    # Angle unit can be degree or radian
     if angle is None:
       angle = 360
       converted_angle = angle*0.875
     if angle_unit == 'd' or angle_unit is None:
       converted_angle = angle * 0.875
     # We are multiplying by 0.875 because for making a complete ellipse we are plotting 315 pts according
     # to our parametric angle value
     elif angle_unit == "r":
       converted_angle = (angle * 0.875 * (180/math.pi))
     # Converting radian to degrees.
    for i in range(int(converted_angle)+1):
       if i == 0:
         myturtle.up()
       else:
         myturtle.down()
       myturtle.setposition(h+a*math.cos(i/50), k+b*math.sin(i/50))
   turtle.done()