Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Snap To Grid and Draw...
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TheXploder




PostPosted: Mon Jan 26, 2004 11:51 am   Post subject: 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.

code:
     
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?... Crying or Very sad
Sponsor
Sponsor
Sponsor
sponsor
McKenzie




PostPosted: Mon Jan 26, 2004 12:00 pm   Post subject: Re: Snap To Grid...

TheXploder wrote:

code:
     
mousex := round (mousex / Zoom) * Zoom
mousey := round (mousey / Zoom) * Zoom


code:
     
mousex := mousex * Zoom div Zoom
mousey := mousey * Zoom div Zoom

do the mult first to reduce roundoff error
TheXploder




PostPosted: Mon Jan 26, 2004 12:12 pm   Post subject: (No subject)

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




PostPosted: Mon Jan 26, 2004 12:26 pm   Post subject: (No subject)

here is an example what I'm getting at...

code:

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:

code:
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:

code:

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




PostPosted: Mon Jan 26, 2004 12:51 pm   Post subject: (No subject)

Embarassed sorry
code:
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




PostPosted: Mon Jan 26, 2004 1:00 pm   Post subject: (No subject)

Thx a lot... Very Happy

I ow you one... Smile
TheXploder




PostPosted: Mon Jan 26, 2004 3:26 pm   Post subject: (No subject)

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... Smile

code:
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




PostPosted: Mon Jan 26, 2004 5:41 pm   Post subject: (No subject)

code:
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)
Sponsor
Sponsor
Sponsor
sponsor
thoughtful




PostPosted: Mon Jan 26, 2004 5:48 pm   Post subject: (No subject)

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 Very Happy

code:

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




PostPosted: Mon Jan 26, 2004 5:50 pm   Post subject: (No subject)

Can someone post the Final of this with all. corrections as a .t file...????PLease and Thankyou
McKenzie




PostPosted: Mon Jan 26, 2004 5:58 pm   Post subject: (No subject)

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




PostPosted: Mon Jan 26, 2004 9:19 pm   Post subject: (No subject)

Maybe instead of all the complexity, use a 1D grid:
code:

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:
code:

for j:1..10
for i:1..10
if button=1 and mousex>gridx(j)-25 and mousex<gridx(j)+25 and mousey>gridy(i)-25 and mousey<gridy(i)+25 then
Draw.Fill(mousex,mousey,blue,black)
end if
end for
end for

or something similar to that. Hope this helps! Very Happy
TheXploder




PostPosted: Mon Jan 26, 2004 9:42 pm   Post subject: (No subject)

[edit] oops
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: