HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

IsUnitOnRect

04-12-2009, 03:43 PM#1
Vexorian
Collapse JASS:
library IsUnitOnRect
//**
//* IsUnitOnRect returns true if the unit's collision circle
//* ------------ interesects with a rect.
//*              
//*              Useful for example, in a "enter rect" event
//*              it will return true, unlike blizz' RectContainsUnit
//*
//* probably slower than RectContainsUnit
//*
//*******************************************************************

//=================================================================
    function IsUnitOnRect takes unit u, rect r returns boolean
     local real x =GetUnitX(u)
     local real y =GetUnitY(u)
     local real mx = GetRectMaxX(r)
     local real nx = GetRectMinX(r)
     local real my = GetRectMaxY(r)
     local real ny = GetRectMinY(r)
        if (nx <= x) and (x <= mx) and (ny <= y) and (y <= my) then
            return true
        endif
        if(x>mx) then
            set x=mx
        elseif(x<nx) then
            set x=nx
        endif
        if(y>my) then
            set y=my
        elseif(y<ny) then
            set y=ny
        endif
        return IsUnitInRangeXY(u,x,y,0.0)
    endfunction


endlibrary
04-12-2009, 03:51 PM#2
Troll-Brain
Why four times :
Collapse JASS:
exitwhen ( IsUnitInRangeOfSegment(u, nx,ny, nx,my, 0) )
04-12-2009, 03:57 PM#3
Vexorian
take a closer look.
04-12-2009, 04:06 PM#4
fX_
y not use 'if or or or or or' instead of loop. i think or evaluates each condition 1 by 1 and terminates evaluation upon finding a true condition; and it is more 'to the point'

is it that u just wanted it the conditions to be enumerated more clearly?
04-12-2009, 04:10 PM#5
Troll-Brain
ok my bad, variables' name are nearly the same :p
04-12-2009, 04:14 PM#6
Vexorian
Quote:
Originally Posted by fX_
y not use 'if or or or or or' instead of loop. i think or evaluates each condition 1 by 1 and terminates evaluation upon finding a true condition; and it is more 'to the point'

is it that u just wanted it the conditions to be enumerated more clearly?
The long line required by a bunch of ors would give me nightmares, and a bunch of if-then-elses would make more lines, so I used this.

Edit: code got shorter.
04-12-2009, 06:42 PM#7
Rising_Dusk
That's a clever way to mesh 4 checks together without a bunch of ugly if/thens.
04-12-2009, 08:25 PM#8
peq
Quote:
Originally Posted by Rising_Dusk
That's a clever way to mesh 4 checks together without a bunch of ugly if/thens.

imho using a loop which will never loop is not clever. loops are ment to loop

and i think it is useless to use IsUnitInRangeOfSegment for checking if a unit is in a rect. I think this does the same:

Collapse JASS:
function IsUnitAtRect takes unit u, rect r returns boolean
    local real x =GetUnitX(u)
    local real y =GetUnitY(u)
    local real maxx = GetRectMaxX(r)
    local real minx = GetRectMinX(r)
    local real maxy = GetRectMaxY(r)
    local real miny = GetRectMinY(r)
    if x > maxx then
        set x = maxx
    elseif x < minx then
        set x = minx
    endif
    if y > maxy then
        set y = maxy
    elseif y < miny then
        set y = miny
    endif
    return IsUnitInRange(u, x, y, 0.)    
endfunction
04-12-2009, 09:22 PM#9
Vexorian
... No, it isn't even close to doing the same.

Edit: darn I forgot rects are always paralel to the x and y axis.

Edit: Updated.
04-12-2009, 10:15 PM#10
Rising_Dusk
Vex, you should probably remove the library requirement of IsUnitInRangeOfSegment() since you don't use it anymore.
04-12-2009, 10:30 PM#11
Vexorian
probably.
04-12-2009, 10:54 PM#12
Rising_Dusk
Quote:
Originally Posted by Vexorian
Requires: IsUnitInRangeOfSegment
This shouldn't be in the first post anymore. Also, I think the function name IsUnitInRect() would flow better with WC3 naming conventions.
04-12-2009, 10:56 PM#13
Vexorian
Technically the unit is not necessarily in the rect, half of its collision circle could be outside. Not sure if At is a much better choice though.
04-12-2009, 10:57 PM#14
Rising_Dusk
Well, I think 'in' makes more sense intuitively, even if it doesn't necessarily describe it perfectly.
04-13-2009, 04:08 AM#15
FriendlyPsycho
IsUnitOnRect() probably? I was thinking of circles when you and Rising_Dusk were having the name discussion. In = inside the radius of the circle, On = exactly at the radius of the circle.