Computer Science Canada

Is there a way to repeatedly draw a pic using 1 or 2 lines?

Author:  KONjbnj [ Thu May 26, 2005 11:01 am ]
Post subject:  Is there a way to repeatedly draw a pic using 1 or 2 lines?

I want to have this

code:
    Pic.Draw (line, 200, 450, picMerge)
    Pic.Draw (line, 250, 450, picMerge)
    Pic.Draw (line, 300, 450, picMerge)
    Pic.Draw (line, 350, 450, picMerge)
    Pic.Draw (line, 400, 450, picMerge)
    Pic.Draw (line, 450, 450, picMerge)
    Pic.Draw (line, 500, 450, picMerge)
    Pic.Draw (line, 300, 400, picMerge)
    Pic.Draw (line, 350, 400, picMerge)
    Pic.Draw (line, 400, 400, picMerge)
    Pic.Draw (line, 450, 400, picMerge)
    Pic.Draw (line, 500, 400, picMerge)


But I would need to do that many other times. I realize that it would take a lot of lines to do, and I was wondering if there waas a way to do that but in fewer lines.

Author:  Cervantes [ Thu May 26, 2005 12:17 pm ]
Post subject: 

Yep, for loops. Check the tutorial section if you dont' know them. There's a link to the tutorial in the sticky at the top of the turing tutorials thread. Here's how:
code:

for decreasing y : 450 .. 400 by 50
    for x : 200 .. 500 by 50
        Pic.Draw (line, x, y, picMerge
    end for
end for

Actually, after looking closer at your code I see that doesn't exactly fit the requirements. This would draw an extra two lines at y = 400. To prevent the ones at (200, 400) & (250, 400) from being drawn, you could use an if statement.
code:

for decreasing y : 450 .. 400 by 50
    for x : 200 .. 500 by 50
        if y = 400 and x >= 300 then
            Pic.Draw (line, x, y, picMerge
        end if
    end for
end for

However you do this, look for patterns in your code. Sometimes there is no pattern, sometimes there's a semi-pattern, as is the case here. Whenever there is a pattern, there is an easier way of doing it then just writing every coordinate.


: