Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- R - Rectangle : X, Y, W, H
- C - Circle : X, Y, R
- L - Line : X1, Y1, X2, Y2
- P - Point : X, Y
- B - Buffer
- ]]--
- function RRCollision( R1, R2 )
- return R1[1] < R2[1] + R2[3] and R1[2] < R2[2] + R2[4] and R2[1] < R1[1] + R1[3] and R2[2] < R1[2] + R1[4]
- end
- function CCCollision( C1, C2 )
- local CX = C1[1] - C2[1]
- local CY = C1[2] - C2[2]
- local CD = math.sqrt( CX^2 + CY^2 )
- return CD <= ( C1[3] + C2[3] )
- end
- function LLCollision( L1, L2 )
- local uA1 = ( L1[4] - L1[3] ) * ( L2[1] - L2[3] ) - ( L2[4] - L2[3] ) * ( L1[1] - L1[3] )
- local uA2 = ( L2[4] - L2[3] ) * ( L1[2] - L1[1] ) - ( L1[4] - L1[3] ) * ( L2[2] - L2[1] )
- local uA = uA1 / uA2
- local uB1 = ( L1[2] - L1[1] ) * ( L2[1] - L2[3] ) - ( L2[2] - L2[1] ) * ( L1[1] - L1[3] )
- local uB2 = ( L2[4] - L2[3] ) * ( L1[2] - L1[1] ) - ( L1[4] - L1[3] ) * ( L2[2] - L2[1] )
- local uB = uB1 / uB2
- return uA >= 0 or uA <= 1 or uB >= 0 or uB <= 1
- end
- function PPCollision( P1, P2, B )
- return P1[1] >= P2[1] - B and P1[2] <= P2[2] + B
- end
- function RCCollision( R, C )
- local calX = ( C[1] < R[1] ) and R[1] or ( C[1] > R[1] + R[3] ) and R[1] + R[3] or C[1]
- local calY = ( C[2] < R[2] ) and R[2] or ( C[2] > R[2] + R[4] ) and R[2] + R[4] or C[2]
- local DX = C[1] - calX
- local DY = C[2] - calY
- local Dis = math.sqrt( DX^2 + DY^2 )
- return Dis <= C[3]
- end
- function RLCollision( R, L )
- local RL = {}
- RL[1] = { R[1], R[2], R[1], R[2] + R[4] }
- RL[2] = { R[1], R[2] + R[4], R[1] + R[3], R[2] + R[4] }
- RL[3] = { R[1] + R[3], R[2] + R[4], R[1] + R[3], R[2] }
- RL[4] = { R[1] + R[3], R[2], R[1], R[2] }
- local CL = {}
- for i = 1, #RL do
- table.insert( CL, LLCollision( L, RL[i] ) )
- end
- for i = 1, #CL do
- if CL[i] then
- return true
- end
- end
- return false
- end
- function CLCollision( C, L )
- local I1 = CPCollision( { L[1], L[2] }, C )
- local I2 = CPCollision( { L[3], L[4] }, C )
- if I1 or I2 then
- return true
- end
- local DX = L[1] - L[3]
- local DY = L[2] - L[4]
- local LN = math.sqrt( DX^2 + DY^2 )
- local DOT = ( ( C[1] - L[1] ) * ( L[3] - L[1] ) + ( C[2] - L[2] ) * ( L[4] - L[2] ) ) / LN^2
- local CX = L[1] + DOT * ( L[3] - L[1] )
- local CY = L[2] + DOT * ( L[4] - L[2] )
- local OnSeg = LPCollision( L, { CX, CY } )
- if not OnSeg then
- return false
- end
- local DX = CX - C[1]
- local DY = CY - C[2]
- local Dis = math.sqrt( DX^2 + DY^2 )
- return Dis <= C[3]
- end
- function RPCollision( R, P )
- return P[1] >= R[1] and P[1] <= R[1] + R[3] and P[2] >= R[2] and P[2] <= R[2] + R[4]
- end
- function CPCollision( C, P )
- local DX = P[1] - C[1]
- local DY = P[2] - C[2]
- local Dis = math.sqrt( DX^2 + DY^2 )
- return Dis < C[3]
- end
- function LPCollision( L, P, B )
- local D1 = math.sqrt( ( P[1] - L[1] )^2 + ( P[2] - L[2] )^2 )
- local D2 = math.sqrt( ( P[1] - L[3] )^2 + ( P[2] - L[4] )^2 )
- local LL = math.sqrt( ( L[1] - L[3] )^2 + ( L[2] - L[4] )^2 )
- return D1 + D2 >= LL - B and D1 + D2 <= LL + B
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement