函数是圆内的圆和修正?

我正在尝试实现一种数学程序,以确保一个圆c1完全在另一个圆c2内。

它应该遵循以下方式:

给定c1(x,y,r)和c2(x,y,r)以及c2.r>c1.r

  • 如果c1在c2内,则返回true
  • 返回一个向量V(x,y),即要应用于c1的最小更正,使其在c2中。

它对你来说如何?对于数学家或物理学家来说应该很容易,但对我来说很难。

我已经尝试在lua中实现,但肯定存在问题。

local function newVector(P1, P2)
    local w, h=(P2.x-P1.x), (P2.y-P1.y)
    local M=math.sqrt(w^2 + h^2)
    local alpha=math.atan(h/w)
    return {m=M, alpha=alpha}
end

local function isWithin(C1, C2)
    local V12=newVector(C1, C2)
    local R12=C2.r-C1.r
    local deltaR=R12-V12.m
    if deltaR>=0 then
        return true
    else
        local correctionM=(V12.m+deltaR)    --module value to correct
        local a=V12.alpha
        print("correction angle: "..math.deg(a))
        local correctionX=correctionM*math.cos(a)
        local correctionY=correctionM*math.sin(a)
        return {x=correctionX, y=correctionY}
    end
end

谢谢!

点赞
用户2365873
用户2365873

题目描述

不是检查距离(Center1, Center2) + Radius1 <= Radius2 好像就够了吗?

local function isWithin(C1, C2)
  local distance = math.sqrt((C1.x-C2.x)^2+(C1.y-C2.y)^2)
  return distance + C1.r <= C2.r + Epsilon

其中Epsilon是用于避免数值误差。 (Epsilon = 1e-9)

这样听起来很简单。

2013-07-30 16:45:52
用户2112239
用户2112239

好的,最终我将它正常运作。

关键是像lhf建议的那样使用math.atan2。此外,我在校正值方面有一些数值错误。

以下是最终代码。我还包括了我用于从任何corona display对象创建圆的circleFromBounds函数。

local function newVector(P1,P2)
    local w,h =(P2.x-P1.x),(P2.y-P1.y)
    local M = math.sqrt(w ^ 2 + h ^ 2)
    local alpha = math.atan2(h,w)
    返回{m = M,alpha = alpha}
end

local function isWithin(C1,C2)
    local V12 = newVector(C1,C2)
    local epsilon = 10 ^(-9)
    if(V12.m + C1.r <=C2.r + epsilon)then
        返回真
    其他
        local correctionM =(C2.r-(C1.r + V12.m)) -模值进行更正
        local A = V12.alpha
        local correctionX = correctionM * math.cos(A)
        local correctionY = correctionM * math.sin(A)
        返回{x = correctionX,y = correctionY}
    结束
end

local function circleFromBounds(bounds,type)
    local x,y,r
    本地宽度,高度=(bounds.xMax-bounds.xMin),(bounds.yMax-bounds.yMin)
    local ratio =宽度/高度

    x = bounds.xMin +宽度* .5
    y = bounds.yMin +高度* .5

    如果“inner”==类型则
        如果比率> 1 then
            r = height* .5
        其他
            r = width* .5
        结束
    否则如果“outer”==类型则
        local c1,c2 = width* .5,height* 0.5
        r = math.sqrt(c1 ^ 2 + c2 ^ 2)
    结束

    返回{x = x,y = y,r = r}
end

感谢你的帮助!

2013-07-31 07:25:09