椭圆(turtle)

在这里插入图片描述

from turtle import *

penup()
goto(0, -100)
pendown()

y = 100
list1 = ['red', 'blue']
for k in list1:
    color(k)
    begin_fill()
    circle(y)
    penup()
    goto(0, -85)
    pendown()
    y -= 15
    end_fill()
hideturtle()

penup()
goto(0, 70)
pendown()
color('gold', 'orange')
begin_fill()
goto(-40, -60)
goto(0, -20)
goto(40, -60)
goto(0, 70)
end_fill()

penup()
goto(-20, -60)
write("CASC", font=("English", 15, "normal"))


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(150, 60, right=1, up=1, alpha=150)
p.hideturtle()
turtle.mainloop()

椭圆1

仅仅以幕布为中心 仅可修改a b

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(100, 50)
p.hideturtle()
turtle.mainloop()


椭圆

可修改位置和角度

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(100, 50, right=100, up=100)
p.hideturtle()
turtle.mainloop()

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值