【问题标题】:How to check if one circle is inside another in corona SDK如何在电晕SDK中检查一个圆圈是否在另一个圆圈内
【发布时间】:2016-11-08 03:26:00
【问题描述】:

我正在制作一个游戏,您必须在其中停止飞行圈,如果停止时它在另一个圈内(静止且在中心),您将获得一分。 我如何检查它是否在另一个圈子内?

【问题讨论】:

  • 拿起笔和纸,试着自己解决。这不是一个有思想的服务。至少给我们一些想法。

标签: lua coronasdk


【解决方案1】:

借用上一个答案:

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

【讨论】:

    【解决方案2】:

    试试

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 2011-03-10
      • 1970-01-01
      • 2013-01-13
      相关资源
      最近更新 更多