Fractal Tree
Ceviz Viki sitesinden
Bu kod Koch kar tanesi çizimi (Python) sayfasındaki koda bir yığıt eklenerek nasıl dallanma yapılabileceğini gösterir.
Wikipedia:L-systems
Kod
#!/usr/bin/env python
"""Copyright 2007 Euclides
euclides@int6.net
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>."""
import wx
import math
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Fractal plant", wx.DefaultPosition, wx.Size(1000, 700))
self.bmp = wx.EmptyBitmap(1000, 700)
self.Bind(wx.EVT_PAINT, self.OnPaint)
dc = wx.BufferedPaintDC(self, self.bmp)
dc.SetBrush(wx.Brush("WHITE",wx.SOLID))
dc.Clear()
self.step = 1.5
self.i = 1.0
self.j = 0.0
self.px = 0.0
self.py = 100.0
# ara durumlarin tutulacagi yigit
self.stack = []
self.Normalize()
self.Center()
self.Show(1)
wcode = "X"
for i in range(7):
wcode = wcode.replace("X","F-[[X]+X]+F[+FX]-X")
wcode = wcode.replace("F","FF")
self.Decode(wcode,math.pi / 180.0 * 25.0)
def OnPaint(self, event):
dc = wx.BufferedPaintDC(self, self.bmp)
def Normalize(self):
lenght = math.sqrt((self.i * self.i) + (self.j * self.j))
self.i = self.i / lenght
self.j = self.j / lenght
def rot(self,rad):
i = (self.i * math.cos(rad)) - (self.j * math.sin(rad))
j = (self.j * math.cos(rad)) + (self.i * math.sin(rad))
self.i = i
self.j = j
def DrawLine(self, x0, y0, x1, y1):
dc = wx.BufferedDC(wx.ClientDC(self), self.bmp)
dc.SetPen(wx.Pen(wx.Colour(90,150,20), 1, wx.SOLID))
dc.DrawLine(x0 , (y0 * -1) + 500, x1, (y1 * -1) + 500)
def Decode(self,data,crt):
for w in data:
if w == '-':
self.rot(-crt)
self.Normalize()
elif w == '+':
self.rot(crt)
self.Normalize()
elif w == 'F':
nx = self.px + self.i*self.step
ny = self.py + self.j*self.step
self.DrawLine(self.px,self.py,nx,ny)
self.px = nx
self.py = ny
elif w == '[':
self.stack.append((self.px, self.py, self.i, self.j))
elif w == ']':
self.px, self.py, self.i, self.j = self.stack.pop()
def main():
theApp = wx.PySimpleApp()
theApp.SetTopWindow(MyFrame())
theApp.MainLoop()
if __name__ == "__main__": main()