Rectangle Problem
Cubist at aol.com
Cubist at aol.com
Wed Apr 6 05:10:08 EDT 2005
sez dburgun at dsl.pipex.com:
>Given I have a "BoundingRect" and "UserRect", how can I check that
>"UserRect" is inside "BoundingRect"? If it overlaps I want to either
>clip the rectangle or stop it from moving outside of the BoundingRect
>in the first place.
Well, a rect is a four-number list, right? If UserRect is wholly contained
within BoundingRect, the "left" value for UserRect will be equal to, or
greater than, the "left" value for BoundingRect; similarly, the "down" value for
UserRect will be less than, or equal to, the "down" value for BoundingRect; and
so on. Thus, what you want can be done by a function like this:
function ItsInside UserRect, BoundRect
if item 1 of UserRect => item 1 of BoundRect then return false
if item 2 of UserRect => item 2 of BoundRect then return false
if item 3 of UserRect <= item 3 of BoundRect then return false
if item 4 of UserRect <= item 4 of BoundRect then return false
return true
end ItsInside
If you wanted to, you could turn this handler's code into a one-liner by
combining the four comparisons, like so:
on ItsInside Ru, Rb
# mind the line-wrap!
return ((item 1 of Ru => item 1 of Rb) and (item 2 of Ru => item 2 of Rb)
and (item 3 of Ru <= item 3 of Rb) and (item 4 of Ru <= item 4 of Rb))
end ItsInside
Hope this helps...
More information about the use-livecode
mailing list