您好,欢迎来到汇意旅游网。
搜索
您的当前位置:首页如何用turtle画椭圆?

如何用turtle画椭圆?

来源:汇意旅游网

问题:turtle工具中有circle(radius, extent=None, steps=None)方法画圆、圆弧、正多边形,但没有方法或函数能画椭圆,那椭圆该如何画呢?

思想:当正多边形的边数n趋于无穷大的时候,所画出来的图形即为圆形。同理,当扁的多边形的边数n趋于无穷大的时候,所画出来的图形即为椭圆。

一、中心在坐标原点的椭圆

已知椭圆的方程为 x 2 a 2 + y 2 b 2 \frac{x^2}{a^2}+\frac{y^2}{b^2} a2x2+b2y2,则椭圆的参数方程为: x = a cos ⁡ θ , y = b sin ⁡ θ , 0 ≤ θ ≤ 2 π x=a\cos \theta ,y=b\sin \theta ,0\le \theta \le 2\pi x=acosθy=bsinθ0θ2π,其实现程序如下:

import turtle, math
p = turtle.Turtle()


def ellipse(a, b, n=500):
    """
    绘制椭圆函数
    :param a: 长半轴长度
    :param b: 短半轴长度
    :param n: 边的数目 -- n越大,越趋近于椭圆
    :return:
    """
    p.penup()
    p.setpos(a, 0)  # 初始点的位置
    p.pendown()
    for i in range(n):  # 画扁的n边形。当n --> 无穷大,所画出的图形即为椭圆
        radian = 2 * math.pi / n  # 将2pi弧度分成n份,每份为radian
        theta = (i+1)*radian  # 每次弧度增加radian
        next_point = (a*math.cos(theta), b*math.sin(theta))
        p.setpos(next_point)

ellipse(200, 100)
p.hideturtle()
turtle.mainloop()

绘制效果:

假设椭圆绕着x正轴方向旋转 α \alpha α角度,则椭圆的参数方程为:

x = a cos ⁡ θ cos ⁡ α − b sin ⁡ θ sin ⁡ α , y = a cos ⁡ θ sin ⁡ α + b sin ⁡ θ cos ⁡ α , 0 ≤ θ 、 α ≤ 2 π x=a\cos \theta\cos \alpha -b\sin \theta \sin \alpha ,y=a\cos \theta\sin \alpha + b\sin \theta \cos \alpha ,0\le \theta、\alpha \le 2\pi x=acosθcosαbsinθsinαy=acosθsinα+bsinθcosα0θα2π

任意左右上下移动的参数方程为: x = a cos ⁡ θ cos ⁡ α − b sin ⁡ θ sin ⁡ α + r i g h t , y = a cos ⁡ θ sin ⁡ α + b sin ⁡ θ cos ⁡ α + u p , 0 ≤ θ 、 α ≤ 2 π x=a\cos \theta\cos \alpha -b\sin \theta \sin \alpha +right ,y=a\cos \theta\sin \alpha + b\sin \theta \cos \alpha+up ,0\le \theta、\alpha \le 2\pi x=acosθcosαbsinθsinα+righty=acosθsinα+bsinθcosα+up0θα2π。当 r i g h t > 0 right>0 right>0时,实际往右移动;当 r i g h t < 0 right<0 right<0时,实际往左移动。当 u p > 0 up>0 up>0时,实际往上移动;当 u p < 0 up<0 up<0时,实际往下移动。

其实现程序如下:

import turtle, math
p = turtle.Turtle()

def new_ellipse(a, b, n=500, right=0, up=0, alpha=360):
    """
    将椭圆旋转和左右移动
    :param a:长半轴长度
    :param b:短半轴长度
    :param n:边的数目 -- n越大,越趋近于椭圆
    :param right:椭圆往右移动的距离 -- 大于0:实际往右移动,小于0:实际往左移动
    :param up:椭圆往上移动的距离 -- 大于0:实际往上移动,小于0:实际往下移动
    :param alpha:椭圆旋转的角度
    :return:
    """
    alpha = (2*math.pi/360)*alpha  # 计算旋转的角度对应的弧度值
    theta = 0
    start_point = (a * math.cos(theta)*math.cos(alpha) - b * math.sin(theta)*math.sin(alpha) + right,
                   a * math.cos(theta)*math.sin(alpha) + b * math.sin(theta)*math.cos(alpha) + up)
    p.penup()
    p.setpos(start_point)  # 初始点的位置
    p.pendown()
    for i in range(n):
        radian = 2 * math.pi / n  # 将2pi弧度分成n份,每份为radian
        theta = (i + 1) * radian  # 每次弧度增加radian
        next_point = (a * math.cos(theta)*math.cos(alpha) - b * math.sin(theta)*math.sin(alpha) + right,
                      a * math.cos(theta)*math.sin(alpha) + b * math.sin(theta)*math.cos(alpha) + up)
        p.setpos(next_point)

new_ellipse(200, 100, alpha=45)
new_ellipse(200, 100, up=-150, alpha=90)
new_ellipse(200, 100, right=100, up=100, alpha=120)
p.hideturtle()
turtle.mainloop()

绘制效果:

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- hids.cn 版权所有 赣ICP备2024042780号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务