Hi guys, I'm having trouble with this program. Mainly because I don't know exactly what it's asking. This is what I have so far. I'd appreciate any help I can get. Thank you.
You are to write a program to compute the lengths of the sides of a triangle and the area of the triangle. The 3 vertices of the triangle are to be entered as x and y coordinates which should be of type double. It would be convenient to store all 6 vertices in separate variables.
After reading the vertices your program should print the distances from the first point to the second, from the second to the third and from the third to the first. It would probably be convenient to store these distances in 3 double variables. These distances are the lengths of the sides of the triangle. The formula for distance is:
distance = sqrt ( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) )
Assuming the 3 distances are a, b and c, you can compute the area of the triangle using this formula:
area = 0.25 * sqrt ( (a+b+c) * (-a+b+c) * (a-b+c) * (a+b-c) )
Your program needs to implement 2 user-defined functions. One function needs to compute the distance between 2 vertices. The second function needs to compute the area of a triangle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main()
{
double height = 0.0;
double width = 0.0;
double area = 0.0;
cout << "Enter height: ";
cin >> height;
cout << "Enter width: ";
cin >> width;
area = width/2 * height;
cout << "The area is: " << area << endl;
system("pause");
return 0;
}
To be honest, I'm not exactly sure what my professor is asking from the directions, but what I do take from the directions is that I'm supposed to use the two equations(distance and area) to generate a program.
Your being asked to write a program to do calculations on a triangle using trigonometry. This kind of work is very important for any applications processing 3D geometry, such as with OpenGL. The formulas are given to you so all you need to do is devise the input for the values and calculate the results. First you should store your vertices correctly, I prefer to use a method such as: typedefdouble Vector2d[2];
Now you can declare your points as new values: Vector2d a, b, c;
Each point has 2 values, one for 'x' and one for 'y' which are accessed like any array. You should get the points from the user one value at a time:
1 2 3 4 5
cout << "Enter x for point A: ";
cin >> a[0];
cout << "Enter y for point A: ";
cin >> a[1];
cout << "Point A = (" << a[0] << "," << a[1] << ")" << endl;
The above code gets the values for one of the three points needed for a triangle, you need to get the values for the other 2 points and then use the formulas given to calculate the values you need and display the results. Hope this helps get you on track, good luck.
Looks good, don't forget to change the input variables for B and C (cin >> b[0]). You need to calculate the distance 3 times, once for each side. Also don't confuse the a, b, and c in the area formula you currently have with the distance values you have to calculate to use in that formula. It might be good to rename the variables to: Vector2d pointA, pointB, pointC;
and then use variables properly named to store the distance: double sideA, sideB, sideC;
Then store the results of the distance calculation into the new variables.
Calculate the other two sides using the appropriate points. Draw it on graph paper and it will help in understanding how everything is working and to keep track of what points and sides you are trying to work with. Then use the sides (sideA, sideB, sideC) in the area formula which should be straightforward.
Does that input look right? And I'm confused on the area equation because there are only 3 sides to a triangle, but the formula my teacher gave me has 4 slots, so to speak.
The calculations for the sides are incorrect, they are calculating the same side each time. The points that are in the formula need to be changed for each side.