Snapping To A Grid of TopLefts?

Ali Lloyd ali at runrev.com
Sun Oct 19 14:17:00 EDT 2014


Hi John,
I use this handler to snap to a grid, there may well be clearer / more
efficient implementations though.

constant kHDist = 100 // horizontal distance between points
constant kVDist = 75 // vertical distance between points

on locToSnap @xLeft, @xTop
   local tTopOver, tLeftOver
   put (xLeft mod kHDist) into tLeftOver
   put (xTop mod kVDist) into tTopOver
   if tLeftOver > kHDist/2 then
      add kHDist to xLeft
   end if
   if tTopOver > kVDist/2 then
      add kVDist to xTop
   end if
   put (xLeft - tLeftOver) into xLeft
   put (xTop - tTopOver) into xTop
end locToSnap

Ali

On 19 October 2014 18:30, JOHN PATTEN <johnpatten at me.com> wrote:

> Hi All,
>
> I’m trying to figure out a way to have objects snap to a grid on the
> mouseUp. I have a list of TopLeft coordinates.
> 500,50
> 500,125
> 500,200
> 500,275
> 400,50
> 400,125 …
>
> I want the object to snap to the closest topLeft coordinate in the list
> upon a mouseUp.
>
> I was tried subtracting current x and y coordinate from each item in list,
> but that quickly was getting complicated.
>
> on mouseUp
>    put the topLeft of me into tMyLoc
>    put item 1 of tMyLoc into tXLoc
>    put item 2 of tMyLoc into tYLoc
>    put 1 into tItem1
>    put 1 into tItem2
>    put 1 into x
>    repeat for number of lines in cd fld 1
>       put item tItem1 of line x of cd fld 1 into tXTarget
>       put item tItem2 of line x of cd fld 1 into tYTarget
>       put the abs of (tXTarget - tXLoc) & "," after tCoords
>       put the abs of (tYTarget - tYLoc) & return after tCoords
>       add 1 to x
>    end repeat
>    put "My Current Loc:" && tMyLoc after tCoords
>    answer tCoords
> end mouseUp
>
> on MouseDown
>    Grab me
> end MouseDown
>
> Interesting it appears the smallest abs result of the subtraction seems to
> be the closes x coord listed. But that only gets me half way there. I
> thought I’d throw it out here for suggestions…?
>
> Thank you!
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list