
-----------------------------------
TheXploder
Mon Jan 26, 2004 11:51 am

Snap To Grid and Draw...
-----------------------------------
I'm making a Paint like program and I got a box that moves along with your mouse as you move it, and it snaps to the grid pattern. But when you zoom in or zoom out the box doesn't fit to the grid anymore its offset.

     
mousex := round (mousex / Zoom) * Zoom
mousey := round (mousey / Zoom) * Zoom
                    
drawfillbox (mousex - round (Zoom / 2), mousey - round (Zoom / 2), mousex + Zoom - round (Zoom / 2), mousey + Zoom - round (Zoom / 2), 1)


*mousex, and mousey are the positions of the mouse and Zoom is a variable that can be anywhere from 3 to 14.

I'm guessing that 'round' makes it not exact as the zoom value increases, yet I want it to be in an integer value. Some values do fit, but I'm guessing the odd ones don't, how do I change that?...  :cry:

-----------------------------------
McKenzie
Mon Jan 26, 2004 12:00 pm

Re: Snap To Grid...
-----------------------------------

     
mousex := round (mousex / Zoom) * Zoom
mousey := round (mousey / Zoom) * Zoom


     
mousex := mousex * Zoom div Zoom 
mousey := mousey * Zoom div Zoom

do the mult first to reduce roundoff error

-----------------------------------
TheXploder
Mon Jan 26, 2004 12:12 pm


-----------------------------------
hmm that doesn't work, now it's just following the exact position of the mouse, I want it to snap each Zoom value of pixels...

-----------------------------------
TheXploder
Mon Jan 26, 2004 12:26 pm


-----------------------------------
here is an example what I'm getting at...


View.Set ("offscreenonly")

var mousex, mousey, button, left, middle, right : int

var WindowGridXsize, WindowGridYsize : int
var WindowWidth, WindowHeight : int
var windowX, windowY : int
var BackroundClr : int

%CHANGE THIS VALUE TO ZOOM
var Zoom : int := 5

WindowGridXsize := Zoom
WindowGridYsize := Zoom
WindowWidth := 100
WindowHeight := 100
windowX := 0
windowY := 0

BackroundClr := gray

proc objDrawWindow (x, y, foregroundClr, gridClr, backgoundClr : int, grid : string)
    drawbox (x, y, x + WindowGridXsize * WindowWidth, y + WindowGridYsize * WindowHeight, foregroundClr)
    drawfillbox (x + 1, y + 1, x + (WindowGridXsize * WindowWidth) - 1, y + (WindowGridYsize * WindowHeight) - 1, backgoundClr)
    if grid = "enabled" then
        for i : 1 .. WindowHeight
            drawline (x, y + i * WindowGridYsize, x + WindowGridXsize * WindowWidth, y + i * WindowGridYsize, gridClr)
        end for
        for i : 1 .. WindowWidth
            drawline (x + i * WindowGridXsize, y, x + i * WindowGridXsize, y + WindowGridYsize * WindowHeight, gridClr)
        end for
    end if
end objDrawWindow

proc objBox (x, y, clr : int)
    mousex := round (mousex / Zoom) * Zoom
    mousey := round (mousey / Zoom) * Zoom
    drawfillbox (mousex - round (Zoom / 2), mousey - round (Zoom / 2), mousex + Zoom - round (Zoom / 2), mousey + Zoom - round (Zoom / 2), 1)
end objBox

loop
    Mouse.Where (mousex, mousey, button)
    objDrawWindow (windowX, windowY, black, black, gray, "enabled")
    objBox (mousex, mousey, 1)
    delay (10)
    View.Update
end loop


* Try changing the zoom values...

If you change this:

proc objBox (x, y, clr : int)
    mousex := round (mousex / Zoom) * Zoom
    mousey := round (mousey / Zoom) * Zoom
    drawfillbox (mousex - round (Zoom / 2), mousey - round (Zoom / 2), mousex + Zoom - round (Zoom / 2), mousey + Zoom - round (Zoom / 2), 1)
end objBox

To that it works better but the position of the mouse doesn't fit:


proc objBox (x, y, clr : int)
    mousex := round (mousex / Zoom) * Zoom
    mousey := round (mousey / Zoom) * Zoom
    drawfillbox (mousex, mousey, mousex + Zoom, mousey + Zoom, 1)
end objBox


-----------------------------------
McKenzie
Mon Jan 26, 2004 12:51 pm


-----------------------------------
:oops:  sorry
proc objBox (x, y, clr : int) 
    mousex := mousex div Zoom * Zoom 
    mousey := mousey div Zoom * Zoom 
    drawfillbox (mousex+1 , mousey+1 , mousex + Zoom -1, mousey +Zoom -1, 1) 
end objBox 

-----------------------------------
TheXploder
Mon Jan 26, 2004 1:00 pm


-----------------------------------
Thx a lot... :D

I ow you one... :)

-----------------------------------
TheXploder
Mon Jan 26, 2004 3:26 pm


-----------------------------------
Another problem came up so thats why I'll just change the topics name...
This time when you press down the left mouse button I want it to draw boxes, but that doesn't work, it just moves along...

* I'll give some bits for a solution... :) 

View.Set ("offscreenonly")

var mousex, mousey, button, left, middle, right : int

var WindowGridXsize, WindowGridYsize : int
var WindowWidth, WindowHeight : int
var windowX, windowY : int
var BackroundClr : int

%CHANGE THIS VALUE TO ZOOM
var Zoom : int := 10

WindowGridXsize := Zoom
WindowGridYsize := Zoom
WindowWidth := 100
WindowHeight := 100
windowX := 0
windowY := 0

BackroundClr := gray

proc objDrawWindow (x, y, foregroundClr, gridClr, backgoundClr : int, grid : string)
    drawbox (x, y, x + WindowGridXsize * WindowWidth, y + WindowGridYsize * WindowHeight, foregroundClr)
    drawfillbox (x + 1, y + 1, x + (WindowGridXsize * WindowWidth) - 1, y + (WindowGridYsize * WindowHeight) - 1, backgoundClr)
    if grid = "enabled" then
        for i : 1 .. WindowHeight
            drawline (x, y + i * WindowGridYsize, x + WindowGridXsize * WindowWidth, y + i * WindowGridYsize, gridClr)
        end for
        for i : 1 .. WindowWidth
            drawline (x + i * WindowGridXsize, y, x + i * WindowGridXsize, y + WindowGridYsize * WindowHeight, gridClr)
        end for
    end if
end objDrawWindow

proc objBox (x, y, clr : int)
    mousex := mousex div Zoom * Zoom
    mousey := mousey div Zoom * Zoom
    drawfillbox (mousex, mousey, mousex + Zoom, mousey + Zoom, 1)
end objBox

loop
    Mouse.Where (mousex, mousey, button)

    left := button mod 10                           % left = 0 or 1
    middle := (button - left) mod 100         % middle = 0 or 10
    right := button - middle - left         % right = 0 or 100

    objDrawWindow (windowX, windowY, black, black, gray, "enabled")

    if left = 1 then
        locate (1, 1)
        objBox (mousex, mousey, 1)
        put "Key Pressed"
    end if
    delay (10)
    View.Update
end loop

-----------------------------------
McKenzie
Mon Jan 26, 2004 5:41 pm


-----------------------------------
objDrawWindow (windowX, windowY, black, black, gray, "enabled") 
loop 
    Mouse.Where (mousex, mousey, button) 

    left := button mod 10                           % left = 0 or 1 
    middle := (button - left) mod 100         % middle = 0 or 10 
    right := button - middle - left         % right = 0 or 100 


    if left = 1 then 
        locate (1, 1) 
        objBox (mousex, mousey, 1) 
    end if 
    delay (10) 
    View.Update 
end loop

All too easy... (oops thats Vader not Yoda)

-----------------------------------
thoughtful
Mon Jan 26, 2004 5:48 pm


-----------------------------------
Okay here, you had the code done pretty good, the only problem was that you proc objDrawWindow was actually coloring the whole field grey and it did that every time the loop executed, thts why it only showed u one box. Here i fixed it up for u, and you can keep the bits, i am always glad to help  :D 


View.Set ("offscreenonly")

var mousex, mousey, button, left, middle, right : int

var WindowGridXsize, WindowGridYsize : int
var WindowWidth, WindowHeight : int
var windowX, windowY : int
var BackroundClr : int

%CHANGE THIS VALUE TO ZOOM
var Zoom : int := 10

WindowGridXsize := Zoom
WindowGridYsize := Zoom
WindowWidth := 100
WindowHeight := 100
windowX := 0
windowY := 0

BackroundClr := gray

proc objDrawWindow (x, y, foregroundClr, gridClr, backgoundClr : int, grid : string)
    drawbox (x, y, x + WindowGridXsize * WindowWidth, y + WindowGridYsize * WindowHeight, foregroundClr)
    %drawfillbox (x + 1, y + 1, x + (WindowGridXsize * WindowWidth) - 1, y + (WindowGridYsize * WindowHeight) - 1, backgoundClr)
    if grid = "enabled" then
        for i : 1 .. WindowHeight
            drawline (x, y + i * WindowGridYsize, x + WindowGridXsize * WindowWidth, y + i * WindowGridYsize, gridClr)
        end for
        for i : 1 .. WindowWidth
            drawline (x + i * WindowGridXsize, y, x + i * WindowGridXsize, y + WindowGridYsize * WindowHeight, gridClr)
        end for
    end if
end objDrawWindow

proc objBox (x, y, clr : int)
    mousex := mousex div Zoom * Zoom
    mousey := mousey div Zoom * Zoom
    drawfillbox (mousex, mousey, mousex + Zoom, mousey + Zoom, 1)
end objBox

drawfillbox(0,0,maxx,maxy,BackroundClr)% i added this
loop
    Mouse.Where (mousex, mousey, button)

    left := button mod 10                           % left = 0 or 1
    middle := (button - left) mod 100         % middle = 0 or 10
    right := button - middle - left         % right = 0 or 100

    objDrawWindow (windowX, windowY, black, black, gray, "enabled")
    

    if left = 1 then
        locate (1, 1)
        objBox (mousex, mousey, 1)
        put "Key Pressed"
    end if
    delay (10)
    View.Update
   
end loop


-----------------------------------
the_short1
Mon Jan 26, 2004 5:50 pm


-----------------------------------
Can someone post the Final of this with all. corrections as a .t file...????PLease and Thankyou

-----------------------------------
McKenzie
Mon Jan 26, 2004 5:58 pm


-----------------------------------
thoughtful and I have the same solution, thoughtful was just more well...thoughtful about it so he didnt know that I already posted. He has the full solution in his post. (Hmmm, thoughtful, if yer not a "him" let me know I'll edit)

-----------------------------------
DanShadow
Mon Jan 26, 2004 9:19 pm


-----------------------------------
Maybe instead of all the complexity, use a 1D grid:

var grid : array 1 .. 10 of int
for i : 1 .. 10
    grid (i) := i * 25
end for
proc drawGrid
    for j : 1 .. 10
        for i : 1 .. 10
            Draw.Box (grid (i) - 25, grid (j) - 25, grid (i) + 25, grid (j) + 25, 255)
        end for
    end for
end drawGrid

drawGrid

Then to draw...do this:

for j:1..10
for i:1..10
if button=1 and mousex>gridx(j)-25 and mousexgridy(i)-25 and mousey