Similar Triangles
InterviewTime limit1sMemory limit1024 MB
Given two triangles by integer vertex coordinates, decide whether they are similar and if so print the squared similarity coefficient as a reduced fraction p/q, else print -1.
- Level
Medium4 of 10
- Topics
- Geometry, Math, Sorting, Number theory
- Solved
- No attempts yet
Problem
Juku is studying triangle similarity at school. Two triangles are similar when all pairs of corresponding angles are equal and the corresponding side lengths are proportional. Two similar triangles may be rotated, reflected, and translated with respect to each other. The ratio of the corresponding side lengths of two similar triangles is called their similarity coefficient.
For homework Juku is given several pairs of triangles and must determine the similarity relationship of each pair. Help Juku by writing a program that decides whether two triangles are similar and, if so, reports their similarity coefficient.
Input
The first line contains six integers: the and coordinates of the three vertices of the first triangle, in the order . Each coordinate lies between and . The second line contains six integers in the same format, giving the vertices of the second triangle. The vertices may be listed in either clockwise or counterclockwise order. The three given points always form a triangle (there are no coincident points and no three collinear points).
Output
Let be the similarity coefficient — how many times larger the first triangle is than the second (so if the first triangle is smaller). Because all vertex coordinates are integers, every squared side length is an integer, so is always a rational number (while itself may be irrational). If the two triangles are similar, print as a reduced fraction p/q with and ; always print the denominator, even when it equals (for example, 4/1). If the two triangles are not similar, print -1.