Warcraft III resources & community, 2003–2006 · archived

IsPointInQuadFast

not rated
Submitted by AltSubmitterFunctions0 downloads
This function is a lot like AIAndy's IsLocInQuad function but it uses a different algorthim, resulting in two primary differences.
A) It's faster
B) It only works for Convex quads

Source

function DoesQuadContainOrigin takes real x1, real y1, real x2, real y2, real x3, real y3, real x4, real y4 returns boolean
    local integer counter = 0

    if (x1-x2)*y1 < x1 * (y1-y2) then
        set counter = counter + 1
    endif

    if (x2-x3)*y2 < x2 * (y2-y3) then
        set counter = counter + 1
    endif
 
    if (x3-x4)*y3 < x3 * (y3-y4) then
        set counter = counter + 1
    endif

    if (x4-x1)*y4 < x4 * (y4-y1) then
        set counter = counter + 1
    endif

    return ((counter == 4) or (counter == 0))
endfunction

function IsPointInQuadFast takes real x, real y, real x1, real y1, real x2, real y2, real x3, real y3, real x4, real y4 returns boolean
    return DoesQuadContainOrigin(x1-x,y1-y,x2-x,y2-y,x3-x,y3-y,x4-x,y4-y)
endfunction

Comments

1
im using this in another function, and my idea was to change the DoesQuadContainOrigin-function:

function DoesQuadContainOrigin takes real x1, real y1, real x2, real y2, real x3, real y3, real x4, real y4 returns boolean
	local boolean firstcheck = (x1-x2)*y1 < x1 * (y1-y2)
	if (x2-x3)*y2 < x2 * (y2-y3) != firstcheck then
		return false
	endif
	if (x3-x4)*y3 < x3 * (y3-y4) != firstcheck then
		return false
	endif
	return (x4-x1)*y4 < x4 * (y4-y1) == firstcheck
endfunction


yours is easier to understand what it is doing, but mine is, i think, faster... it returns false sooner and prevents the function from doing unneccessary calculations.