

-----------------------------------

aum

Thu Jan 12, 2006 9:52 pm


help to make recursive grid

-----------------------------------

this is my first time on compsci forums...so here it goes

 from reading other topics, I know that i shouldnt be an idiot and ask for an aswer code or for someone to do my work and i wont. 

I tried many times to make a recursive grid in turing. (procedure)

but i just cant get the hang of it. I know it relates to making a sierpinskis triangle but i just cant figure it out. 

Here is my code so far..

setscreen ("graphics:max, max")
procedure grid (x1, y1, x2, y2, c : int)
    var x : int := 0
    var y : int := 0
    x := (x2 + x1) div 2
    y := (y2 + y1) div 2

    drawbox (x1, y1, x2, y2, c)

    end grid


Its almost nothing but i tried to add many things but it wont work and so i dont know the right step after this. 

If anyone could help please, id be thankful.


-----------------------------------

Cervantes

Thu Jan 12, 2006 10:52 pm



-----------------------------------

What do you mean by a "recursive grid"?  Are you trying to draw one?  Search one?


-----------------------------------

aum

Thu Jan 12, 2006 11:00 pm



-----------------------------------

i mean like we make a square and put 4 squares in there and then four squares in the new sqaures and so on. Im sorry if its confusing but im trying my best to expalin. Its like four grids in a box and four grids in the box in box. Its repetetive. I wish i could draw a picture to show u. I have to call the procedure inside the procedure itself which creates recursion or so i think. But i dont know how. I hope u understnad what im trying to say.


-----------------------------------

MysticVegeta

Fri Jan 13, 2006 3:29 pm



-----------------------------------

You keep on putting squares into new squares untill when?


-----------------------------------

Avarita

Fri Jan 13, 2006 4:32 pm



-----------------------------------

You mean something like this?


proc drawGrid (x1, y1, x2, y2, c : int)
    for i : 0 .. 1
        for j : 0 .. 1
            drawbox (x1 + j * abs (x2 - x1), y1 + i * abs (y2 - y1), +x2 + j * abs (x2 - x1), y2 + i * abs (y2 - y1), c)
        end for
    end for
    var newx2 := x2 div 2
    var newy2 := y2 div 2
    if newx2 > x1 and newy2 > y1 then
        drawGrid (x1, y1, newx2, newy2, black)
    end if
end drawGrid

drawGrid (10, 10, 400, 400, black)



-----------------------------------

Cervantes

Fri Jan 13, 2006 4:38 pm



-----------------------------------

Until you reach the Planck length. ;)

Perhaps until the squares width becomes less then one (pixel).

So, let's think about this.  You're going to need four parameters to specify the location of the rectangle.  You'll also need to know the number of squares you want to put inside the given square.  After drawing your lines, you'll call the procedure again, passing in updated positions to draw your boxes at, and keeping the same number of squares to draw (probably, though not necessary).

It seems like you don't know where to begin, since you didn't have any recursion in the example you posted.  Beyond what I said above, I will point you to the recursion tutorial, which will give you a good look at recursion.  You can find a link to it near the end of the Turing Walkthrough.


-----------------------------------

aum

Sat Jan 14, 2006 4:57 pm



-----------------------------------

thanks for helping me. I figured it out a bit by looking at what u said and just by drawing it out. 

take a look to see whther i have any flaws or not:

setscreen ("graphics:max, max")
procedure grid (x1, y1, size, c : int)

    drawbox (x1, y1, size + x1, size + y1, 7)

    if size > 3 then
        grid (x1 + (size div 2), y1, (size div 2), c)
        grid (x1 + (size div 2), y1 + (size div 2), size div 2, c)
        grid (x1, y1, size div 2, c)
        grid (x1, y1 + (size div 2), size div 2, c)
    end if
end grid


I think i did it right. 
Anyway thanks for the hints and help.

