【问题标题】:Calculate Center and Radius of Circle from 3 points on it从上面的3个点计算圆的中心和半径
【发布时间】:2012-05-11 13:41:18
【问题描述】:

有人可以显示可以快速执行此操作的代码吗?假设我们得到三分 p1、p2、p3 按左->右顺序。因此,解决方案还应该检查圆是否有效,即(p1,p2,p3)是逆时针的。

【问题讨论】:

    标签: center geometry


    【解决方案1】:

    要计算圆参数,请看:

    http://paulbourke.net/geometry/circlesphere/ 查找“3 点(2 维)圆方程”

    要确定方向,可以使用多边形面积公式:

    http://paulbourke.net/geometry/polygonmesh/ 寻找“计算多边形的面积和质心”

    如果您需要特定的编程语言,请告诉我。

    【讨论】:

    • 正是我所需要的,详尽而清晰的解释。谢谢!
    • 两个链接都坏了。还有其他链接吗?
    • @SharjithN./M.Hassan - 链接已修复。
    • 当直线垂直 (x2 - x1 = 0 并且除以零) 时,任何依赖于斜率 (m = (y2 - y1)/(x2 - x1)) 的解决方案都不起作用.线性代数是要走的路。
    【解决方案2】:
    • 连接圆上的任意两点,你就有了一个和弦。

    • 弦的垂直平分线必须通过中心。

    • 两条和弦平分线的交点就是中心。

    余数(简化为最有效计算的形式)留给读者作为练习......

    【讨论】:

      【解决方案3】:

      这是一个只有一个 if 的简短函数(Swift 语言)。

      enum Result {
          case circle(center: CGPoint, radius: CGFloat)
          case invalid
      }
      
      func circleTouching3Points(a: CGPoint, b: CGPoint, c: CGPoint) -> Result {
          let d1 = CGPoint(x: b.y - a.y, y: a.x - b.x)
          let d2 = CGPoint(x: c.y - a.y, y: a.x - c.x)
          let k: CGFloat = d2.x * d1.y - d2.y * d1.x
          guard k < -0.00001 || k > 0.00001 else {
              return Result.invalid
          }
          let s1 = CGPoint(x: (a.x + b.x) / 2, y: (a.y + b.y) / 2)
          let s2 = CGPoint(x: (a.x + c.x) / 2, y: (a.y + c.y) / 2)
          let l: CGFloat = d1.x * (s2.y - s1.y) - d1.y * (s2.x - s1.x)
          let m: CGFloat = l / k
          let center = CGPoint(x: s2.x + m * d2.x, y: s2.y + m * d2.y)
          let dx = center.x - a.x
          let dy = center.y - a.y
          let radius = sqrt(dx * dx + dy * dy)
          return Result.circle(center: center, radius: radius)
      }
      

      【讨论】:

        猜你喜欢
        • 2021-04-09
        • 2014-05-12
        • 1970-01-01
        • 2010-09-16
        • 1970-01-01
        • 1970-01-01
        • 2018-09-08
        • 1970-01-01
        • 2018-07-28
        相关资源
        最近更新 更多