以极坐标的中心点为基础找直角坐标来画多边形

该博客介绍了如何利用matplotlib在Python中通过极坐标转换为直角坐标来绘制多边形。首先导入matplotlib库,并设定坐标轴范围。然后,定义一个函数`huitu(a)`,其中参数`a`代表多边形的边数。通过循环计算每个角度对应的直角坐标点,存储到x和y列表中,最后使用matplotlib的plot函数绘制图形。博客还提到了使用cmath库进行坐标转换的可能性。

以极坐标的中心点为基础找直角坐标来画多边形

import matplotlib.pyplot as plt


def huitu(a):
    fig = plt.figure()
    plt.xlim(-10, 10)
    plt.ylim(-10, 10)
    
    r = 9
    angle = 360 // a
    initial = 0
    x = []
    y = []
    while initial <= 360:
        x.append(round(r*math.cos(math.radians(initial))))
        y.append(round(r*math.sin(math.radians(initial))))
        initial += angle
    plt.plot(x, y)
    plt.show()

分析

1、plt的使用

  • 方法一:
  • import matplotlib.pyplot as plt
  • 方法二:
    from networkx.drawing.tests.test_pylab import plt
    import matplotlib
    dmatplotlib.use('TkAgg')

2、以极坐标的原点发散来找多边形的直角坐标

极坐标转换为直角坐标
x = r * cos(度数)
y = r * sin(度数)

这里转换需注意:
❌ x = r * math.cos(度数)
❌ x = r * math.sin(度数)

✅ (rmath.cos(math.radians(initial)))
✅ (r
math.sin(math.radians(initial)))

最后存入x,y列表中,还需要将上面算出来的数据四舍五入,否则画出来的图会因为数据误差而无法紧密连接
如:
在这里插入图片描述
使用round四舍五入后
如:
在这里插入图片描述

turtle

import turtle

t = turtle.Pen()
def draw(n):
    for i in range(n):
        t.forward(100)
        t.left(360/n)

draw(5)
turtle.exitonclick()

在这里插入图片描述

用cmath这个库来画多边形可以吗😭(极坐标转直角坐标)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Taverry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值