Advertisement
OIQ

Untitled

OIQ
Nov 4th, 2020
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System;
  2.  
  3. public class Triangle
  4. {
  5.     private readonly Point a;
  6.     private readonly Point b;
  7.     private readonly Point c;
  8.  
  9.     private double AB => GetLengthOfSide(a, b);
  10.     private double AC => GetLengthOfSide(a, c);
  11.     private double BC => GetLengthOfSide(b, c);
  12.  
  13.     public Triangle(Point a, Point b, Point c)
  14.     {
  15.         this.a = a;
  16.         this.b = b;
  17.         this.c = c;
  18.     }
  19.  
  20.     public double GetPerimeter()
  21.     {
  22.         return AB + AC + BC;
  23.     }
  24.  
  25.     public double GetSquare()
  26.     {
  27.         double p = (AB + BC + AC) / 2;
  28.         return Math.Sqrt(p * (p - AB) * (p - AC) * (p - BC));
  29.     }
  30.  
  31.     public bool GetAngleBetweenEqualsSides(out double angle)
  32.     {
  33.         if (AB == BC) {
  34.             angle = Math.Asin(AC / (2 * AB * AC * BC / 4 / GetSquare()));
  35.             return true;
  36.         }
  37.         if (AB == AC) {
  38.             angle = Math.Asin(BC / (2 * AB * AC * BC / 4 / GetSquare()));
  39.             return true;
  40.         }
  41.         if (AC == BC) {
  42.             angle = Math.Asin(AB / (2 * AB * AC * BC / 4 / GetSquare()));
  43.             return true;
  44.         }
  45.         angle = -1;
  46.         return false;
  47.     }
  48.  
  49.     public bool GetHypotenuse(out double hypotenuse)
  50.     {
  51.         if (Math.Sqrt(AB * AB + AC * AC) == BC ) {
  52.             hypotenuse = Math.Sqrt(AB * AB + AC * AC);
  53.             return true;
  54.         }
  55.         if (Math.Sqrt(AB * AB + BC * BC) == AC) {
  56.             hypotenuse = Math.Sqrt(AB * AB + BC * BC);
  57.             return true;
  58.         }
  59.         if (Math.Sqrt(BC * BC + AC * AC) == AB) {
  60.             hypotenuse = Math.Sqrt(BC * BC + AC * AC);
  61.             return true;
  62.         }
  63.         hypotenuse = -1;
  64.         return false;
  65.     }
  66.  
  67.    
  68.     private static double GetLengthOfSide(Point first, Point second)
  69.     {
  70.         return Math.Sqrt((first.GetX() - second.GetX()) * (first.GetX() - second.GetX()) +
  71.             (first.GetY() - second.GetY()) * (first.GetY() - second.GetY()));
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement