Skip to content
Snippets Groups Projects
CF_1_4_template.py 2.23 KiB
Newer Older
  • Learn to ignore specific revisions
  • stalscar's avatar
    stalscar committed
    # CF_1_4_template.py
    # rectangle subdivision
    
    stalscar's avatar
    stalscar committed
    # Here with "WindowRasterized" - that means (0,0) is top/left !!!
    
    stalscar's avatar
    stalscar committed
    
    from libcfcg import cf
    import sys
    import time
    
    color = cf.Color.BLACK
    
    stalscar's avatar
    stalscar committed
    i_range = 512  # 64 #8
    j_range = 512  # 64 #8
    counter = 0
    
    
    stalscar's avatar
    stalscar committed
    ###
    # Recursive function painting the top right rectangle
    # window: surface of the framework
    # bot_left: bottom left corner of the rectangle to be processed
    # top_right: top right corner of the rectangle to be processed
    ###
    def paint(window, bot_left, top_right):
    
    stalscar's avatar
    stalscar committed
        width = top_right.x - bot_left.x + 1
        height = bot_left.y - top_right.y + 1
        # Example for drawing a rectangle
        # (1. param: bottom left corner, 2. param: top right corner)
        middlepoint = cf.Point(bot_left.x + width / 2, bot_left.y - height / 2)
        middlepoint_l_b = cf.Point(middlepoint.x/2, middlepoint.y*1.5)
        middlepoint_r_b = cf.Point(middlepoint.x*1.5,middlepoint.y*1.5)
        middlepoint_l_t = cf.Point(middlepoint.x/2,middlepoint.y/2)
    
    stalscar's avatar
    stalscar committed
    
    
    stalscar's avatar
    stalscar committed
        uppermiddlepoint = cf.Point(middlepoint.x, 0)
        lowermiddlepoint = cf.Point(middlepoint.x, height-1)
        leftmiddlepoint = cf.Point(0, middlepoint.y)
        rightmiddlepoint = cf.Point(width-1, middlepoint.y)
    
    stalscar's avatar
    stalscar committed
    
    
    stalscar's avatar
    stalscar committed
        window.drawRectangle(middlepoint, top_right, -1, cf.Color.BLACK)
        window.drawRectangle(middlepoint_l_t, uppermiddlepoint,-1,cf.Color.BLACK)
        window.drawRectangle(middlepoint_l_b,middlepoint,-1,cf.Color.BLACK)
        window.drawRectangle(middlepoint_r_b,rightmiddlepoint,-1,cf.Color.BLACK)
        window.show()   # show the progress after every recursive function call
        window.waitKey()
        # Your code here:
        # 1.) Call the recursive function for the top left, bottom left and
        #     bottom right square
    
        paint(window,middlepoint,top_right)
    
        # 2.) Terminate process right in time
    
    stalscar's avatar
    stalscar committed
    
    
    
    def main():
        window = cf.WindowRasterized(i_range, j_range, "CF_1_4")
    
    stalscar's avatar
    stalscar committed
        window.setWindowDisplayScale(512 / i_range)
    
    stalscar's avatar
    stalscar committed
        window.clear(cf.Color.RED)
        # Define initial corners
        bot_left = cf.Point(0, window.getHeight() - 1)
        top_right = cf.Point(window.getWidth() - 1, 0)
        # Call the recursive function
        paint(window, bot_left, top_right)
        window.show()
        print("Press any key to finish")
        sys.stdout.flush()
        time.sleep(0.1)
        window.waitKey()
    
    
    stalscar's avatar
    stalscar committed
    
    
    stalscar's avatar
    stalscar committed
    main()
    
    window = None