#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2

    Need help with creating a drawing program


    I have to write a program (doGraphics.py) that creates a drawing from a series of instructions stored in the program as string such as:

    Code:
    drawingInstructions = """r 200 200 100 80
    l 235 200 220 180
    l 235 200 240 175
    l 245 200 270 180
    r 220 220 20 20
    r 260 220 20 20
    r 240 260 30 10"""
    how do i do this?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    The program I envision has two parts.

    An interpreter. Reading the tea leaves I'm getting strong feelings that each drawing instruction occupies a line, starts with a word---l or r here, followed by 4 integers. What does this l and r mean? The interpreter reads this external form and converts it into an internal form that can display a drawing.

    A drawing module. Use the turtle graphics module (link here). It will be easiest, unless these drawing instructions have a deep and complicated meaning.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    Sigh, ok maybe I'll let him have some code to draw something this one time:
    Code:
    #/usr/bin/python
    import sys
    
    drawingInstructions = """r 200 200 100 80
    l 235 200 220 180
    l 235 200 240 175
    l 245 200 270 180
    r 220 220 20 20
    r 260 220 20 20
    r 240 260 30 10"""
    
    foo = [
    (32, 4), (95, 8), (32, 3), (95, 2), (32, 4), (95, 2), (32, 4), (95, 7), (32, 3), (95, 2), (32, 4), (95, 2), ('RL', 1), 
    (32, 3), (124, 1), (9, 1), (32, 4), (124, 1), (32, 1), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (47, 1), (9, 1), (32, 2), (124, 1), (32, 1), (124, 1), (32, 2), (124, 1), (32, 2), (47, 1), (32, 2), (47, 1), ('RL', 1), 
    (32, 3), (124, 1), (32, 2), (45, 6), (39, 1), (32, 1), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 3), (95, 4), (124, 1), (32, 1), (124, 1), (32, 2), (124, 1), (32, 1), (47, 1), (32, 2), (47, 1), ('RL', 1), 
    (32, 3), (124, 1), (32, 2), (124, 1), (95, 3), (32, 4), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (9, 1), (32, 4), (124, 1), (32, 2), (124, 1), (47, 1), (32, 2), (47, 1), ('RL', 1), 
    (32, 3), (124, 1), (32, 3), (95, 3), (124, 1), (32, 3), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (9, 1), (32, 4), (124, 1), (9, 1), (32, 2), (47, 1), ('RL', 1), 
    (32, 3), (124, 1), (32, 2), (124, 1), (32, 7), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (95, 4), (32, 2), (124, 1), (9, 1), (32, 2), (92, 1), ('RL', 1), 
    (32, 3), (124, 1), (32, 2), (124, 1), (32, 7), (124, 1), (32, 3), (45, 2), (32, 3), (124, 1), (32, 2), (124, 1), (9, 1), (32, 2), (124, 1), (32, 1), (124, 1), (32, 2), (124, 1), (92, 1), (32, 2), (92, 1), ('RL', 1), 
    (32, 3), (124, 1), (95, 2), (124, 1), (32, 7), (92, 1), (95, 8), (47, 1), (32, 3), (92, 1), (95, 6), (124, 1), (32, 1), (124, 1), (95, 2), (124, 1), (32, 1), (92, 1), (95, 2), (92, 1), ('RL', 1), 
    ('RL', 1), 
    (9, 4), (32, 5), (95, 5), ('RL', 1), 
    (9, 4), (32, 4), (124, 2), (9, 1), (32, 1), (124, 2), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (92, 1), (95, 3), (47, 1), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 3), (32, 7), (95, 5), (124, 1), (60, 1), (45, 3), (62, 1), (124, 1), (95, 5), ('RL', 1), 
    (9, 3), (32, 6), (47, 1), (32, 5), (124, 1), (9, 1), (32, 2), (124, 1), (9, 1), (92, 1), ('RL', 1), 
    (9, 3), (32, 1), (47, 1), (32, 4), (124, 1), (32, 5), (124, 1), (9, 1), (32, 2), (124, 1), (9, 1), (124, 1), (32, 1), (92, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (32, 4), (124, 1), (32, 5), (124, 1), (9, 1), (32, 2), (124, 1), (9, 1), (124, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (32, 4), (124, 1), (32, 5), (124, 1), (9, 1), (32, 2), (124, 1), (9, 1), (124, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (9, 3), (124, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (9, 3), (124, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (9, 3), (32, 2), (47, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (9, 3), (32, 1), (47, 1), ('RL', 1), 
    (9, 3), (32, 2), (92, 1), (9, 2), (32, 7), (47, 1), ('RL', 1), 
    (9, 3), (32, 3), (92, 1), (9, 2), (32, 6), (47, 1), ('RL', 1), 
    (9, 3), (32, 4), (124, 1), (9, 2), (32, 6), (124, 1), ('RL', 1), 
    (9, 3), (32, 4), (124, 1), (9, 2), (32, 6), (124, 1), ('RL', 1), 
    ('RL', 1), 
    (9, 4), (32, 5), (95, 4), (32, 5), (95, 4), (32, 2), (95, 9), (9, 1), (95, 3), (9, 1), (95, 3), ('RL', 1), 
    (9, 4), (32, 5), (92, 1), (9, 1), (32, 1), (92, 1), (32, 3), (47, 1), (9, 1), (32, 1), (47, 1), (32, 1), (47, 1), (9, 1), (32, 5), (92, 1), (32, 1), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 4), (32, 6), (92, 1), (32, 3), (92, 1), (32, 1), (47, 1), (9, 1), (47, 1), (32, 2), (124, 1), (32, 2), (95, 5), (32, 2), (124, 1), (32, 1), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 4), (32, 7), (92, 1), (32, 7), (47, 1), (32, 3), (124, 1), (32, 1), (124, 1), (9, 1), (32, 3), (124, 1), (32, 1), (124, 1), (32, 1), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 5), (92, 1), (32, 5), (47, 1), (32, 4), (124, 1), (32, 1), (124, 1), (9, 1), (32, 3), (124, 1), (32, 1), (124, 1), (32, 1), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 5), (32, 1), (124, 1), (32, 3), (124, 1), (9, 1), (32, 3), (124, 1), (32, 1), (124, 1), (9, 1), (32, 3), (124, 1), (32, 1), (124, 1), (32, 1), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 5), (32, 1), (124, 1), (32, 3), (124, 1), (9, 1), (32, 3), (124, 1), (32, 1), (124, 1), (95, 5), (124, 1), (32, 1), (124, 1), (32, 1), (124, 1), (32, 3), (124, 1), (95, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 5), (32, 1), (124, 1), (32, 3), (124, 1), (9, 1), (32, 3), (124, 1), (9, 1), (32, 5), (124, 1), (32, 1), (124, 1), (9, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 5), (32, 1), (124, 1), (95, 3), (124, 1), (9, 1), (32, 3), (92, 1), (95, 9), (47, 1), (32, 1), (92, 1), (95, 11), (47, 1), ('RL', 1), 
    
    ]
    for i in foo:
        (ch,cnt) = i
        if ch == "RL":
            sys.stdout.write("\n")
        else:
            sys.stdout.write(chr(ch)*cnt)

    Comments on this post

    • b49P23TIvg agrees : I don't need to run that.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by Scorpions4ever
    Sigh, ok maybe I'll let him have some code to draw something this one time:
    Code:
    #/usr/bin/python
    import sys
    
    drawingInstructions = """r 200 200 100 80
    l 235 200 220 180
    l 235 200 240 175
    l 245 200 270 180
    r 220 220 20 20
    r 260 220 20 20
    r 240 260 30 10"""
    
    foo = [
    (32, 4), (95, 8), (32, 3), (95, 2), (32, 4), (95, 2), (32, 4), (95, 7), (32, 3), (95, 2), (32, 4), (95, 2), ('RL', 1), 
    (32, 3), (124, 1), (9, 1), (32, 4), (124, 1), (32, 1), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (47, 1), (9, 1), (32, 2), (124, 1), (32, 1), (124, 1), (32, 2), (124, 1), (32, 2), (47, 1), (32, 2), (47, 1), ('RL', 1), 
    (32, 3), (124, 1), (32, 2), (45, 6), (39, 1), (32, 1), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 3), (95, 4), (124, 1), (32, 1), (124, 1), (32, 2), (124, 1), (32, 1), (47, 1), (32, 2), (47, 1), ('RL', 1), 
    (32, 3), (124, 1), (32, 2), (124, 1), (95, 3), (32, 4), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (9, 1), (32, 4), (124, 1), (32, 2), (124, 1), (47, 1), (32, 2), (47, 1), ('RL', 1), 
    (32, 3), (124, 1), (32, 3), (95, 3), (124, 1), (32, 3), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (9, 1), (32, 4), (124, 1), (9, 1), (32, 2), (47, 1), ('RL', 1), 
    (32, 3), (124, 1), (32, 2), (124, 1), (32, 7), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (32, 2), (124, 1), (95, 4), (32, 2), (124, 1), (9, 1), (32, 2), (92, 1), ('RL', 1), 
    (32, 3), (124, 1), (32, 2), (124, 1), (32, 7), (124, 1), (32, 3), (45, 2), (32, 3), (124, 1), (32, 2), (124, 1), (9, 1), (32, 2), (124, 1), (32, 1), (124, 1), (32, 2), (124, 1), (92, 1), (32, 2), (92, 1), ('RL', 1), 
    (32, 3), (124, 1), (95, 2), (124, 1), (32, 7), (92, 1), (95, 8), (47, 1), (32, 3), (92, 1), (95, 6), (124, 1), (32, 1), (124, 1), (95, 2), (124, 1), (32, 1), (92, 1), (95, 2), (92, 1), ('RL', 1), 
    ('RL', 1), 
    (9, 4), (32, 5), (95, 5), ('RL', 1), 
    (9, 4), (32, 4), (124, 2), (9, 1), (32, 1), (124, 2), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (92, 1), (95, 3), (47, 1), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 4), (32, 4), (124, 1), (9, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 3), (32, 7), (95, 5), (124, 1), (60, 1), (45, 3), (62, 1), (124, 1), (95, 5), ('RL', 1), 
    (9, 3), (32, 6), (47, 1), (32, 5), (124, 1), (9, 1), (32, 2), (124, 1), (9, 1), (92, 1), ('RL', 1), 
    (9, 3), (32, 1), (47, 1), (32, 4), (124, 1), (32, 5), (124, 1), (9, 1), (32, 2), (124, 1), (9, 1), (124, 1), (32, 1), (92, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (32, 4), (124, 1), (32, 5), (124, 1), (9, 1), (32, 2), (124, 1), (9, 1), (124, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (32, 4), (124, 1), (32, 5), (124, 1), (9, 1), (32, 2), (124, 1), (9, 1), (124, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (9, 3), (124, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (9, 3), (124, 1), (32, 2), (124, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (9, 3), (32, 2), (47, 1), ('RL', 1), 
    (9, 3), (32, 1), (124, 1), (9, 3), (32, 1), (47, 1), ('RL', 1), 
    (9, 3), (32, 2), (92, 1), (9, 2), (32, 7), (47, 1), ('RL', 1), 
    (9, 3), (32, 3), (92, 1), (9, 2), (32, 6), (47, 1), ('RL', 1), 
    (9, 3), (32, 4), (124, 1), (9, 2), (32, 6), (124, 1), ('RL', 1), 
    (9, 3), (32, 4), (124, 1), (9, 2), (32, 6), (124, 1), ('RL', 1), 
    ('RL', 1), 
    (9, 4), (32, 5), (95, 4), (32, 5), (95, 4), (32, 2), (95, 9), (9, 1), (95, 3), (9, 1), (95, 3), ('RL', 1), 
    (9, 4), (32, 5), (92, 1), (9, 1), (32, 1), (92, 1), (32, 3), (47, 1), (9, 1), (32, 1), (47, 1), (32, 1), (47, 1), (9, 1), (32, 5), (92, 1), (32, 1), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 4), (32, 6), (92, 1), (32, 3), (92, 1), (32, 1), (47, 1), (9, 1), (47, 1), (32, 2), (124, 1), (32, 2), (95, 5), (32, 2), (124, 1), (32, 1), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 4), (32, 7), (92, 1), (32, 7), (47, 1), (32, 3), (124, 1), (32, 1), (124, 1), (9, 1), (32, 3), (124, 1), (32, 1), (124, 1), (32, 1), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 5), (92, 1), (32, 5), (47, 1), (32, 4), (124, 1), (32, 1), (124, 1), (9, 1), (32, 3), (124, 1), (32, 1), (124, 1), (32, 1), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 5), (32, 1), (124, 1), (32, 3), (124, 1), (9, 1), (32, 3), (124, 1), (32, 1), (124, 1), (9, 1), (32, 3), (124, 1), (32, 1), (124, 1), (32, 1), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 5), (32, 1), (124, 1), (32, 3), (124, 1), (9, 1), (32, 3), (124, 1), (32, 1), (124, 1), (95, 5), (124, 1), (32, 1), (124, 1), (32, 1), (124, 1), (32, 3), (124, 1), (95, 3), (124, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 5), (32, 1), (124, 1), (32, 3), (124, 1), (9, 1), (32, 3), (124, 1), (9, 1), (32, 5), (124, 1), (32, 1), (124, 1), (9, 1), (32, 3), (124, 1), ('RL', 1), 
    (9, 5), (32, 1), (124, 1), (95, 3), (124, 1), (9, 1), (32, 3), (92, 1), (95, 9), (47, 1), (32, 1), (92, 1), (95, 11), (47, 1), ('RL', 1), 
    
    ]
    for i in foo:
        (ch,cnt) = i
        if ch == "RL":
            sys.stdout.write("\n")
        else:
            sys.stdout.write(chr(ch)*cnt)
    can u show me another way? i dont get this
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Did you actually run his code? I think you will understand the meaning of it if you do.

    But seriously you have neither given enough information nor shown enough effort for any one to want to help you. That is the lesson here.

    -Mek
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    After alot of research and effort, i managed to do this:

    Code:
    def draw():
      pic=makeEmptyPicture(650,550) 
      
      Newstring=contents.split("\n")
      
      for element in Newstring:
        ch = element.split()
        task = ch[0]
        
        startX = int(ch[1])                            
        startY = int(ch[2])                           
        
        endX = int(ch[3])                              
        endY = int(ch[4])                             
      
        for ch in element: 
          if task == "l":                                    
            addLine( pic, startX, startY, endX, endY)
          elif task == "r":                                  
            addRect( pic, startX, startY, endX, endY)
        show (pic)
      
    
    programfile=pickAFile()
    if programfile.endswith("txt"):
      file=open(programfile,"rt")
      contents=file.read()
      draw()
    else:
      showError("Open a file that ends with .txt")

    This program is supposed to let the user pick a file and then the program reads the instructions in the file and then draw the graphic based on the instructions. One problem i need help with is it always points to an error on this line:

    Code:
      startX = int(ch[1])
    it says the following:
    'The error was: =
    Inappropriate argument value (of correct type).
    An error occurred attempting to pass an argument to a function.
    Please check line 12 of /Users/KhalidEltayeb/Desktop/Python/Assignment 7/doGraphics_ReadFromFile.py'


    I need it so that it reads the file i attached and then draw the graphic, please help.
    Attached Files

IMN logo majestic logo threadwatch logo seochat tools logo