Computing for graphics: Computing for graphics.

Project Description

THE BRIEF
 
You have to fulfill the objectives of one the following four projects. All projects carry equal marks.
1) Drawing Fractal Shapes
Fractals are often used in modelling and animation to represent organic structures likes trees and plants.
The following fractal shapes are some of the well-studied self-replicating (recursive) organic-like structures.
Your task is to write a computer program(s) in C++ or Python that is capable of the following:
1.1) Drawing the Pythagorean Tree:
 
see also, http://en.wikipedia.org/wiki/Pythagoras_tree
1.2) Drawing the Hilbert Curve
see also:
http://en.wikipedia.org/wiki/Hilbert_curve,
http://en.wikipedia.org/wiki/David_Hilbert
http://www.digizeitschriften.de/dms/img/?PPN=GDZPPN002253135
Pythagorean Tree
Hilbert Curve
Your program should allow the user to change parameters controlling the appearance of the Pythagorean
Tree and Hilbert Curve interactively, for example the user should be able to control branching angle, scale,
colour, recursion depth, etc. Your implementation should not use the Python Tk “turtle” library
http://docs.python.org/library/turtle.htm l , although the wikipedia Hilbert Curve implementation using the
Python turtle library is a good starting point.
2) Automatic generation of Paul Klee-like pain
Paul Klee is a Swiss-born painter who produced powerful and influential work during the early 20th century.
Klee taught at the BAUHAUS school after World War I, where his friend Kandinsky was also a faculty
member. In the Pedagogical Sketchbook (1925) one of his several important essays on art theory, Klee
tried to define and analyse the primary visual elements and the ways in which they could be applied. In
1931 he began teaching at Dusseldorf Academy, but he was dismissed by the Nazis, who termed his work
degenerate. For more information about the artist look at:
http://en.wikipedia.org/wiki/Paul_Klee
http://www.ibiblio.org/wm/paint/auth/klee/
http://www.amazon.co.uk/Pedagogical-Sketchbook-Paul-Klee/dp/0571086187/ref=pd_sim_b_1
Many Paul Klee paintings are complex in structure, while created using simple rules of composition. Some
of his works consist of vertical/horizontal subdivision of space filled with colour gradients. The simplicity of
composition rules used by Paule Klee allow to design simple algorithms for visualizing paintings in the style
of Paul Klee. You could use the following two paintings as starting points: “Harbinger of autumn” 1922, and
“Castle and Sun” 1928.
Paul Klee, Harbinger of autumn, 1922
Your task is to write a program in C++ or in Python that generates Paul Klee like compositions. The purpose
of this assignment is not to replicate specific Paul Klee compositions (although your starting point should be
the two paintings shown above). Your application should be able to produce an infinite number of images all
in the style of composition of the paintings above.
The generated images should be based on a grid of vertical and horizontal subdivisions of the surface of
the painting. The colouring of each cell in the grid should be based on colours (or colour gradients) similar
to the ones used in the paintings shown above. Some cells may contain the same colour(s) or combine to
form cells of greater size. Some cells may contain “decorations”, such as, triangles, circles, or arches.
Horizontal and vertical lines that make up the grid’s subdivisions and cells should be in most cases
approximately parallel. However, as the images are hand-drawn the lines of the grid may not be entirely
straight, and may sometimes converge or undulate. Your program should allow the user to change
parameters controlling the appearance of the paintings generated. For example the user should be able to
control the number of horizontal or vertical subdivisions, the colour scheme, direction of subdivisions and
frequency of subdivisions, the number of decorations, etc.
3) The Towers of Hanoi
Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg
to another. The initial stack had 64 disks threaded onto one peg (the source peg) and arranged from bottom
to top by decreasing size. The priests are attempting to move the stack form this peg to a second peg (the
destination peg) under the constraints that exactly one disk is moved at a time, and at no time may a larger
disk be placed above a smaller disk. A third peg (the auxiliary peg) is available for temporarily holding disks.
Supposedly the world will end if the priests complete their task.
Paul Klee, Castle and Sun, 1928
In the above image source peg is Peg1 destination peg is Peg2 and auxiliary peg is Peg3. Implement an
program in Python or C++ that can generate an animation sequence (or the corresponding sequence of
frames) capable of visualising moving n disks from Peg 1 to Peg 2 while abiding by the rules outlined
above. See also:
http://en.wikipedia.org/wiki/Tower_of_Hanoi
http://obelix.ee.duth.gr/~apostolo/TowersOfHanoi/
4) Self portrait (a simple image processing filter)
Read a grey scale image (or images) in TIFF/JPG/BMP format using C++/Python, and proceed to modify
the images using the following rule: For every pixel (or rectangular area of pixels) matching a given intensity
substitute that pixel (or area of pixels) with a black-and-white shape (or shapes) of analogous intensity. For
example consider a row of grey scale pixels (as shown in the following figure), one could replace these
pixels with circles of different radius that correspond to the different pixel intensities.
Start by using very simple images, i.e. a row of pixels, and eventually apply the technique to a grey scale
image of yourself. Using circles of varying radius is only one possibility, one may use different shapes (or
collections of shapes), such as triangles, rectangles, ellipses lines, points, or letters. Your program should
define a range of parameters controlling the appearance of the resulting image. For example one should
— 

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

  1. Scale

Each recursion, the rectangle will scaled down as this parameter

  1. Branching angle

This is the angle between left branch scaled down rectangle and parent rectangle

  1. Color

The color of the tree

  1. 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

  1. Unit length

The unit length of the Hilbert Curve, the size will be changed

  1. 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()

hilbert curve.py:
 
import wx, math
# The direction of drawing line
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
# Two drop list user control id
LEN_BOX_ID = 100
LEVEL_BOX_ID = 101
# parameters available
length = [‘6’, ‘8’, ’12’]
levels = [‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’]
length_idx = 0
level_idx = 4
# wx frame for GUI
class Frame(wx.Frame):
  def __init__(self, parent=None, id=-1,
        pos=wx.DefaultPosition, title=’Hilbert Curve Demo!’):
    wx.Frame.__init__(self, parent, id, title, pos)
    size = (600, 600)
    self.SetClientSize(size)
    self.SetBackgroundColour(“BLACK”)
    self.canvas = Panel(self, -1, 600, 560)
    font = wx.Font(12, wx.MODERN, wx.NORMAL, wx.BOLD)
    #wx.ListBox(self, -1, wx.DefaultPosition, (500, 120), [‘toby’, ‘1’, ‘2’], wx.LB_DROPDOWN) # wx.LB_SINGLE
    label1 = wx.StaticText(self, -1, ‘unit length:’, (20, 572))
    label1.SetFont(font)
    label1.SetForegroundColour(“WHITE”)
    box1 = wx.ComboBox(self, LEN_BOX_ID, “”, (150, 570), (60, 30), length, wx.CB_DROPDOWN)
    box1.SetSelection(length_idx)
    label2 = wx.StaticText(self, -1, ‘level:’, (230, 572))
    label2.SetFont(font)
    label2.SetForegroundColour(“WHITE”)
    box2 = wx.ComboBox(self, LEVEL_BOX_ID, “”, (300, 570), (60, 30), levels, wx.CB_DROPDOWN)
    box2.SetSelection(level_idx)
    self.Bind(wx.EVT_COMBOBOX, self.OnSelect)
  def OnSelect(self, event):
    global length_idx, level_idx
    id = event.GetId()
    index = event.GetSelection()
    if LEN_BOX_ID == id:
      length_idx = index
    elif LEVEL_BOX_ID == id:
      level_idx = index
    #print id, index
    self.canvas.Refresh()
# wx panel to draw Hilbert Curve in OnPaint(…)
class Panel(wx.Panel):
  def __init__(self, parent=None, id=-1, width = 500, height = 500):
    wx.Panel.__init__(self, parent, id, size=(width, height))
    self.Bind(wx.EVT_PAINT, self.OnPaint)
    self.SetBackgroundColour(“BLACK”)
  def DrawTo(self, dc, direction):
    tox = toy = 0
    if UP == direction:
      tox = self.curx
      toy = self.cury – int(length[length_idx])
    elif DOWN == direction:
      tox = self.curx
      toy = self.cury + int(length[length_idx])
    elif LEFT == direction:
      tox = self.curx – int(length[length_idx])
      toy = self.cury
    elif RIGHT == direction:
      tox = self.curx + int(length[length_idx])
      toy = self.cury
    dc.DrawLine(self.curx, self.cury, tox, toy)
    self.curx = tox
    self.cury = toy
  # This is recursive function to draw Hilbert Curve
  # level will count down, when it’s 1, recursion function will return
  def DrawHilbert(self, dc, level, direction):
    if 1 == level:
      if LEFT == direction:
        self.DrawTo(dc, RIGHT);
        self.DrawTo(dc, DOWN);
        self.DrawTo(dc, LEFT);
      elif RIGHT == direction:
        self.DrawTo(dc, LEFT);
        self.DrawTo(dc, UP);
        self.DrawTo(dc, RIGHT);
      elif UP == direction:
        self.DrawTo(dc, DOWN);
        self.DrawTo(dc, RIGHT);
        self.DrawTo(dc, UP);
      elif DOWN == direction:
        self.DrawTo(dc, UP);
        self.DrawTo(dc, LEFT);
        self.DrawTo(dc, DOWN);
    else:
      if LEFT == direction:
        self.DrawHilbert(dc, level-1,UP);
        self.DrawTo(dc, RIGHT);
        self.DrawHilbert(dc, level-1,LEFT);
        self.DrawTo(dc, DOWN);
        self.DrawHilbert(dc, level-1,LEFT);
        self.DrawTo(dc, LEFT);
        self.DrawHilbert(dc, level-1,DOWN);
      elif RIGHT == direction:
        self.DrawHilbert(dc, level-1,DOWN);
        self.DrawTo(dc, LEFT);
        self.DrawHilbert(dc, level-1,RIGHT);
        self.DrawTo(dc, UP);
        self.DrawHilbert(dc, level-1,RIGHT);
        self.DrawTo(dc, RIGHT);
        self.DrawHilbert(dc, level-1,UP);
      elif UP == direction:
        self.DrawHilbert(dc, level-1,LEFT);
        self.DrawTo(dc, DOWN);
        self.DrawHilbert(dc, level-1,UP);
        self.DrawTo(dc, RIGHT);
        self.DrawHilbert(dc, level-1,UP);
        self.DrawTo(dc, UP);
        self.DrawHilbert(dc, level-1,RIGHT);
      elif DOWN == direction:
        self.DrawHilbert(dc, level-1,RIGHT);
        self.DrawTo(dc, UP);
        self.DrawHilbert(dc, level-1,DOWN);
        self.DrawTo(dc, LEFT);
        self.DrawHilbert(dc, level-1,DOWN);
        self.DrawTo(dc, DOWN);
        self.DrawHilbert(dc, level-1,LEFT);
  def OnPaint(self, event):
    dc = wx.PaintDC(self)
    dc.SetPen(wx.WHITE_PEN)
    self.curx = 30
    self.cury = 30
    self.DrawHilbert(dc, int(levels[level_idx]), UP)
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()
For EXE file check this link below

Project Details

  • Date November 27, 2015
  • Tags Python Programming

Leave a reply

Your email address will not be published. Required fields are marked *

Back to Top