如何在corona SDK中检查一个圆圈是否在另一个圆圈内

时间:2016-11-07 17:30:11

标签: lua corona

我正在制作一个游戏,你必须停止一个飞行圈,如果停止它是在另一个圈内(不移动并在中心),你得到一个点。 我怎么能检查它是否在另一个圈内?

First photo enter image description here

2 个答案:

答案 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