-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrilemma.cc
More file actions
90 lines (72 loc) · 1.87 KB
/
trilemma.cc
File metadata and controls
90 lines (72 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// William Sjöblom
#include <cmath>
#include <algorithm>
#include <iterator>
#include <iostream>
/**
* Vertex.
*/
struct Vertex {
long x, y;
};
/**
* Triangle.
*/
struct Triangle {
Vertex vertices[3];
Vertex& operator[](int i) { return vertices[i]; }
};
/**
* Is this a triangle (i.e. are the vertices non collinear)?
*/
bool is_triangle(Triangle& tri) {
http://mathworld.wolfram.com/Collinear.html
return 0 != (tri[0].x * (tri[1].y - tri[2].y) +
tri[1].x * (tri[2].y - tri[0].y) +
tri[2].x * (tri[0].y - tri[1].y));
}
/**
* Euclidian distance squared.
*/
long distance2(Vertex& v0, Vertex& v1) {
long dx = v0.x - v1.x;
long dy = v0.y - v1.y;
return std::abs(dx*dx + dy*dy);
}
/**
* Analyze triangle.
*/
void analyze(Triangle& tri) {
if (!is_triangle(tri)) {
std::cout << "not a triangle" << std::endl;
return;
}
float side2[3];
side2[0] = distance2(tri[0], tri[1]);
side2[1] = distance2(tri[1], tri[2]);
side2[2] = distance2(tri[2], tri[0]);
std::sort(std::begin(side2), std::end(side2));
if (side2[0] == side2[1] || side2[1] == side2[2])
std::cout << "isosceles ";
else
std::cout << "scalene ";
if (side2[0] + side2[1] == side2[2])
std::cout << "right triangle";
else if (side2[0] + side2[1] < side2[2])
std::cout << "obtuse triangle";
else
std::cout << "acute triangle";
std::cout << std::endl;
}
int main() {
int tri_count; scanf("%d", &tri_count);
for (int i = 0; i < tri_count; i++) {
Triangle tri;
scanf("%ld %ld %ld %ld %ld %ld",
&tri.vertices[0].x, &tri.vertices[0].y,
&tri.vertices[1].x, &tri.vertices[1].y,
&tri.vertices[2].x, &tri.vertices[2].y);
std::cout << "Case #" << i + 1 << ": ";
analyze(tri);
}
}