Date: March 19, 2020
Author: Gábor P. Nagy
In this exercise, we want to compute different measurements of a triangle, which is given either by three sides or by the coordinates of the three vertices. Our basic program inputs the three sides a, b, c and checks if they satisfy the triangle inequality.
// geom_triangle_inequality.cxx
using namespace std;
int main() {
double a, b, c;
cout << "Side a = ";
cin >> a;
cout << "Side b = ";
cin >> b;
cout << "Side c = ";
cin >> c;
if (a+b<=c or a+c<=b or b+c<=a) {
cout << "No triangle with sides " << a << ", " << b << " and " << c << endl;
} else {
cout << "Thank you!" << endl;
}
return 0;
}
Save this file on your computer, compile it and run with different input.
In order to use common mathematical operations and transformations, you must include the <cmath>
numerics library.
x// numerics_cos.cxx
using namespace std;
const double PI(atan(1) * 4);
int main() {
double param, result;
param = 60.0;
result = cos(param * PI / 180.0);
cout << "The cosine of " << param << " degrees is " << result << "." << endl;
return 0;
}
Notice that the value of π is stored in the constant double precision variable PI
. It is calculated using the fact that the tangent of π / 4 is equal to 1.
In the program geom_triangle_inequality.cxx
we did not check if the sides are negative. Why?
Modify the program numerics_cos.cxx
such that π is computed using cos(π) = -1.
Modify the program geom_triangle_inequality.cxx
such that for proper triangles, it also prints the area of the triangle. Use Heron's_formula to compute the area:
where
Modify the program geom_triangle_inequality.cxx
such that it inputs the coordinates of the three vertices and it outputs the perimeter of the triangle. Use Pythagoras' theorem to compute the sides.
Modify the program geom_triangle_inequality.cxx
such that it inputs the coordinates of the three vertices and it outputs the area of the triangle. You can either compute the sides first and then use Heron's formula. Or, you use the fact that in the plane, the (signed) area of a parallelogram is equal to the 2x2 determinant, with rows as spanning vectors. Hence,
Modify the program geom_triangle_inequality.cxx
such that it inputs the coordinates of the three vertices and it outputs the radius of the inscribed circle of the triangle. Compute the area A and use the equation
to find the radius.
geom_triangle_inequality.cxx
such that it inputs the coordinates of the three vertices and it outputs the radius and center of the inscribed circle of the triangle. geom_triangle_inequality.cxx
such that it inputs the coordinates of the three vertices and it outputs the radius and center of the escribed circle of the triangle. geom_triangle_inequality.cxx
such that it inputs the coordinates of the three vertices and it outputs the coordinates of the orthocenter of the triangle. Pay attention to check the input. geom_triangle_inequality.cxx
such that it inputs the coordinates of the three vertices and it outputs if the orientation of the vertices is positive or negative. geom_triangle_inequality.cxx
such that it inputs the coordinates of the three vertices and it outputs whether the origin is contained in the triangle.