Witzig Adrien
Witzig Adrien

Reputation: 102

Draw a triangle with 3 sides given

I want to draw a triangle with only the 3 sides length. (In C# Winforms)

Example: S1(3), S2(4), S3(5) with SN(x) as the length of each side.

I've tried to do this and the result is not a right triangle but it looks like it.
For the first side i just draw it like a line. and after i triy to find the angle with Pythagore and the same for the second one, but i realised that if i enter (5,4,3) it's something else.I'm just try to understand how can i find coordinates of a triangle with only the length of the sides.

  Point a = new Point(0, 0);
  Point b = new Point(s1, 0);

  double y = (Math.Pow(s1, 2) + Math.Pow(s3, 2) - Math.Pow(s2, 2)) / (2 * s1);
  double x = Math.Sqrt(Math.Pow(s3, 2) - Math.Pow(y, 2));

  Point c = new Point((int)x, (int)y);
  e.Graphics.DrawLine(Pens.Black, a, b);
  e.Graphics.DrawLine(Pens.Black, b, c);
  e.Graphics.DrawLine(Pens.Black, c, a);

That's the result:

Result

Can someone help me? because I think I don't understand how can I do this.

Upvotes: 1

Views: 1460

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 25972

This is more a math problem. At point A you have the sides s1, s3 with opposing side s2. The cosine formula then gives

2*s1*s3*cos(alpha) = s1^2+s3^2-s2^2.

Now the cosine is the projection of the angle to the horizontal axis, so you should have

x = s3*cos(alpha) = (s1^2+s3^2-s2^2)/(2*s1)

and correspondingly

y = sqrt(s3^2-x^2).

For the test side lengths 3,4,5 this would give

x = (3^2 + (5^2-4^2))/(2*3) = 3
y = sqrt(5^2-3^2) = 4

producing the points for the rectangular triangle.

Upvotes: 5

Related Questions