알고리즘

다각형2다각형 충돌 선별2

조규현15 2015. 6. 19. 09:48
반응형

function Rect2Rect (RECT, RECT2)

for j=1,4,1 do

for i=1,2,1 do

local d1 = {x=RECT[1+i].x-RECT[1].x,y=RECT[1+i].y-RECT[1].y}

local d2 = {x=RECT[2+i].x-RECT[1].x,y=RECT[2+i].y-RECT[1].y}

local p  = {x=RECT2[j].x-RECT[1].x,y=RECT2[j].y-RECT[1].y}

t2 = (p.x*d1.y-p.y*d1.x)/(d2.x*d1.y-d2.y*d1.x)

t1 = (p.x-t2*d2.x)/d1.x

if t1>=0 and t1<=1 and t2>=0 and t2<=1 and t1+t2>=0 and t1+t2<=1 then

return true

end

end

end

return false

end

function Rect2RectbyLine (RECT, RECT2)

local Composition = 0

local op = {x=-9999,y=-9999}

for i=1,4 do

for j=1,4 do

local d = {x = RECT2[i].x, y = RECT2[i].y}

local l1 = {x = RECT[j].x, y = RECT[j].y}

local l2

if j<4 then

l2 = {x = RECT[j+1].x, y = RECT[j+1].y}

else

l2 = {x = RECT[1].x, y = RECT[1].y}

end

local den = (l2.y-l1.y)*(d.x-op.x)-(l2.x-l1.x)*(d.y-op.y)

if den ~= 0 then

local ua = ((l2.x-l1.x)*(op.y-l1.y)-(l2.y-l1.y)*(op.x-l1.x))/den

local ub = ((d.x-op.x)*(op.y-l1.y)-(d.y-op.y)*(op.x-l1.x))/den

if ua > 0 and ua < 1 and ub > 0 and ub < 1 then

Composition = Composition + 1

end

end

end

if Composition % 2 ~= 0 then

return true

end

end

return false

end

side = 4

RECT1 = {{x=10,y=10},{x=15,y=25},{x=20,y=5},{x=25,y=20}}

RECT2 = {{x=0,y=0},{x=0,y=10},{x=10,y=0},{x=10,y=10}}


if( Rect2RectbyLine(RECT1, RECT2) ) then

print("R1@R2 Inner")

else

if( Rect2RectbyLine(RECT2, RECT1) ) then

print("R2@R1 Inner")

end

end


반응형

'알고리즘' 카테고리의 다른 글

쿼드트리  (0) 2015.08.24
a* search  (0) 2015.07.09
다각형2다각형 충돌 선별  (0) 2015.06.13
다각형에 특정 점 포함 유무선별  (0) 2015.06.13
사각형 그리기  (0) 2015.02.13