三角坐标计算

Triangle coordinates calculation

我将代码从 this post 转换为 ruby,只是添加了一些三角形方向的代码,它适用于 95% 的情况。

def find_triangle_coordinates(point_a, point_b, ac_length, bc_length, orientation)
  if orientation == :down
    temp = point_a
    point_a = point_b
    point_b = temp
  end

  ab_length = distance(point_a, point_b)
  ad_length = (ab_length**2 + ac_length**2 - bc_length**2) / (2.0 * ab_length)

  h = sqrt((ac_length**2 - ad_length**2).abs)

  d_x = point_a.x + ad_length * (point_b.x - point_a.x) / ab_length
  d_y = point_a.y + ad_length * (point_b.y - point_a.y) / ab_length

  point_d = Point(d_x, d_y)

  c_x1 = point_d.x + h * (point_b.y - point_a.y) / ab_length
  c_x2 = point_d.x - h * (point_b.y - point_a.y) / ab_length
  c_y1 = point_d.y - h * (point_b.x - point_a.x) / ab_length
  c_y2 = point_d.y + h * (point_b.x - point_a.x) / ab_length

  if orientation == :down
    Point(c_x1, c_y1)
  else
    Point(c_x2, c_y2)
  end

但是当我传递以下参数时:

find_triangle_coordinates(Point(0, 0), Point(539.939, 0), 130.0, 673.746, :down)

给我这个三角形的输出:

略大于预期。 任何人都知道为什么?此外,欢迎任何其他解决相同问题的方法。谢谢!!

您输入的长度不能组成三角形。

三角形任意两条边的长度之和必须大于该三角形第三条边的长度。

539.939 + 130.0 = 669.939 小于第三边的长度 673.746

因此您的代码似乎试图将这些长度拟合为三角形,这需要一点伸长。