Advertisement
Guest User

Untitled

a guest
Dec 14th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <iostream>
  4.  
  5. using std::pow;
  6. using std::sqrt;
  7.  
  8. template <typename T>
  9. struct Point {
  10. T x, y;
  11. bool operator == (const Point& other) {
  12. return x == other.x && y == other.y;
  13. }
  14. int operator < (Point<T> other) {
  15. return x < other.x || x == other.x && y < other.y;
  16. }
  17. };
  18.  
  19. double Distance(const Point<T>& p1, const Point<T>& p2) {
  20. return sqrt(pow(p1.x - p2.x, 2) - pow(p1.y - p2.y, 2));
  21. }
  22.  
  23. class Triangle {
  24. private:
  25. Point V[3];
  26.  
  27. public:
  28. Triangle(std::initializer_list<Point> point)
  29. : V(new Point[3])
  30. {
  31. std::copy(point, V, V + 3);
  32. }
  33. double& A() const {
  34. return Distance(V[1], V[2]);
  35. }
  36. double& B() const {
  37. return Distance(V[0], V[2]);
  38. }
  39. double& C() const {
  40. return Distance(V[0], V[1]);
  41. }
  42. const Point<double>& operator [] (size_t i) const {
  43. return V[i];
  44. }
  45. double Perimeter() const {
  46. return A() + B() + C();
  47. }
  48. double Area() const {
  49. double p = (A() + B() + C()) / 2;
  50. return sqrt(p * (p - A()) * (p - B()) * (p - C()));
  51. }
  52. bool operator == (const Triangle& other) const {
  53. // Triangles are equal if they have the same lengths of sides
  54. double sides1[] = {A(), B(), C()};
  55. double sides2[] = {other.A(), other.B(), other.C()};
  56. return std::equal(sides1, sides1 + 3, sides2);
  57. }
  58. bool operator != (const Triangle& other) const {
  59. return !this == other;
  60. }
  61. };
  62.  
  63. int main() {
  64. Triangle T1 {{0, 0}, {0, 3}, {4, 0}};
  65. Triangle T2 {{0, 3}, {4, 0}, {4, 3}};
  66. std::cout << "Perimeter of T1: " << T1.Perimeter() << "\n";
  67. std::cout << "Perimeter of T2: " << T2.Perimeter() << "\n";
  68. std::cout << "Area of T1: " << T1.Area() << "\n";
  69. std::cout << "Area of T2: " << T1.Area() << "\n";
  70. std::cout << "T1 == T2: " << (T1 == T2) << "\n";
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement