Wc3C.net IsUnitOnRect
 Register Rules Get Hosted! Chat Pastebin FAQ and Rules Members List Calendar

04-12-2009, 03:43 PM   #1
Vexorian
Free Software Terrorist

Technical Director

Join Date: Apr 2003
Posts: 14,898

Submissions (37)

IsUnitOnRect

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
```
__________________
 Wc3 map optimizer 5.0 Someone should fix .wav sound in this thing. JassHelper 0.A.2.A Turns your simple code into something that is complicated enough to work.
 Wc3 map optimizer 5.0 JassHelper 0.A.2.A Xye 0.12.1 | Editor/Levels xe0.9 CS16.0 My spells jEdit modes for vJass&Zinc (v9) WarCiTy 0.2.0
Faster != more useful

 04-12-2009, 03:51 PM #2 Troll-Brain User   Join Date: Oct 2006 Posts: 1,490 Submissions (1) Why four times : JASS:`exitwhen ( IsUnitInRangeOfSegment(u, nx,ny, nx,my, 0) )`
04-12-2009, 03:57 PM   #3
Vexorian
Free Software Terrorist

Technical Director

Join Date: Apr 2003
Posts: 14,898

Submissions (37)

take a closer look.
__________________
 Wc3 map optimizer 5.0 Someone should fix .wav sound in this thing. JassHelper 0.A.2.A Turns your simple code into something that is complicated enough to work.
 Wc3 map optimizer 5.0 JassHelper 0.A.2.A Xye 0.12.1 | Editor/Levels xe0.9 CS16.0 My spells jEdit modes for vJass&Zinc (v9) WarCiTy 0.2.0
Faster != more useful

 04-12-2009, 04:06 PM #4 fX_ User     Join Date: Jan 2007 Posts: 528 Submissions (2) 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 User   Join Date: Oct 2006 Posts: 1,490 Submissions (1) ok my bad, variables' name are nearly the same :p
04-12-2009, 04:14 PM   #6
Vexorian
Free Software Terrorist

Technical Director

Join Date: Apr 2003
Posts: 14,898

Submissions (37)

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.
__________________
 Wc3 map optimizer 5.0 Someone should fix .wav sound in this thing. JassHelper 0.A.2.A Turns your simple code into something that is complicated enough to work.
 Wc3 map optimizer 5.0 JassHelper 0.A.2.A Xye 0.12.1 | Editor/Levels xe0.9 CS16.0 My spells jEdit modes for vJass&Zinc (v9) WarCiTy 0.2.0
Faster != more useful

 04-12-2009, 06:42 PM #7 Rising_Dusk Obscurity, the Art Projects DirectorProject Leader: OD   Join Date: Feb 2006 Posts: 9,729 Submissions (27) 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
User

Join Date: Jul 2006
Posts: 57

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:

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
Free Software Terrorist

Technical Director

Join Date: Apr 2003
Posts: 14,898

Submissions (37)

... 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.
__________________
 Wc3 map optimizer 5.0 Someone should fix .wav sound in this thing. JassHelper 0.A.2.A Turns your simple code into something that is complicated enough to work.
 Wc3 map optimizer 5.0 JassHelper 0.A.2.A Xye 0.12.1 | Editor/Levels xe0.9 CS16.0 My spells jEdit modes for vJass&Zinc (v9) WarCiTy 0.2.0
Faster != more useful

 04-12-2009, 10:15 PM #10 Rising_Dusk Obscurity, the Art Projects DirectorProject Leader: OD   Join Date: Feb 2006 Posts: 9,729 Submissions (27) 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
Free Software Terrorist

Technical Director

Join Date: Apr 2003
Posts: 14,898

Submissions (37)

probably.
__________________
 Wc3 map optimizer 5.0 Someone should fix .wav sound in this thing. JassHelper 0.A.2.A Turns your simple code into something that is complicated enough to work.
 Wc3 map optimizer 5.0 JassHelper 0.A.2.A Xye 0.12.1 | Editor/Levels xe0.9 CS16.0 My spells jEdit modes for vJass&Zinc (v9) WarCiTy 0.2.0
Faster != more useful

04-12-2009, 10:54 PM   #12
Rising_Dusk
Obscurity, the Art

Projects Director

Join Date: Feb 2006
Posts: 9,729

Submissions (27)

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
Free Software Terrorist

Technical Director

Join Date: Apr 2003
Posts: 14,898

Submissions (37)

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.
__________________
 Wc3 map optimizer 5.0 Someone should fix .wav sound in this thing. JassHelper 0.A.2.A Turns your simple code into something that is complicated enough to work.
 Wc3 map optimizer 5.0 JassHelper 0.A.2.A Xye 0.12.1 | Editor/Levels xe0.9 CS16.0 My spells jEdit modes for vJass&Zinc (v9) WarCiTy 0.2.0
Faster != more useful

 04-12-2009, 10:57 PM #14 Rising_Dusk Obscurity, the Art Projects DirectorProject Leader: OD   Join Date: Feb 2006 Posts: 9,729 Submissions (27) 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 User     Join Date: Feb 2009 Posts: 109 Submissions (1) 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. __________________ " YOU RAGE, YOU LOSE! "