基于Python的matplotlib库绘制分形树

import math
import copy
import numpy as np
import matplotlib.pyplot as plt
# 距离计算公式
def get_len(x1,x2,y1,y2):
    diff_x = (x1-x2)**2
    diff_y = (y1-y2)**2
    length = np.sqrt(diff_x+diff_y)
    return length
  
# 绕pointx,pointy顺时针旋转
def Srotate(angle,valuex,valuey,pointx,pointy):
  # 将角度转换为弧度
  angle = math.radians(angle)
  valuex = np.array(valuex)
  valuey = np.array(valuey)
  sRotatex = (valuex-pointx)*math.cos(angle) + (valuey-pointy)*math.sin(angle) + pointx
  sRotatey = (valuey-pointy)*math.cos(angle) - (valuex-pointx)*math.sin(angle) + pointy
  sRotatex = sRotatex.tolist()
  sRotatey = sRotatey.tolist()
  sRotate = sRotatex + sRotatey
  sRotate = np.array(sRotate)
  return sRotate

# 绕pointx,pointy逆时针旋转
def Nrotate(angle,valuex,valuey,pointx,pointy):
  # 将角度转换为弧度
  angle = math.radians(angle)
  valuex = np.array(valuex)
  valuey = np.array(valuey)
  nRotatex = (valuex-pointx)*math.cos(angle) - (valuey-pointy)*math.sin(angle) + pointx
  nRotatey = (valuex-pointx)*math.sin(angle) + (valuey-pointy)*math.cos(angle) + pointy
  nRotatex = nRotatex.tolist()
  nRotatey = nRotatey.tolist()
  nRotate = nRotatex + nRotatey
  nRotate = np.array(nRotate)
  return nRotate
# 判断两条线段是否相交
def judge(a,b,c,d):
  if min(a[0],b[0])<=max(c[0],d[0]) and min(c[1],d[1])<=max(a[1],b[1]) and min(c[0],d[0])<=max(a[0],b[0]) and min(a[1],b[1])<=max(c[1],d[1]):
    return True
  else:
    return False

allBranchs = []
def pltTrees(arr):
  branchLen = get_len(arr[0],arr[1],arr[2],arr[3])
  if branchLen>0.2:
    plt.plot(arr[0:2],arr[2:4])
    arr_next_left = Srotate(20,arr[0:2],arr[2:4],arr[0],arr[2])
    arr_next_left = np.array(arr_next_left)*0.75
    arr_next_left[0:2] = arr_next_left[0:2] - (arr_next_left[0]-arr[1])
    arr_next_left[2:4] = arr_next_left[2:4] - (arr_next_left[2]-arr[3])
    booleanListLeft = []
    for i in range(len(allBranchs)):
      a = [allBranchs[i][0],allBranchs[i][2]]
      b = [allBranchs[i][1],allBranchs[i][3]]
      c = [arr_next_left[0],arr_next_left[2]]
      d = [arr_next_left[1],arr_next_left[3]]
      if a == c or b == d or a == d or b == c:
        continue
      result = judge(a,b,c,d)
      booleanListLeft.append(result)
    if True not in booleanListLeft:
      allBranchs.append(arr_next_left)
      pltTrees(arr_next_left)
    arr_next_right = Nrotate(40,arr_next_left[0:2],arr_next_left[2:4],arr_next_left[0],arr_next_left[2])
    booleanListRight = []
    for i in range(len(allBranchs)):
      a = [allBranchs[i][0],allBranchs[i][2]]
      b = [allBranchs[i][1],allBranchs[i][3]]
      c = [arr_next_right[0],arr_next_right[2]]
      d = [arr_next_right[1],arr_next_right[3]]
      if a == c or b == d or a == d or b == c:
        continue
      result = judge(a,b,c,d)
      booleanListRight.append(result)
    if True not in booleanListRight:
      allBranchs.append(arr_next_right)
      pltTrees(arr_next_right)
    
  
arr = np.array([1,2,1,2])
pltTrees(arr)
plt.show()

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值