Advertisement
Guest User

Untitled

a guest
Oct 8th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. const float eps = 1e-20f; //10^-20
  7.  
  8. int Validate(float a, float b, float c, float d)
  9. {
  10.     //if at least one side < 0.0 -> not trapezoid!
  11.     if(!(a > 0.0f && b > 0.0f && c > 0.0f && d > 0.0f))
  12.         return 0;
  13.    
  14.     //parallellogram isn't trapezoid! area can not be found!
  15.     if(fabs(a - b) < eps)
  16.         return 0;
  17.    
  18.     //additional check to distinguish quadrangle from trapezoid
  19.     float ab  =  a - b;
  20.     float arg = (d * d - c * c - ab * ab) / (2.0f * c * ab);
  21.    
  22.     return fabs(arg) < 1.0f;
  23. }
  24.  
  25. float Area(float a, float b, float c, float d)
  26. {
  27.     if(!Validate(a, b, c, d))
  28.         return -1.0f;
  29.    
  30.     //trapezoid area through four sides
  31.    
  32.     float ab = a - b;
  33.     float t  = 0.5f * (ab * ab + c * c - d * d) / ab;
  34.    
  35.     return (a + b) * 0.5f * sqrtf(c * c - t * t);
  36. }
  37.  
  38. int main()
  39. {
  40.     float a, b, c, d;
  41.    
  42.     cout << "Base 1 = ";
  43.     cin  >> a;
  44.    
  45.     cout << "Base 2 = ";
  46.     cin  >> b;
  47.    
  48.     cout << "Side 1 = ";
  49.     cin  >> c;
  50.    
  51.     cout << "Side 2 = ";
  52.     cin  >> d;
  53.    
  54.     float s = Area(a, b, c, d);
  55.    
  56.     if(s < 0.0f)
  57.         cout << "Not trapezoid!" << endl;
  58.     else
  59.         cout << "Area = " << s << endl;
  60.    
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement