2016-11-07 110 views
0

我正在制作一个游戏,您必须停止飞行圆圈,并且如果停止时它位于另一个圆圈(不移动且在中心)内,一个点。 我怎么能检查它是否在另一个圈内?如何检查一个圆圈是否在另一个圆球内部SDK

First photo enter image description here

+1

拿纸笔并尝试自己解决它。这不是一个思维服务。 至少给了我们一些想法。 – Piglet

回答

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