绘制几个月饼(matplotlb)

中秋节到了画月饼。月饼由折线组成的,一共有5条折线组成的,用matplotlib绘制的

import matplotlib.pyplot as plt
import numpy as np
import math

R = 50.0
r = 30.0


def drawMoonCake(plt,cx = 0.0,cy = 0.0,R = 50.0,r=30.0,h=20.0,segn = 36):
    dsi = math.pi / segn

    xv = []
    yv = []
    

    for i in range(segn+1):
        si = i*dsi
        x = R*math.cos(si)
        y = r*math.sin(si)
        xv.append(x+cx)
        yv.append(y+cy)
    plt.plot(xv,yv)

    xv = []
    yv = []
    for i in range(segn+1):
        si = i*dsi
        x = R*math.cos(si)
        y = -r*math.sin(si)
        xv.append(x+cx)
        yv.append(y+cy)
    plt.plot(xv,yv)

    xv = []
    yv = []
    for i in range(segn+1):
        si = i*dsi
        x = R*math.cos(si)
        y = -r*math.sin(si)-h
        xv.append(x+cx)
        yv.append(y+cy)
    plt.plot(xv,yv)

    xv = [-R+cx, -R+cx]
    yv = [0.0+cy,-h+cy]
    plt.plot(xv,yv)

    xv = [R+cx, R+cx]
    yv = [0.0+cy,-h+cy]
    plt.plot(xv,yv)

drawMoonCake(plt, 0.0, 0.0)
drawMoonCake(plt, 0.0, -100.0)
drawMoonCake(plt, 120.0, -100.0)
drawMoonCake(plt, 0.0, -200.0)
drawMoonCake(plt, 120.0, -200.0)
drawMoonCake(plt, 240.0, -200.0)
plt.show()

 

posted @ 2021-09-21 10:17  abcstar  阅读(113)  评论(0编辑  收藏  举报