摘要:分形一詞是芒德勃羅創(chuàng)造出來(lái)的其原意具有不規(guī)則支離破碎等意義。年芒德勃羅在法蘭西學(xué)院講課時(shí)首次提出了分維和分形的設(shè)想。分形是一個(gè)數(shù)學(xué)術(shù)語(yǔ)也是一套以分形特征為研究主題的數(shù)學(xué)理論。
上一篇《Turtle庫(kù)畫(huà)一棵對(duì)稱勾股樹(shù),美麗驚艷的分形世界!》中描繪了一棵對(duì)稱的雙色勾股樹(shù),詳情見(jiàn):Python Turtle庫(kù)畫(huà)一棵對(duì)稱勾股樹(shù),美麗驚艷的分形世界!_漢陽(yáng)Hann's Home-CSDN博客 分形,具有以非整數(shù)維形式充填空間的形態(tài)特征。通常被定義為“一個(gè)粗糙或零碎的幾何形狀,可以分成數(shù)個(gè)部分,且每一部分都(至少近似地)是整體縮小后的形狀”,即具有自相似的性質(zhì)。分形(Fractal)一詞,是芒德勃羅創(chuàng)造出來(lái)的,其原意具有不規(guī)則、支離破碎等意義。1973年,芒德勃羅(B.B.Mandelbrot)在法蘭西學(xué)院講課時(shí),首次提出了分維和分形的設(shè)想?! 》中问且粋€(gè)數(shù)學(xué)術(shù)語(yǔ),也是一套以分形特征為研究主題的數(shù)學(xué)理論。分形理論既是非線性科學(xué)的前沿和重要分支,又是一門(mén)新興的橫斷學(xué)科,是研究一類(lèi)現(xiàn)象特征的新.https://blog.csdn.net/boysoft2002/article/details/120466255
簡(jiǎn)單的,加上一隨機(jī)取色的函數(shù),就能畫(huà)出彩色樹(shù)來(lái)。顏色取值方法有很多,常用的如下:
方法一:隨機(jī)產(chǎn)生一個(gè)6位十六進(jìn)制數(shù)
>>> from random import choice>>> def rndColor(): color,strColor = '#','0123456789ABCDEF' for _ in range(6): color += choice(strColor) return color>>> print([rndColor() for _ in range(7)])['#47922E', '#540621', '#AEA706', '#263CAC', '#2F346E', '#067BAC', '#4D295B']>>>
方法二:在給定預(yù)定義顏色單詞中取值,顏色數(shù)相對(duì)少但也夠用
>>> def rndColor(): from random import choice colors = ('black', 'navy', 'darkblue', 'mediumblue', 'blue', 'darkgreen', 'green', 'teal', 'darkcyan', 'deepskyblue', 'darkturquoise', 'mediumspringgreen', 'lime', 'springgreen', 'aqua', 'cyan', 'midnightblue', 'dodgerblue', 'lightseagreen', 'forestgreen', 'seagreen', 'darkslategray', 'limegreen', 'mediumseagreen', 'turquoise', 'royalblue', 'steelblue', 'darkslateblue', 'mediumturquoise', 'indigo', 'darkolivegreen', 'cadetblue', 'cornflowerblue', 'mediumaquamarine', 'dimgray', 'slateblue', 'olivedrab', 'slategray', 'lightslategray', 'mediumslateblue', 'lawngreen', 'chartreuse', 'aquamarine', 'maroon', 'purple', 'olive', 'gray', 'skyblue', 'lightskyblue', 'blueviolet', 'darkred', 'darkmagenta', 'saddlebrown', 'darkseagreen', 'lightgreen', 'mediumpurple', 'darkviolet', 'palegreen', 'darkorchid', 'yellowgreen', 'sienna', 'brown', 'darkgray', 'lightblue', 'greenyellow', 'paleturquoise', 'lightsteelblue', 'powderblue', 'firebrick', 'darkgoldenrod', 'mediumorchid', 'rosybrown', 'darkkhaki', 'silver', 'mediumvioletred', 'indianred', 'peru', 'chocolate', 'tan', 'lightgray', 'thistle', 'orchid', 'goldenrod', 'palevioletred', 'crimson', 'gainsboro', 'plum', 'burlywood', 'lightcyan', 'lavender', 'darksalmon', 'violet', 'palegoldenrod', 'lightcoral', 'khaki', 'aliceblue', 'honeydew', 'azure', 'wheat', 'beige', 'whitesmoke', 'mintcream', 'ghostwhite', 'salmon', 'sandybrown', 'antiquewhite', 'linen', 'lightgoldenrodyellow', 'oldlace', 'red', 'fuchsia', 'magenta', 'deeppink', 'orangered', 'tomato', 'hotpink', 'coral', 'darkorange', 'lightsalmon', 'orange', 'lightpink', 'pink', 'gold', 'peachpuff', 'navajowhite', 'moccasin', 'bisque', 'mistyrose', 'blanchedalmond', 'papayawhip', 'lavenderblush', 'seashell', 'cornsilk', 'lemonchiffon', 'floralwhite', 'snow', 'yellow', 'lightyellow', 'ivory', 'white') return choice(colors)>>> print([rndColor() for _ in range(7)])['whitesmoke', 'dodgerblue', 'sandybrown', 'wheat', 'mediumorchid', 'goldenrod', 'tan']>>> print([rndColor() for _ in range(7)])['darkblue', 'slateblue', 'aliceblue', 'salmon', 'seashell', 'darkgray', 'bisque']>>>
就用方法一來(lái)取色,上一篇中的對(duì)稱勾股樹(shù)經(jīng)填色后,效果如下:
源代碼如下,填色語(yǔ)句已用#號(hào)注釋掉了(效果上圖左上):
from turtle import *from random import choicedef rndColor(): color,strColor = '#','0123456789ABCDEF' for _ in range(6): color += choice(strColor) return color def Square(self,length): self.color(rndColor()) #self.begin_fill() for _ in range(5): self.forward(length) self.right(90) #self.end_fill() def Triangle(self,length): self.color(rndColor()) #self.begin_fill() self.left(45) self.forward(length/2**0.5) self.right(90) self.forward(length/2**0.5) self.right(135) self.forward(length) #self.end_fill() def Move2Right(self,length): self.back(length) self.right(45) self.forward(length/2**0.5) self.right(90) def Recursive(n, tracer, length): if n<1: return tracers = [] for left in tracer: left.pencolor(rndColor()) Square(left, length) Triangle(left, length) right = left.clone() left.right(45) Move2Right(right, length) tracers.append(left) tracers.append(right) Recursive(n-1, tracers, length/2**0.5) def Setup(self, length, speed): self.hideturtle() self.speed(speed) self.penup() self.goto(-length*0.5, -length*1.8) self.seth(90) self.pensize(2) self.pendown() def main(level, length, speed=-1): setup(800,600) title('Fractal Tree') if speed==-1: tracer(0) else: tracer(1) t = Turtle() Setup(t, length, speed) from time import sleep sleep(2) Recursive(level, list([t]), length) done() bye() if __name__ == '__main__': main(9,135,-1)
左右偏向角都是45度勾股樹(shù)就是對(duì)稱的,那么角度指定從列表[i for i in range(5,86)]中抽取,勾股樹(shù)的“樹(shù)枝”就能任意角度生長(zhǎng)了。參考網(wǎng)上的代碼,增加了 onclick 事件,這樣就可以反復(fù)觀察效果。類(lèi)似于 tkinter 庫(kù)中用 .bind(<事件>,方法) 綁定函數(shù)來(lái)增加對(duì)象的事件動(dòng)作,當(dāng)然turtle窗口中也能使用bind方法(之后再作研究討論)。經(jīng)過(guò)修改后效果如下:
源代碼如下:
import turtlefrom random import choicefrom math import sin,cos,radiansdef rndColor(flag=True): colorTuple=( ('navy', '#000080'), ('darkblue', '#00008B'), ('mediumblue', '#0000CD'), ('blue', '#0000FF'), ('darkgreen', '#006400'), ('green', '#008000'), ('teal', '#008080'), ('darkcyan', '#008B8B'), ('deepskyblue', '#00BFFF'), ('darkturquoise', '#00CED1'), ('mediumspringgreen', '#00FA9A'), ('lime', '#00FF00'), ('springgreen', '#00FF7F'), ('aqua', '#00FFFF'), ('cyan', '#00FFFF'), ('midnightblue', '#191970'), ('dodgerblue', '#1E90FF'), ('lightseagreen', '#20B2AA'), ('forestgreen', '#228B22'), ('seagreen', '#2E8B57'), ('darkslategray', '#2F4F4F'), ('limegreen', '#32CD32'), ('mediumseagreen', '#3CB371'), ('turquoise', '#40E0D0'), ('royalblue', '#4169E1'), ('steelblue', '#4682B4'), ('darkslateblue', '#483D8B'), ('mediumturquoise', '#48D1CC'), ('indigo', '#4B0082'), ('darkolivegreen', '#556B2F'), ('cadetblue', '#5F9EA0'), ('cornflowerblue', '#6495ED'), ('mediumaquamarine', '#66CDAA'), ('dimgray', '#696969'), ('slateblue', '#6A5ACD'), ('olivedrab', '#6B8E23'), ('slategray', '#708090'), ('lightslategray', '#778899'), ('mediumslateblue', '#7B68EE'), ('lawngreen', '#7CFC00'), ('chartreuse', '#7FFF00'), ('aquamarine', '#7FFFD4'), ('maroon', '#800000'), ('purple', '#800080'), ('olive', '#808000'), ('gray', '#808080'), ('skyblue', '#87CEEB'), ('lightskyblue', '#87CEFA'), ('blueviolet', '#8A2BE2'), ('darkred', '#8B0000'), ('darkmagenta', '#8B008B'), ('saddlebrown', '#8B4513'), ('darkseagreen', '#8FBC8F'), ('lightgreen', '#90EE90'), ('mediumpurple', '#9370DB'), ('darkviolet', '#9400D3'), ('palegreen', '#98FB98'), ('darkorchid', '#9932CC'), ('yellowgreen', '#9ACD32'), ('sienna', '#A0522D'), ('brown', '#A52A2A'), ('darkgray', '#A9A9A9'), ('lightblue', '#ADD8E6'), ('greenyellow', '#ADFF2F'), ('paleturquoise', '#AFEEEE'), ('lightsteelblue', '#B0C4DE'), ('powderblue', '#B0E0E6'), ('firebrick', '#B22222'), ('darkgoldenrod', '#B8860B'), ('mediumorchid', '#BA55D3'), ('rosybrown', '#BC8F8F'), ('darkkhaki', '#BDB76B'), ('silver', '#C0C0C0'), ('mediumvioletred', '#C71585'), ('indianred', '#CD5C5C'), ('peru', '#CD853F'), ('chocolate', '#D2691E'), ('tan', '#D2B48C'), ('lightgray', '#D3D3D3'), ('thistle', '#D8BFD8'), ('orchid', '#DA70D6'), ('goldenrod', '#DAA520'), ('palevioletred', '#DB7093'), ('crimson', '#DC143C'), ('gainsboro', '#DCDCDC'), ('plum', '#DDA0DD'), ('burlywood', '#DEB887'), ('lightcyan', '#E0FFFF'), ('lavender', '#E6E6FA'), ('darksalmon', '#E9967A'), ('violet', '#EE82EE'), ('palegoldenrod', '#EEE8AA'), ('lightcoral', '#F08080'), ('khaki', '#F0E68C'), ('aliceblue', '#F0F8FF'), ('honeydew', '#F0FFF0'), ('azure', '#F0FFFF'), ('wheat', '#F5DEB3'), ('beige', '#F5F5DC'), ('whitesmoke', '#F5F5F5'), ('mintcream', '#F5FFFA'), ('ghostwhite', '#F8F8FF'), ('salmon', '#FA8072'), ('sandybrown', '#FAA460'), ('antiquewhite', '#FAEBD7'), ('linen', '#FAF0E6'), ('lightgoldenrodyellow', '#FAFAD2'), ('oldlace', '#FDF5E6'), ('red', '#FF0000'), ('fuchsia', '#FF00FF'), ('magenta', '#FF00FF'), ('deeppink', '#FF1493'), ('orangered', '#FF4500'), ('tomato', '#FF6347'), ('hotpink', '#FF69B4'), ('coral', '#FF7F50'), ('darkorange', '#FF8C00'), ('lightsalmon', '#FFA07A'), ('orange', '#FFA500'), ('lightpink', '#FFB6C1'), ('pink', '#FFC0CB'), ('gold', '#FFD700'), ('peachpuff', '#FFDAB9'), ('navajowhite', '#FFDEAD'), ('moccasin', '#FFE4B5'), ('bisque', '#FFE4C4'), ('mistyrose', '#FFE4E1'), ('blanchedalmond', '#FFEBCD'), ('papayawhip', '#FFEFD5'), ('lavenderblush', '#FFF0F5'), ('seashell', '#FFF5EE'), ('cornsilk', '#FFF8DC'), ('lemonchiffon', '#FFFACD'), ('floralwhite', '#FFFAF0'), ('snow', '#FFFAFA'), ('yellow', '#FFFF00'), ('lightyellow', '#FFFFE0'), ('ivory', '#FFFFF0'), ) # 已去掉black和white color,strColor = '#','0123456789ABCD' #去掉EF調(diào)節(jié)顏色 for _ in range(6): color += choice(strColor) if flag: return color return choice(colorTuple)[1] def Square(length): t.color(rndColor()) # 參數(shù)改為Flase換colorTuple中的顏色 t.begin_fill() for i in range(4): t.fd(length) t.right(90) t.end_fill()def biTree(length): if length<0.08*Length: return xy = t.position() if abs(xy[0])>Width/2-2*length: return if xy[1]>Half_H-length*3: return if xy[1]<-Half_H+length*0.5: return # 以上四行盡可能約束樹(shù)枝不超出可視范圍 Square(length) Angle = choice([i for i in range(5,86)]) #左右偏向角度,不宜過(guò)大或過(guò)小 t.forward(length) t.left(Angle) biTree(length * cos(radians(Angle))) Square(length * cos(radians(Angle))) t.right(90) t.forward(length * cos(radians(Angle))) biTree(length * sin(radians(Angle))) Square(length * sin(radians(Angle))) t.right(90) t.forward(length * sin(radians(Angle))) t.right(Angle) t.forward(length) t.right(90) t.forward(length) t.right(90)def main(x=None,y=None): x,y = -Half_L, Half_L-Half_H t.clear() t.up() t.goto(x,y) t.down() #t.pensize(2) t.setheading(90) biTree(Length) t.penup() t.goto(Length*0.8,-Half_H+Half_L) t.pencolor('red') t.write("Click to refresh......") t.hideturtle() if __name__ == '__main__': Width,Height = 1200,800 #屏幕寬高 Length = 100 #第一層正方形的邊長(zhǎng) Half_L,Half_H = Length/2,Height/2 turtle.setup(Width,Height) turtle.title('隨機(jī)勾股樹(shù)') turtle.tracer(0) t = turtle.Turtle() #t.speed(0) main() ts = turtle.Screen() ts.onclick(main) turtle.mainloop()
代碼執(zhí)行后,不停點(diǎn)擊屏幕的動(dòng)態(tài)效果如下:
本篇完!?
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/121422.html
摘要:但今年不能老送同樣的東西啊,那就給大家送上幾棵圣誕樹(shù)吧。極簡(jiǎn)版這個(gè)可算是最簡(jiǎn)單的圣誕樹(shù)了。例如上面這棵圣誕樹(shù),每一個(gè)樹(shù)枝又是一個(gè)小的圣誕樹(shù)。這與編程中的遞歸思想很像頂部五角星略過(guò)炫彩版一般圣誕樹(shù)上都會(huì)掛上的小彩燈。 今天是圣誕節(jié),先祝大家圣誕快樂(lè)! 有人要說(shuō)了,圣誕節(jié)是耶穌誕生的日子,我又不信基督教,有啥好慶祝的。這你就有所不知了,Python 的誕生也跟圣誕節(jié)有關(guān):1989 年,那是...
摘要:然而,這樣的狂野生長(zhǎng)背后,卻擴(kuò)大了這四個(gè)問(wèn)題的嚴(yán)重性。當(dāng)云計(jì)算市場(chǎng)規(guī)模擴(kuò)大,這種先天疾病勢(shì)必對(duì)企業(yè)云業(yè)務(wù)發(fā)展造成更多的影響,導(dǎo)致競(jìng)爭(zhēng)力進(jìn)一步缺失。2015年,云計(jì)算產(chǎn)值為1750億美元,2016年為2092億美元,據(jù)最新的預(yù)測(cè),2017年云計(jì)算的產(chǎn)業(yè)總值可以達(dá)到2468億美元。如果按照這樣的勢(shì)頭去發(fā)展,2018年就能達(dá)到2900億美元,而2019年就已經(jīng)快達(dá)到3500億美元了。2015年的時(shí)...
摘要:年月,小邪正式入職阿里巴巴,首次接觸淘寶商城項(xiàng)目開(kāi)啟了其十年的阿里技術(shù)生涯,去年月加入阿里云,任飛天八部掌門(mén)人。技術(shù)成長(zhǎng)角色轉(zhuǎn)變也是職責(zé)轉(zhuǎn)變?cè)诎⒗镌埔馕吨N近客戶,這對(duì)小邪來(lái)說(shuō)是最大的改變。 摘要: 從2008年到2018年,從阿里巴巴中間件團(tuán)隊(duì)到飛天八部——小邪與阿里的十年。 編者按:從2008年到2018年,從阿里巴巴中間件團(tuán)隊(duì)到飛天八部——小邪與阿里的十年。 2008年4月,小...
摘要:周末參加了又認(rèn)了不少人面對(duì)大型活動(dòng)總讓我有點(diǎn)鄉(xiāng)下人進(jìn)城的感覺(jué)我說(shuō)好聽(tīng)是宅實(shí)際上缺少各種社交場(chǎng)合打交道的經(jīng)驗(yàn)只有技術(shù)還能說(shuō)得開(kāi)我遇到過(guò)好多人讓我艷羨的社交能力當(dāng)然這不是文章的重點(diǎn)這是我第一次參加前端大型的聚會(huì)第二次去大型的活動(dòng)第二次去淘寶城有 周末參加了 D2, 又認(rèn)了不少人, 面對(duì)大型活動(dòng)總讓我有點(diǎn)鄉(xiāng)下人進(jìn)城的感覺(jué) 我說(shuō)好聽(tīng)是宅, 實(shí)際上缺少各種社交場(chǎng)合打交道的經(jīng)驗(yàn), 只有技術(shù)還能說(shuō)得...
閱讀 735·2023-04-25 19:43
閱讀 3981·2021-11-30 14:52
閱讀 3807·2021-11-30 14:52
閱讀 3871·2021-11-29 11:00
閱讀 3802·2021-11-29 11:00
閱讀 3904·2021-11-29 11:00
閱讀 3580·2021-11-29 11:00
閱讀 6182·2021-11-29 11:00