Reputation: 2883
I converted the code from this post to ruby, just added some code for the triangle orientation, which works on 95% percent of the cases.
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
But when I pass the following parameters:
find_triangle_coordinates(Point(0, 0), Point(539.939, 0), 130.0, 673.746, :down)
The output it's giving me this triangle:
Which is slightly bigger than the expected. Anyone knows why? Also, any other approaches to solve the same problem are welcome. Thanks!!
Upvotes: 2
Views: 264
Reputation: 4517
Your input lengths cannot form a triangle.
The sum of the lengths of any two sides of a triangle must be greater than the length of the third side of that triangle.
539.939 + 130.0 = 669.939 which is less than the third side of length 673.746
So your code seems to be trying to fit those lengths to a triangle, which requires a bit of elongation.
Upvotes: 4