Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- const float eps = 1e-20f; //10^-20
- int Validate(float a, float b, float c, float d)
- {
- float side[4] = {a, b, c, d};
- float sum = 0.0f;
- //if at least one side < 0.0 -> not trapezoid!
- for(int i = 4; i--; sum += side[i])
- if(side[i] <= 0.0f)
- return 0;
- //if at least one side >= sum of other three sides -> not trapezoid!
- for(int i = 4; i--; )
- if(side[i] >= sum - side[i])
- return 0;
- //parallellogram isn't trapezoid! area can not be found!
- if(fabs(a - b) < eps)
- return 0;
- //additional check to distinguish quadrangle from trapezoid
- float ab = a - b;
- float arg = (d * d - c * c - ab * ab) / (2.0f * c * ab);
- return fabs(arg) < 1.0f;
- }
- float Area(float a, float b, float c, float d)
- {
- if(!Validate(a, b, c, d))
- return -1.0f;
- //trapezoid area through four sides
- float ab = a - b;
- float t = 0.5f * (ab * ab + c * c - d * d) / ab;
- return (a + b) * 0.5f * sqrtf(c * c - t * t);
- }
- int main()
- {
- float a, b, c, d;
- cout << "Base 1 = ";
- cin >> a;
- cout << "Base 2 = ";
- cin >> b;
- cout << "Side 1 = ";
- cin >> c;
- cout << "Side 2 = ";
- cin >> d;
- float s = Area(a, b, c, d);
- if(s < 0.0f)
- cout << "Not trapezoid!" << endl;
- else
- cout << "Area = " << s << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement