turtle python如何从坐标开始绘制椭圆

问题描述 投票:0回答:1

我无法绘制图中所示的椭圆形。问题是椭圆的一半应该正好位于点 (200, -200) 处。当我画一个椭圆形时,它的一半错过了点(200,-200)。但这是主要条件:程序仅引入了较小的椭圆半径(在本例中为120)。有谁知道如何做到这一点,也许您需要在绘制之前将其移动到某个位置?如何在不使用其他工具的情况下做到这一点图书馆? 输入字体大小、颜色、小圆弧的半径和椭圆的颜色。

shema

示例:

60

午夜蓝

120

草坪绿

结果:

expected result

我的代码:

import turtle
fontsize = int(input())  # size of font (60)
maincol = input()  # color of text and  axis (MidnightBlue)
r = int(input())  # smaller radius of oval (120)
ovalcol = input()  # color of oval (LawnGreen)
st = turtle.Turtle()  # name of turtle
st.pu()
st.goto(-300, 150)
st.color(maincol)
s1 = '\N{GREEK SMALL LETTER THETA}'
s2 = '='
s3 = '\N{GREEK SMALL LETTER OMEGA}'
s4 = 't'
s5 = '+'
s6 = '\N{VULGAR FRACTION ONE HALF}'
s7 = '\N{GREEK SMALL LETTER EPSILON}'
s8 = 't'
s9 = '\N{SUPERSCRIPT TWO}'
res = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9  # all symbols in one formula
st.write(res, move=False, align='left', font=('Times New Roman', 
fontsize, 'normal'))
st.pu()
st.goto(200, -200)  # here is the half of the vertical oval
st.color(ovalcol)
st.pd()
st.seth(45)  # I don't know what the angle should be here
st.begin_fill()
for i in range(2):
    st.circle(r * 2, 90)  # drawing a larger arc
    st.circle(r, 90)  # drawing a smaller arc
st.end_fill()
st.pu()
st.goto(200, -200 - r / 4)  # drawing an axis
st.color(maincol)
st.pd()
st.seth(90)
st.fd(r * 4)  # as you can see, the axis does not divide the oval in half and this is the whole problem
st.hideturtle()
turtle.done()

我的结果: unexpected result

我尝试从不同的角画椭圆形,但这对我没有帮助

python geometry turtle-graphics python-turtle ellipse
1个回答
0
投票

这是使用冲压而不是绘图的替代方法。它简化了逻辑,但不会产生好看的椭圆:

from turtle import Screen, Turtle

CURSOR_SIZE = 20

s1 = '\N{GREEK SMALL LETTER THETA}'
s3 = '\N{GREEK SMALL LETTER OMEGA}'
s6 = '\N{VULGAR FRACTION ONE HALF}'
s7 = '\N{GREEK SMALL LETTER EPSILON}'
s9 = '\N{SUPERSCRIPT TWO}'

res = f"{s1}={s3}t+{s6}{s7}t{s9}" # all symbols in one formula

FONT_SIZE = int(input())  # size of font (60)
MAIN_COLOR = input()  # color of text and  axis (MidnightBlue)
RADIUS = int(input())  # smaller RADIUS of oval (120)
OVAL_COLOR = input()  # color of oval (LawnGreen)

screen = Screen()

turtle = Turtle()
turtle.hideturtle()
turtle.penup()

turtle.goto(-300, 150)
turtle.color(MAIN_COLOR)
turtle.write(res, align='left', font=('Times New Roman', FONT_SIZE, 'normal'))

turtle.goto(200, 0)
turtle.color(OVAL_COLOR)
turtle.shape('circle')
turtle.shapesize(stretch_len=RADIUS*2/CURSOR_SIZE, stretch_wid=RADIUS*4/CURSOR_SIZE)
turtle.stamp()

turtle.sety(-RADIUS*2.25)  # drawing an axis
turtle.color(MAIN_COLOR)
turtle.setheading(90)
turtle.pendown()
turtle.forward(RADIUS*4.5)

screen.exitonclick()

这里我们从中心绘制椭圆,基本上是将圆拟合成矩形:

© www.soinside.com 2019 - 2024. All rights reserved.