首页 > 编程语言 >Python递归画树 - 画蕨类植物 - 画窗格 - 搭建分形平台

Python递归画树 - 画蕨类植物 - 画窗格 - 搭建分形平台

时间:2023-03-14 21:23:14浏览次数:54  
标签:origin 画树 scale angle img Python self length 窗格

@

目录


画树

import numpy as np
import cv2
import math


def draw_tree(img, origin, length, angle, scale, left_angle, left_scale, right_angle, right_scale):
    if length < 4:
        return  # 当length小于2的时候,停止画线(也可以用下面两行代码,表示当length小于2的时候,再画一次停止)

        # end = to_int(get_point(origin, length * scale, angle))
        # cv2.line(img, origin, end, (0, 255, 0))

    else:
        end = to_int(get_point(origin, length*scale, angle))
        cv2.line(img, origin, end, (0, 255, 0))
        # cv2.imshow('MyTree', img)
        # cv2.waitKey(100)

        draw_tree(img, end, length*left_scale, angle+left_angle, scale, left_angle, left_scale, right_angle, right_scale)
        draw_tree(img, end, length*right_scale, angle+right_angle, scale, left_angle, left_scale, right_angle, right_scale)


def get_point(origin, length, angle):
    x, y = origin
    angle *= math.pi / 180
    x += length * math.cos(angle)
    y += length * math.sin(angle)
    return x, y


def to_int(p):
    return int(p[0] + 0.5), int(p[1] + 0.5)


if __name__ == '__main__':
    height = 300
    width = 400
    img = np.zeros([height, width, 3])  # H,W,C
    draw_tree(img, (width//2, height), height, -90, 0.4, -40, 0.5, 30, 0.6)  # BRG
    cv2.imshow('MyTree', img)
    cv2.waitKey()

效果图如下:


画蕨类植物

import numpy as np
import cv2
import math


def draw_tree(img, origin, length, angle, scale, left_angle, left_scale, right_angle, right_scale):
    if length < 4:
        return  # 当length小于2的时候,停止画线(也可以用下面两行代码,表示当length小于2的时候,再画一次停止)

        # end = to_int(get_point(origin, length * scale, angle))
        # cv2.line(img, origin, end, (0, 255, 0))

    else:
        end = to_int(get_point(origin, length*scale, angle))
        cv2.line(img, origin, end, (0, 255, 0))
        # cv2.imshow('MyTree', img)
        # cv2.waitKey(100)

        draw_tree(img, end, length*left_scale, angle+left_angle, scale, left_angle, left_scale, right_angle, right_scale)
        draw_tree(img, end, (1-scale) * length, angle - 10, scale, left_angle, left_scale, right_angle, right_scale)
        draw_tree(img, end, length*right_scale, angle+right_angle, scale, left_angle, left_scale, right_angle, right_scale)


def get_point(origin, length, angle):
    x, y = origin
    angle *= math.pi / 180
    x += length * math.cos(angle)
    y += length * math.sin(angle)
    return x, y


def to_int(p):
    return int(p[0] + 0.5), int(p[1] + 0.5)


if __name__ == '__main__':
    height = 300
    width = 400
    img = np.zeros([height, width, 3])  # H,W,C
    draw_tree(img, (width//2, height), height, -90, 0.2, -40, 0.3, 30, 0.3)  # BRG
    cv2.imshow('MyTree', img)
    cv2.waitKey()

效果图如下:


画窗格

import numpy as np
import cv2
import math


def draw_window(img, origin, length, angle):
    if length < 20:
        end = to_int(get_point(origin, length, angle))
        cv2.line(img, to_int(origin), end, (0, 255, 0))
        # cv2.imshow('img', img)
        # cv2.waitKey(1000)

    else:
        length /= 3
        draw_window(img, origin, length, angle)
        end = to_int(get_point(origin, length, angle))

        draw_window(img, end, length, angle - 60)
        end = to_int(get_point(end, length, angle-60))

        draw_window(img, end, length, angle + 60)
        end = to_int(get_point(end, length, angle+60))

        draw_window(img, end, length, angle)


def get_point(origin, length, angle):
    x, y = origin
    angle *= math.pi / 180
    x += length * math.cos(angle)
    y += length * math.sin(angle)
    return x, y


def to_int(p):
    return int(p[0] + 0.5), int(p[1] + 0.5)


if __name__ == '__main__':
    height = 300
    width = 400
    img = np.zeros([height, width, 3])
    draw_window(img, (0, height // 2), width, 0)
    cv2.imshow('img', img)
    cv2.waitKey()

效果图如下:


分形平台

import cv2
import numpy as np
import math


class Env:
    def __init__(self, origin, alpha, length, color, limit=4):
        self.origin = origin
        self.alpha = alpha
        self.length = length
        self.color = color
        self.limit = limit

    def copy(self):
        env2 = Env(self.origin, self.alpha, self.length, self.color, self.limit)
        return env2


class Graphics:
    # def __init__(self):
    #     self.fraction = None

    def draw(self, env):
        raise Exception('The draw() method is not override!')


class Fraction(Graphics):
    def __init__(self):
        self.gs = []

    def add(self, g: Graphics):
        g.fraction = self
        self.gs.append(g)

    def draw(self, env):
        for g in self.gs:
            g.draw(env)


class LR(Graphics):
    def __init__(self, len_scale, alpha_adjust, move):
        self.len_scale = len_scale
        self.alpha_adjust = alpha_adjust
        self.move = move


class Line(LR):
    def __init__(self, len_scale, alpha_adjust, move=True):
        super(Line, self).__init__(len_scale, alpha_adjust, move)

    def draw(self, env):
        end = to_int(get_end(env.origin, self.len_scale * env.length, env.alpha + self.alpha_adjust))
        cv2.line(img, env.origin, end, env.color)

        if self.move:
            env.origin = end


def get_end(origin, length, alpha):
    x, y = origin[0], origin[1]
    alpha = alpha * math.pi / 180
    x += length * math.cos(alpha)
    y += length * math.sin(alpha)
    return x, y

def to_int(coord):
    return int(coord[0] + 0.5), int(coord[1] + 0.5)


class Recur(LR):
    def __init__(self, len_scale, alpha_adjust, move=False):
        super(Recur, self).__init__(len_scale, alpha_adjust, move)

    def draw(self, env):
        length = self.len_scale * env.length
        alpha = self.alpha_adjust + env.alpha
        end = to_int(get_end(env.origin, length, alpha))

        if length <= env.limit:
            cv2.line(img, env.origin, end, env.color)
        else:
            env2 = env.copy()
            env2.length = length
            env2.alpha = alpha
            self.fraction.draw(env2)

        if self.move:
            env.origin = end


if __name__ == '__main__':
    f = Fraction()
    f.add(Line(0.4, 0, True))
    f.add(Recur(0.5, -20, False))
    f.add(Recur(0.6, 40, False))
    h, w = 600, 900
    img = np.zeros((h, w, 3))
    env = Env((w//6, h), -90, h, (0, 0, 255))
    f.draw(env)

    f = Fraction()
    f.add(Recur(0.333, 0, True))
    f.add(Recur(0.333, -60, True))
    f.add(Recur(0.333, 60, True))
    f.add(Recur(0.333, 0))
    env = Env((w//3, h * 3//4), 0, w//3, (255, 0, 0), limit=20)
    f.draw(env)

    f = Fraction()
    f.add(Line(0.2, 0, True))
    f.add(Recur(0.3, -30, False))
    f.add(Recur(0.8, -10, False))
    f.add(Recur(0.3, 35, False))
    env = Env((w * 5//6, h), -90, h, (0, 255, 0))
    f.draw(env)

    cv2.imshow('MyImage', img)
    cv2.waitKey()

效果图如下:

标签:origin,画树,scale,angle,img,Python,self,length,窗格
From: https://www.cnblogs.com/jybf/p/17216450.html

相关文章

  • 实验1 Python
    1.1print('hey,u')x,y,z=1,2,3print(x,y,z)print('x=%d,y=%d,z=%d'%(x,y,z))print('x={},y={},z={}.format(x,y,z)')print(f'x={x},y={y},z={z}')print(x)print(y......
  • opencv-python笔记(1)
    openCV笔记图像灰度图像​ 0代表纯黑色,255代表白色(类似亮度)通道拆分通过索引拆分:importcv2lena=cv2.imread("lenacolor.png")cv2.imshow("lena1",lena)b=lena[:......
  • Python+ESP32 收录集
    目录Python+ESP32收录集Python+ESP32收录集Python+ESP32快速上手(持续更新中)wifi蓝牙智能控制单片机手把手教你学ESP32--基于MicroPython......
  • HJ走方格的方案数_动态规划 python
      1importsys2m,n=list(map(int,sys.stdin.readline().strip().split()))3#记录横向小格边为1,竖向为-1。n=1*n,m=-1*m4#记录从出发点到每个节点的走法,取......
  • 实验1 Python开发环境使用和编程初体验
    实验一task1-1.python实验源码1#print输出的几种用法23#用法1:用于输出单个字符串或单个变量4print('hey,u')56#用法2:用于输出多个数据项,用逗号分隔......
  • 实验1 Python开发环境使用和编程初体验
    实验任务1#task1_1:print输出的几种用法实验内容#用法1:输出单个字符串或者变量print("hey,u")#用法2:用于输出多个数......
  • 实验1Python初体验
    实验任务1task1_1.py实验源码print('hey,u')print('hey','u')x,y,z=1,2,3print(x,y,z)print('x=%d,y=%d,z=%d'%(x,y,z))print('x={},y={},z={}'.form......
  • 实验1 python开发环境使用和编程初体验
    实验任务1: task1_1.py print('hey,u')print('hey','u')x,y,z=1,2,3print(x,y,z)print('x=%d,y=%d,z=%d'%(x,y,z))print('x={},y={},z={}......
  • 教你用Python画个可爱的皮卡丘!(附完整源码)
    版权声明:原创不易,本文禁止抄袭、转载,侵权必究! 一、去吧!皮卡丘!使用turtle(海龟库)制作而成,感觉挺好玩的,哈哈@>_<@,效果如下: 由于源码过长,这里仅展示部分代码:from......
  • Python基础--字符串处理
    Python中,字符串变量提供了3种方法来删除字符串中多余的空格和特殊字符,它们分别是:strip():删除字符串前后(左右两侧)的空格或特殊字符。lstrip():删除字符串前面(左边)的空......