HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

IsUnitInTriangle

02-16-2007, 08:52 AM#1
PandaMine
Does anyone know on the top of their heads (or how to create) a function that determines if a unit is in a triangle and returns true if that is the case
i.e. the function takes 7 arguments, 1 being the unit and 6 being the 3 points with x's and y's

Collapse JASS:
i.e. IsUnitInTriangle takes unit u, real x1, real y1, real x2, real y2, real x3, real y3 returns boolean

I would also really like it if the funtions supports all types of triangles, even scalar (obtuse) ones.

Thanks
02-16-2007, 09:15 AM#2
Pyrogasm
Im pretty sure someone had a function that did it. In fact, I think it's in one of my maps.

Here it is, though it's rather lengthy and not exactly what you need. It's a function that adds units in a triangle to a group. For your purposes, you could check to see if that unit is in the group.

Collapse JASS:
//##Start##
//**************************************************************************************************
//*
//*  GrupAddUnitsInTriange (Polygonal madness)
//*
//*     To implement this function, you must select the contents of this trigger starting
//*   with the //##Start## and finishing with the //##End##, and copy that TO YOUR MAP's
//*   CUSTOM SCRIPT SECTION, (it is at the top of the trigger list in the trigger editor)
//*
//*     DON'T JUST COPY THE TRIGGER; copy this to the custom script section
//*
//*     (If you already have these functions in your map don't copy these ones)
//*
//**************************************************************************************************

//==================================================================================================
//
// For more JASS functions, visit : [url]http://www.wc3JASS.com[/url]
//
//==================================================================================================

//==================================================================================================
function GroupAddUnitsInTriangle_AngleCheck takes real angle, real angle1, real angle2, real PI2 returns boolean
 local real x
    if (angle1>angle2) then
        set x=angle1
        set angle1=angle2
        set angle2=x
    endif
    if (angle2-angle1>angle1 - (angle2-PI2)) then
        set angle2=angle2-PI2
        if (angle < bj_PI) then
            set angle=angle-PI2
        endif
        return (angle>=angle2) and (angle<=angle1)
    endif
 return (angle>=angle1) and (angle<=angle2)
endfunction

function GetRectThatLimitsTriangle takes real x1, real y1, real x2, real y2, real x3, real y3 returns rect
 local real maxx
 local real minx
    if (x1>x2) then
        if (x2>x3) then     //x1>x2>x3
            set maxx=x1
            set minx=x3
        elseif (x3>x1) then //x3>x1>x2
            set maxx=x3
            set minx=x2
        else                //x3>x1>x2
            set maxx=x1
            set minx=x2
        endif
    elseif (x1>x3) then //x2>x1>x3
        set maxx=x2
        set minx=x3
    elseif (x3>x2) then //x3>x2>x1 
        set maxx=x3
        set minx=x1
    else                //x2>x3>x1
        set maxx=x2
        set minx=x1
    endif

    if (y1>y2) then
        if (y2>y3) then     //y1>y2>y3
            return Rect(minx,y3,maxx,y1)
        elseif (y3>y1) then //y3>y1>y2
            return Rect(minx,y2,maxx,y3)
        else                //y3>y1>y2
            return Rect(minx,y2,maxx,y1)
        endif
    elseif (y1>y3) then //y2>y1>y3
        return Rect(minx,y3,maxx,y2)
    elseif (y3>y2) then //y3>y2>y1 
        return Rect(minx,y1,maxx,y3)
    endif
 //y2>y3>y1
 return Rect(minx,y1,maxx,y2)
endfunction



function GroupAddUnitsInTriangle takes group whichGroup, real x1, real y1, real x2, real y2, real x3, real y3 returns nothing
 local rect semi=GetRectThatLimitsTriangle(x1,y1,x2,y2,x3,y3)
 local group inrect=CreateGroup()
 local unit picked
 local real x
 local real y
 local real alpha=Atan2(y2-y1,x2-x1)
 local real beta =Atan2(y3-y1,x3-x1)
 local real gama =Atan2(y1-y2,x1-x2)
 local real delta=Atan2(y3-y2,x3-x2)
 local real PI2=bj_PI+bj_PI

    call GroupEnumUnitsInRect( inrect, semi,null)
    loop
        set picked=FirstOfGroup(inrect)
        exitwhen picked==null
        set x=GetUnitX(picked)
        set y=GetUnitY(picked)
        if IsUnitInGroup(picked,whichGroup) then
        elseif (GroupAddUnitsInTriangle_AngleCheck(Atan2(y-y1,x-x1),alpha,beta,PI2) and GroupAddUnitsInTriangle_AngleCheck( Atan2(y-y2,x-x2) ,gama,delta,PI2)) then
            call GroupAddUnit( whichGroup, picked)
        endif
        call GroupRemoveUnit(inrect,picked)
    endloop
 call RemoveRect(semi)
 call DestroyGroup(inrect)
 set whichGroup=null
 set picked=null
 set semi=null
 set inrect=null
endfunction
//##end##
02-16-2007, 09:19 AM#3
PandaMine
Hmm thanks a lot, i can just modify it to suit my purpouses