三角坐标计算

时间:2015-07-31 18:43:24

标签: ruby math trigonometry

我将代码从this帖子转换为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)

输出它给了我这个三角形:

output

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

1 个答案:

答案 0 :(得分:4)

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

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

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

所以你的代码似乎试图将这些长度适合三角形,这需要一点伸长。

相关问题