python3 matplotlib library use recursion to draw a regular hexagon

Experimental requirements:
use python regular hexagon graph recursion, recursive layers need to be optional

Tucao, matplotlib parameters must draw the line is x, y, really is a giant pit

code show as below:

import matplotlib.pyplot as plt#约定俗成的写法plt
import numpy as np
import math


num = int(input("please input the number of recursion:"))
# num = 5


plt.figure()
 
plt.xlim(0,800) #x轴坐标轴
plt.ylim((0, 600))#y轴坐标轴
plt.xlabel('X')#x轴标签
plt.ylabel('Y')#y轴标签


# give the pot of regular hexagon
distance = 150*math.sqrt(3)
x = [300,600,750,600,300,150,300]
y = [0,0,distance,2*distance,2*distance,distance,0]
num -= 1
plt.plot(x,y, color='#FF0000')
dataX = x
dataY = y
while num > 0:
    num -= 1
    demoX = [abs(dataX[i] + dataX[i+1])/2 for i in range(6)]
    demoY = [abs(dataY[i] + dataY[i+1])/2 for i in range(6)]
    dataX = demoX
    dataX.append(demoX[0])
    dataY = demoY
    dataY.append(demoY[0])
    x = dataX
    y = dataY
    plt.plot(x,y, color='#FF0000',) #注意这里必须是x,y,所以我的上一步做了一个赋值操作


plt.show()

Demonstration effect is as follows:

Here Insert Picture Description

Published 168 original articles · won praise 344 · views 720 000 +

Guess you like

Origin blog.csdn.net/wy_97/article/details/102826673