Midterm Exam 2 Review Spring 2003: Graphics from a List
Comments? Concerns? Answers? Questions? Questions about Answers?
Back to Sp2003 Midterm Review 2
I don't even know where to start with this.
I can't tell if you are using Ls or 1s when differentiating the second type of strings. Which is which?
def doGraphics(mylist):
canvas = makePicture(r"\mediasources\640x480.jpg")
for j in mylist:
for i in string:
if i[0] == "b":
x = int (i[2:4])
y = int (i[6:8])
setColor(getPixel(canvas, x,y),black)
if i[0] =="l":
x1 = int(i[2:4])
y1 = int(i[6:8])
x2 = int(i[10:12])
y2 = int(i[14:16])
addLine(canvas, x1, y1, x2, y2)
return canvas
Is this right at all? Anyone? Katie Graybeal
Katie, I changed your numbers because in the notes it said that the brackets meant up to but not including. For example for a list
["9 8 736"], [2:6] would return '8 73'. At least I think that's what the notes were saying. Anyway, This is what I got. Although I haven't looked at the canvas to check. by the way, how do you do that?
def doGraphics(mylist):
canvas = makePicture(r"C:\Documents and Settings\Lauren Biddle\My Documents\mediasources\640x480.jpg")
for j in mylist:
for i in mylist:
if i[0] == "b":
x = int(i[2:5])
y = int(i[6:9])
setColor(getPixel(canvas, x,y),black)
if i[0] =="l":
x1 = int(i[2:5])
y1 = int(i[6:9])
x2 = int(i[10:13])
y2 = int(i[14:17])
addLine(canvas, x1, y1, x2, y2)
return canvas
To try this out, use this program and for the input list put ["b 200 120"] or whatever.
def tryit(list):
for i in list:
if i[0] == "b":
x=int(i[2:5])
print x
| Lauren, why do you have two loops? Try just dropping the "j" loop. You might also try putting in print statements to see what's going on. Like the below. Mark Guzdial |
def doGraphics(mylist):
canvas = makePicture(getMediaPath("640x480.jpg"))
for i in mylist:
if i[0] == "b":
x = int(i[2:5])
y = int(i[6:9])
print "Drawing pixel at ",x,":",y
setColor(getPixel(canvas, x,y),black)
if i[0] =="l":
x1 = int(i[2:5])
y1 = int(i[6:9])
x2 = int(i[10:13])
y2 = int(i[14:17])
print "Drawing line at",x1,y1,x2,y2
addLine(canvas, x1, y1, x2, y2)
return canvas
| BTW, do you realize what you're doing here? You're implementing Flash or Postscript! This is what an .swf or .ps (or even .pdf) file contains – a bunch of internal commands that get interpreted by a simple program to draw pictures. Mark Guzdial |
I thought you needed 2 loops because you won't just be entering [b 200 120] or something, you will be entering ["b 200 120", "l 000 010 100 200",...]. Why is this not right?
| You need one loop to walk the ELEMENTS of the list. You walk through the individual ELEMENTS using your i[2:5] etc. pieces. It's only a list of things – one-dimensional. Like a sound, not like a picture. Mark Guzdial |
Where are you getting the numbers that are in the bracket from? Please Help.
they come from where the number is in the string... for example, in the string for a dot, 200 is the 2-4th values in the string. you have to put 2:5 because the list notation only goes up to but not including the upper bound.
What does that mean in dumb people words? I am so confused.
Oh, do the list start with "0" in the parenthases... and then count from there?
yeah
so apparently I've just missed something but what is the i next to the brackets for?
Just out of pure curiosity, do we need to show the canvas on this one? To view the end result?
| The i next to the brackets is each string – look at the FOR loop. Yes, if you want to see the canvas at the end, show whatever doGraphics() returns. Mark Guzdial |
Link to this Page