// The Pennyslvania State University // ***** // // Description: // Calculate the area of a quadrilateral using 3 different methods, 18th Century formula, & 2 applications of Heron's formula. // // Input: // 4 ordered pairs (x_1,y_1) (x_2,y_2) (x_3,y_3) (x_4,y_4), one component at a time. // // Processing: // Calculate the area with the 18th Century formula as per project handout, & 2 applications of Heron's formula for computing the area of a triangle, the first dividing the quadrilateral from the top-left vertex to the bottom-right vertex, & the second dividing it using the opposite vertices (top-right to bottom_left). // // Output: // Three area calculations (a_1 (Output 1), a_2 (Output 2), a_3 (Output 3)) accurate to 12 decimal places. // //*********************************************** #include <iostream> // Preprocesser for cin and cout #include <cmath> // Includes math operators #include <iomanip> // Includes precision operators using namespace std ; int main () // Program starts here { // Variable definition // ************************* double x_1 , x_2 , x_3 , x_4 ; // Input variables, x-values double y_1 , y_2 , y_3 , y_4 ; // Input variables, y-values double a_1 ; // Calculation 1 (Output 1) storage double a_2 , s_1a , s_2a , s_3a , s_1b , s_2b , s_3b , semi_1a , semi_1b , a_2a , a_2b ; // Calculation 2 (Output 2) storage double a_3 , s_1c , s_2c , s_3c , s_1d , s_2d , s_3d , semi_2a , semi_2b , a_3a , a_3b ; // Calculation 3 (Output 3) storage // Begins prompt from input values // ************************* // Displays message to user cout << "Calculate the area of a quadrilateral" << endl ; cout << "Please enter the 4 ordered pairs:" << endl ; cout << "(Press ENTER afer each value)" << endl ; // Vertex 1: (x_1,y_1) cout << "First x-value, top-left vertex (vertex 1): x_1 = " << endl ; cin >> x_1 ; // Stores input to x_1 cout << "First y-value, top-left vertex (vertex 1): y_1 = " << endl ; cin >> y_1 ; // Stores input to y_1 // Vertex 2: (x_2,y_2) cout << "Second x-value, bottom-left vertex (vertex 2): x_2 = " << endl ; cin >> x_2 ; // Stores input to x_2 cout << "Second y-value, bottom-left vertex (vertex 2): y_2 = " << endl ; cin >> y_2 ; // Stores input to y_2 // Vertex 3: (x_3,y_3) cout << "Third x-value, bottom-right vertex (vertex 3): x_3 = " << endl ; cin >> x_3 ; // Stores input to x_3 cout << "Third y-value, bottom-right vertex (vertex 3): y_3 = " << endl ; cin >> y_3 ; // Stores input to y_3 // Vertex 4: (x_4,y_4) cout << "Fourth x-value, top-right vertex (vertex 4): x_4 = " << endl ; cin >> x_4 ; // Stores input to x_4 cout << "Fourth y-value, top-right vertex (vertex 4): x_4 = " << endl ; cin >> y_4 ; // Stores input to y_4 // Displays input confirmation cout << "Vertices were entered as:" << endl ; // Defines following data as: cout << "Vertex 1 ( " << x_1 << " , " << y_1 << " )" << endl ; // Vertex 1 cout << "Vertex 2 ( " << x_2 << " , " << y_2 << " )" << endl ; // Vertex 2 cout << "Vertex 3 ( " << x_3 << " , " << y_3 << " )" << endl ; // Vertex 3 cout << "Vertex 4 ( " << x_4 << " , " << y_4 << " )" << endl ; // Vertex 4 // Area calculation with 18th century formula (Output 1) // ************************* a_1 = ( 1.0 / 2.0 ) * ( (( x_1 * y_2 ) - ( x_2 * y_1 )) + (( x_2 * y_3 ) - ( x_3 * y_2 )) + (( x_3 * y_4 ) - ( x_4 * y_3 )) + (( x_4 * y_1 ) - ( x_1 * y_4 )) ) ; // Area calculation with Heron's rule (division of figure from vertex 1 to vertex 3) (Output 2) // ************************* s_1a = pow ( pow (( x_1 - x_2 ), 2.0 ) + pow (( y_1 - y_2 ), 2.0 ) , ( 1.0 / 2.0 )); // Calculating side lengths of first triangle in area calculation s_2a = pow ( pow (( x_2 - x_3 ), 2.0 ) + pow (( y_2 - y_3 ), 2.0 ) , ( 1.0 / 2.0 )); // s_3a = pow ( pow (( x_3 - x_1 ), 2.0 ) + pow (( y_3 - y_1 ), 2.0 ) , ( 1.0 / 2.0 )); // semi_1a = ( s_1a + s_2a + s_3a ) / ( 2.0 ); // Calculates length of the semiperimeter of first triangle a_2a = pow ( ( semi_1a * ( semi_1a - s_1a ) * ( semi_1a - s_2a ) * ( semi_1a - s_3a ) ) , ( 1.0 / 2.0 )); // Calculates area of first triangle //cout << s_1a << "," << s_2a << "," << s_3a << "," << semi_1a << "," << a_2a << endl; // Section output for testing. s_1b = pow ( pow (( x_1 - x_4 ), 2.0 ) + pow (( y_1 - y_4 ), 2.0 ) , ( 1.0 / 2.0 )); // Calculating side lengths of second triangle in area calculation s_2b = pow ( pow (( x_3 - x_4 ), 2.0 ) + pow (( y_3 - y_4 ), 2.0 ) , ( 1.0 / 2.0 )); // s_3b = pow ( pow (( x_3 - x_1 ), 2.0 ) + pow (( y_3 - y_1 ), 2.0 ) , ( 1.0 / 2.0 )); // semi_1b = ( s_1b + s_2b + s_3b ) / ( 2.0 ); // Calculates length of the semiperimeter of second triangle a_2b = pow ( ( semi_1b * ( semi_1b - s_1b ) * ( semi_1b - s_2b ) * ( semi_1b - s_3b ) ) , ( 1.0 / 2.0 )); // Calculates area of second triangle //cout << s_1b << "," << s_2b << "," << s_3b << "," << semi_1b << "," << a_2b << endl; // Section output for testing. a_2 = a_2a + a_2b ; // Calculates area of quadrilateral // Area calculation with Heron's rule (division of figure from vertex 2 to vertex 4) (Output 3) // ************************* s_1c = pow ( pow (( x_1 - x_2 ), 2.0 ) + pow (( y_1 - y_2 ), 2.0 ) , ( 1.0 / 2.0 )); // Calculating side lengths of first triangle in area calculation s_2c = pow ( pow (( x_2 - x_4 ), 2.0 ) + pow (( y_2 - y_4 ), 2.0 ) , ( 1.0 / 2.0 )); // s_3c = pow ( pow (( x_4 - x_1 ), 2.0 ) + pow (( y_4 - y_1 ), 2.0 ) , ( 1.0 / 2.0 )); // semi_2a = ( s_1c + s_2c + s_3c ) / ( 2.0 ); // Calculates length of the semiperimeter of first triangle a_3a = pow ( ( semi_2a * ( semi_2a - s_1c ) * ( semi_2a - s_2c ) * ( semi_2a - s_3c ) ) , ( 1.0 / 2.0 )); // Calculates area of first triangle //cout << s_1c << "," << s_2c << "," << s_3c << "," << semi_2a << "," << a_3a << endl; // Section output for testing. s_1d = pow ( pow (( x_3 - x_2 ), 2.0 ) + pow (( y_3 - y_2 ), 2.0 ) , ( 1.0 / 2.0 )); // Calculating side lengths of second triangle in area calculation s_2d = pow ( pow (( x_2 - x_4 ), 2.0 ) + pow (( y_2 - y_4 ), 2.0 ) , ( 1.0 / 2.0 )); // s_3d = pow ( pow (( x_4 - x_3 ), 2.0 ) + pow (( y_4 - y_3 ), 2.0 ) , ( 1.0 / 2.0 )); // semi_2b = ( s_1d + s_2d + s_3d ) / ( 2.0 ); // Calculates length of the semiperimeter of second triangle a_3b = pow ( ( semi_2b * ( semi_2b - s_1d ) * ( semi_2b - s_2d ) * ( semi_2b - s_3d ) ) , ( 1.0 / 2.0 )); // Calculates area of second triangle //cout << s_1d << "," << s_2d << "," << s_3d << "," << semi_2b << "," << a_3b << endl; // Section output for testing. a_3 = a_3a + a_3b ; // Calculates area of quadrilateral // Output statements // ************************* cout << fixed ; // Sets precision to digits to the right of the decimal cout << setprecision ( 12 ); // Sets digit precision to 12 digits cout << "Area (a_1) equals " << a_1 << " (Output 1)" << endl ; // First area calculation cout << "Area (a_2) equals " << a_2 << " (Output 2)" << endl ; // Second area calculation cout << "Area (a_3) equals " << a_3 << " (Output 3)" << endl ; // Third area calculation cout << "If all three outputs are not equal, an error with your data input occured!" << endl ; // If/else statements haven't been in lecture when this was completed. /* if (( a_1 == a_2 ) && ( a_2 == a_3 )) { // Output statements // ************************* cout << fixed; // Sets precision to digits to the right of the decimal cout << setprecision(12); // Sets digit precision to 12 digits cout << "Area (a_1) equals " << a_1 << " (Output 1)" << endl; // First area calculation cout << "Area (a_2) equals " << a_2 << " (Output 2)" << endl; // Second area calculation cout << "Area (a_3) equals " << a_3 << " (Output 3)" << endl; // Third area calculation }else{ cout << "An error with your input has occured." << endl; // Displays input confirmation cout << "Vertices were entered as:" << endl; // Defines following data as: cout << "Vertex 1 ( " << x_1 << " , " << y_1 << " )" << endl; // Vertex 1 cout << "Vertex 2 ( " << x_2 << " , " << y_2 << " )" << endl; // Vertex 2 cout << "Vertex 3 ( " << x_3 << " , " << y_3 << " )" << endl; // Vertex 3 cout << "Vertex 4 ( " << x_4 << " , " << y_4 << " )" << endl; // Vertex 4 } */ return 0 ; }