Lucas Farias
Lucas Farias

Reputation: 518

Checking Triangle Similarity in C

The problem set asks me to create two triangles, defining them using points, and then checking if they're similar.

I did first part: created a struct point and a struct triangle, as the profesor told us to. To solve the problem of checking similarity, I thought I could use the points to define vectors, and them use the law of cosines to calculate its angles, together with some if sentences to check if the triangles are similar.

Which codes could help me achieve that? I could not find anything that I'd be able to turn into a partial solution.

Upvotes: 1

Views: 992

Answers (2)

Surya Kasturi
Surya Kasturi

Reputation: 4828

You are given coordinates of all three points of each triangle. Let us consider two triangles T1 A(a1,a2) B(b1,b2) C(c1, c2), T2 P(p1,p2) Q(q1,q2) R(r1,r2).

  1. a = length of opposite side of vertex A
  2. b = length of opposite side of vertex B
  3. c = length of opposite side of vertex C

similarly p,q,r of triangle T2

So, for the two triangles to be similar, it has to follow the following conditions

    1. AB = PQ; BC = QR; CA = RP 
       (We don't need their directions, So I am considering only magnitudes)

    2. angle (A) = angle(B)  i.e angle(BAC) = angle(QPR); 
       angle(B) = angle(Q)   i.e angle(CBA) = angle (RQP) and 
       angle(C) = angle(R).

Now, you got to use coordinate geometry/ spherical geometry here.

COS (A) = ( b^2 + c^2 - a^2 )/2bc
COS (B) = ( c^2 + a^2 - b^2 )/2ac
COS (C) = (a^2 +  b^2 - c^2)/2ab

Note:: As cosine is periodic with 2*pi, please make sure that you have exact angle. So, why don't you think of using inverse cosine functions where you get principle angles.(I am not sure of them, as how they work. please do check)

(Similarly for P,Q,R of triangle T2).

Actually there is another rule by which its easy to do. law: a/sin(A) = b/sin(B) = c/sin(C).

I think you have to go through Spherical Geometry

I hope this helps you to do the program.

How to do the program:

Actually, its fine if you want to use structures. Create a structure with fields of 3 sides and 3 angles. Thus you need to take two variables under structure type and compare those quantities mentioned above.

If they satisfy, they are similar triangles.

I hope this helps you.

Upvotes: 1

Kos
Kos

Reputation: 72241

What you said does the trick!

For the first triangle, take some measures, like as you said: an angle (or its cosine - easy to calculate with a dot product) on any vertex and the lengths of the sides next to it.

For another triangle, use if-conditions to see if the angle (or its cosine) is the same, and if the ratios of the lengths are also the same. You'd have to do this check from all 3 vertices in this way (if at least one fits, then the triangles are similar).

A faster way would be to always start with (for instnace) the vertex with the smallest angle, then you'd need to only compare once.

Now go code it! :-)

Upvotes: 1

Related Questions