• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

IsUnitInLine with radius

Level 7
Joined
Jan 7, 2009
Messages
44
JASS:
function IsUnitInLine takes unit u, real x, real y, real x2, real y2, real radius returns boolean
    local real x3 = GetUnitX(u)
    local real y3 = GetUnitY(u)
    local real angle = bj_RADTODEG * Atan2(y2 - y, x2 - x)
    local real angle2 = bj_RADTODEG * Atan2(y3 - y, x3 - x)
    local real angle3 = angle2-angle
    local real xx = x3-x
    local real yy = y3-y
    local real dist = SquareRoot(xx * xx + yy * yy)
    local real yonline = dist * Sin(angle3 * bj_DEGTORAD)
    local real xonline = dist * Cos(angle3 * bj_DEGTORAD)
    local real xx2 = x2-x
    local real yy2 = y2-y
    local real linedist = SquareRoot(xx2 * xx2 + yy2 * yy2)
    return xonline > 0. and xonline < linedist and (yonline > -radius and yonline < radius)
endfunction

Example: IsUnitInLine(udg_ghoul[11], 0., 0., 500., 0., 128.)

Based on XY rotation.

Took me a while to figure this out, code doesn't look too complicated (i hope) but the creation progress was totally desperate. Hope this is acceptable.

For GetXYInLine it's already in the bounds of working like that, you only need do add and remove few things.

JASS:
function IsXYInLine takes real x3, real y3, real x, real y, real x2, real y2, real radius returns boolean
    local real angle = bj_RADTODEG * Atan2(y2 - y, x2 - x)
    local real angle2 = bj_RADTODEG * Atan2(y3 - y, x3 - x)
    local real angle3 = angle2-angle
    local real xx = x3-x
    local real yy = y3-y
    local real dist = SquareRoot(xx * xx + yy * yy)
    local real yonline = dist * Sin(angle3 * bj_DEGTORAD)
    local real xonline = dist * Cos(angle3 * bj_DEGTORAD)
    local real xx2 = x2-x
    local real yy2 = y2-y
    local real linedist = SquareRoot(xx2 * xx2 + yy2 * yy2)
    return xonline > 0. and xonline < linedist and (yonline > -radius and yonline < radius)
endfunction
 
Last edited:
Much more optimal functions already exist to do this and this illustrates your fundamental lack of knowledge about the your own math you employ here:

JASS:
    local real angle = bj_RADTODEG * Atan2(y2 - y, x2 - x)
    local real angle2 = bj_RADTODEG * Atan2(y3 - y, x3 - x)
    local real angle3 = angle2-angle
    local real yonline = dist * Sin(angle3 * bj_DEGTORAD)
    local real xonline = dist * Cos(angle3 * bj_DEGTORAD)
 
Top