2016-11-07 112 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