答案 0 :(得分:0)
尝试
local abs = math.abs
local function distanceBetweenTwoPoints(x1, y1, x2, y2)
return (((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)) ^ 0.5
end
-- (x1, y1) center and r1 radius of first circle
-- (x2, y2) center and r2 radius of second circle
local function circleOverlap(x1, y1, r1, x2, y2, r2)
return (distanceBetweenTwoPoints(x1, y1, x2, y2) <= r2 + r1)
end
local function oneCircleInsideOther(x1, y1, r1, x2, y2, r2)
return (distanceBetweenTwoPoints(x1, y1, x2, y2) <= abs(r2 - r1))
end
一些测试
print(circleOverlap(0, 0, 1, 0, 0, 2)) -- true
print(circleOverlap(0, 1, 1, 0, 3, 1)) -- false
print(circleOverlap(1, 1, 1, 3, 3, 1)) -- false
print(circleOverlap(5, 10, 5, 12, 10, 2)) -- true
print(oneCircleInsideOther(0, 0, 1, 0, 0, 2)) -- true
print(oneCircleInsideOther(0, 1, 1, 0, 3, 1)) -- false
print(oneCircleInsideOther(1, 1, 1, 3, 3, 1)) -- false
print(oneCircleInsideOther(5, 10, 5, 12, 10, 2)) -- false
答案 1 :(得分:0)
借鉴上一个答案:
-- (x1, y1) center and r1 radius of first circle
-- (x2, y2) center and r2 radius of second circle
-- return true if cirecle 2 is inside circle 1
local function circleInside(x1, y1, r1, x2, y2, r2)
return (distanceBetweenTwoPoints(x1, y1, x2, y2)+ r2 < r1)
end