Project Description
Solutions with code
User Manual
Please install wxPython2.8 according to your python version first
The following two programs use wxPython2.8 for GUI
1.1 Drawing the Pythagorean Tree
Run step:
python pythagoras_tree.py
There are four drop list to control appearance of the Pythagorean Tree
- Scale
Each recursion, the rectangle will scaled down as this parameter
- Branching angle
This is the angle between left branch scaled down rectangle and parent rectangle
- Color
The color of the tree
- Depth
This is the recursion depth of the Pythagorean Tree
1.2 Drawing the Hilbert Curve
Run step:
python hilbert_curve.py
There are two drop list to control appearance of the Hilbert Curve
- Unit length
The unit length of the Hilbert Curve, the size will be changed
- Level
Level is the recursion depth, user can choose 1~6 to check different appearance of the Hilbert Curve
Code :
pythogoras.py
import wx, math
PI = 3.1415926
#LEFT_BRANCH_ANGLE = 30.0*PI/180
#MIN_WIDTH = 3
#SCALE = 0.75
# Four drop list user control id
SCALE_BOX_ID = 100
ANGLE_BOX_ID = 101
COLOR_BOX_ID = 102
DEPTH_BOX_ID = 103
# parameters available
scale = [‘0.75’, ‘0.6’, ‘0.3’]
branch_angle = [’30’, ’40’, ’45’, ’50’]
color = [“RED”, “GREEN”, “BLUE”]
depth = [‘1’, ‘2’, ‘3’, ‘6’, ‘8’, ’10’, ’12’]
scale_idx = 0
branch_angle_idx = 0
color_idx = 1
depth_idx = 5
# wx frame for GUI
class Frame(wx.Frame):
def __init__(self, parent=None, id=-1,
pos=wx.DefaultPosition, title=’Pythagoras Tree Demo!’):
wx.Frame.__init__(self, parent, id, title, pos)
size = (600, 360)
self.SetClientSize(size)
self.SetBackgroundColour(“BLACK”)
self.canvas = Panel(self, -1, 600, 300)
font = wx.Font(10, wx.MODERN, wx.NORMAL, wx.BOLD)
label1 = wx.StaticText(self, -1, ‘scale:’, (10, 322))
label1.SetFont(font)
label1.SetForegroundColour(“WHITE”)
box1 = wx.ComboBox(self, SCALE_BOX_ID, “”, (60, 320), (50, 30), scale, wx.CB_DROPDOWN)
box1.SetSelection(scale_idx)
label2 = wx.StaticText(self, -1, ‘branching angle:’, (124, 322))
label2.SetFont(font)
label2.SetForegroundColour(“WHITE”)
box2 = wx.ComboBox(self, ANGLE_BOX_ID, “”, (256, 320), (40, 30), branch_angle, wx.CB_DROPDOWN)
box2.SetSelection(branch_angle_idx)
label3 = wx.StaticText(self, -1, ‘color:’, (316, 322))
label3.SetFont(font)
label3.SetForegroundColour(“WHITE”)
box3 = wx.ComboBox(self, COLOR_BOX_ID, “”, (370, 320), (80, 30), color, wx.CB_DROPDOWN)
box3.SetSelection(color_idx)
label4 = wx.StaticText(self, -1, ‘depth:’, (472, 322))
label4.SetFont(font)
label4.SetForegroundColour(“WHITE”)
box4 = wx.ComboBox(self, DEPTH_BOX_ID, “”, (520, 320), (60, 30), depth, wx.CB_DROPDOWN)
box4.SetSelection(depth_idx)
self.Bind(wx.EVT_COMBOBOX, self.OnSelect)
def OnSelect(self, event):
global scale_idx, branch_angle_idx, color_idx, depth_idx
id = event.GetId()
index = event.GetSelection()
if SCALE_BOX_ID == id:
scale_idx = index
elif ANGLE_BOX_ID == id:
branch_angle_idx = index
elif COLOR_BOX_ID == id:
color_idx = index
elif DEPTH_BOX_ID == id:
depth_idx = index
self.canvas.Refresh()
# wx panel to draw Pythagoras Tree in OnPaint(…)
class Panel(wx.Panel):
def __init__(self, parent=None, id=-1, width = 600, height = 320):
wx.Panel.__init__(self, parent, id, size=(width, height))
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.SetBackgroundColour(“BLACK”)
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.DrawPythagorasTree(dc, 270, 280, 60, 60, 0)
# This is recursive function to draw a rectangle and it’s left&right branch
def DrawRect(self, dc, level, left, bottom, width, height, angle):
left_bottom = (left, bottom)
right_bottom = (left+width*math.cos(angle), bottom-width*math.sin(angle))
right_top = (right_bottom[0]-height*math.sin(angle), right_bottom[1]-height*math.cos(angle))
left_top = (left-height*math.sin(angle), bottom-height*math.cos(angle))
dc.SetPen(wx.RED_PEN)
dc.DrawLine(left_bottom[0], left_bottom[1], right_bottom[0], right_bottom[1])
dc.SetPen(wx.Pen(color[color_idx]))
dc.DrawLine(right_bottom[0], right_bottom[1], right_top[0], right_top[1])
dc.DrawLine(right_top[0], right_top[1], left_top[0], left_top[1])
dc.DrawLine(left_top[0], left_top[1], left_bottom[0], left_bottom[1])
#if width <= MIN_WIDTH or height <= MIN_WIDTH:
if level >= int(depth[depth_idx]):
return left_bottom, right_bottom, right_top, left_top
lb1, rb1, rt1, lt1 = left_bottom, right_bottom, right_top, left_top
left_branch_angle = float(branch_angle[branch_angle_idx])*PI/180
# Draw left branch
leftbranch_w, leftbranch_h = width*float(scale[scale_idx]), height*float(scale[scale_idx])
leftbranch_ang = angle+left_branch_angle
lb2, rb2, rt2, lt2 = self.DrawRect(dc, level+1, lt1[0], lt1[1], leftbranch_w, leftbranch_h, leftbranch_ang)
# Draw right branch
rightbranch_w = math.sqrt((rb2[0]-rt1[0])*(rb2[0]-rt1[0]) + (rb2[1]-rt1[1])*(rb2[1]-rt1[1]))
rightbranch_ang = angle – math.asin(leftbranch_w*math.sin(left_branch_angle)/rightbranch_w)
self.DrawRect(dc, level+1, rb2[0], rb2[1], rightbranch_w, rightbranch_w, rightbranch_ang)
return left_bottom, right_bottom, right_top, left_top
def DrawPythagorasTree(self, dc, left, bottom, width, height, angle):
dc.SetPen(wx.GREEN_PEN)
self.DrawRect(dc, 1, left, bottom, width, height, angle)
class App(wx.App):
def OnInit(self):
self.frame = Frame()
self.frame.Show()
self.SetTopWindow(self.frame)
return True
def main():
app = App()
app.MainLoop()
if __name__ == ‘__main__’:
main()
Project Details
- Date November 27, 2015
- Tags Python Programming