Computer Science Canada

Help with a grid procedure

Author:  Danjen [ Fri Nov 24, 2006 12:14 pm ]
Post subject:  Help with a grid procedure

I'm trying to make a customizable grid program, that lets the user input the X, Y, X size, Y size, # of lines and color, but something keeps screwing up for some odd reason and the lines won't align properly. Basically, the first for statement works okay, but then I can't determine a formula for the second for statement. Sad

code:

proc drawGrid (x, y, xsize, ysize, xrep, yrep, col : int)

    for i : 1 .. xrep
        Draw.Line (x + (i - 1) * xsize, y - (i - 1) * ysize, x + (i - 1) * xsize + (xsize * xrep), y - (i - 1) * ysize + (ysize * yrep), col)
        put "(", x + (i - 1) * xsize, ",", y - (i - 1) * ysize, ") (", x + (i - 1) * xsize + (xsize * xrep), ",", y - (i - 1) * ysize + (ysize * yrep), ")"
    end for

    for i : 1 .. yrep
        Draw.Line (x + (i - 1) * xsize, y, x + (i - 1) * xsize + (yrep * xsize), 20, 7)
    end for

end drawGrid

drawGrid (100, 100, 45, 20, 5, 4, 7)

Author:  Windsurfer [ Sat Nov 25, 2006 6:54 pm ]
Post subject: 

Your problem comes from having no real "grid" but rather you just have a method of drawing diagonal lines... what you need to do is make a function that draws dots on the screen in your grid fashion... then you can figure out how to draw them. Then you will always be able to have any grid line up.


: