Triangle Program

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>
using namespace 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;
}
Sorry, your question confused me. Was it this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double height1    = 0.0;
    
    
   double width1     = 0.0;
   double distance = 0.0;
   
   double area      = 0.0;
   
    cout << "Enter height 1: ";
    cin >>      height1;
    
    
   cout <<   "Enter width 1: ";
   cin >>    width1;
   
   cout << "Down distance: ";
   cin >> distance;
   
   
    area = 0.25 * sqrt((height1+width1+distance) * (-height1+width1+distance) * (height1-width1+distance) * (height1+width1-distance));
   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:
typedef double 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.
Okay, thanks for your response. And I know this isn't complete, but am I on the right track now?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    typedef double Vector2d[2];
    Vector2d a, b, c;
    
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;
cout << "Enter x for point B: ";
cin >> a[0];
cout << "Enter y for point B: ";
cin >> a[1];
cout << "Point B = (" << b[0] << "," << b[1] << ")" << endl;
cout << "Enter x for point C: ";
cin >> a[0];
cout << "Enter y for point C: ";
cin >> a[1];
cout << "Point C = (" << c[0] << "," << c[1] << ")" << endl;

distance = sqrt ( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
///

area = 0.25 * sqrt ( (a+b+c) * (-a+b+c) * (a-b+c) * (a+b-c) );
///

system("pause");
  
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.
Okay, cleaned it up a little bit, but I'm still slightly confused on how to correctly declare a,b,c etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    typedef double Vector2d[2];
    Vector2d pointA, pointB, pointC;
    double sideA, sideB, sideC;
    
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;
cout << "Enter x for point B: ";
cin >> b[0];
cout << "Enter y for point B: ";
cin >> b[1];
cout << "Point B = (" << b[0] << "," << b[1] << ")" << endl;
cout << "Enter x for point C: ";
cin >> c[0];
cout << "Enter y for point C: ";
cin >> c[1];
cout << "Point C = (" << c[0] << "," << c[1] << ")" << endl;

distance = sqrt ( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );


area = 0.25 * sqrt ( (a+b+c) * (-a+b+c) * (a-b+c) * (a+b-c) );


system("pause");
    return 0;
}
They are declared as pointA, pointB, and pointC for better clarity. So change the input section like this:
1
2
3
4
5
	cout << "Enter x for point A: ";
	cin >> pointA[0];
	cout << "Enter y for point A: ";
	cin >> pointA[1];
	cout << "Point A = (" << pointA[0] << "," << pointA[1] << ")" << endl;


And change the distance calculation like this for the first side:
1
2
	sideA = sqrt( (pointA[0]-pointB[0])*(pointA[0]-pointB[0]) + (pointA[1]-pointB[1])*(pointA[1]-pointB[1]));
	cout << "side A = " << sideA << endl;


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    typedef double Vector2d[2];
    Vector2d pointA, pointB, pointC;
    double sideA, sideB, sideC;
    
cout << "Enter x for point A: ";
cin >> pointA[0];
cout << "Enter y for point A: ";
cin >> pointA[1];
cout << "Point A = (" << pointA[0] << "," << pointA[1] << ")" << endl;

cout << "Enter x for point B: ";
cin >> pointB[0];
cout << "Enter y for point B: ";
cin >> pointB[1];
cout << "Point B = (" << pointB[0] << "," << pointB[1] << ")" << endl;

cout << "Enter x for point C: ";
cin >> pointC[0];
cout << "Enter y for point C: ";
cin >> pointC[1];
cout << "Point C = (" << pointC[0] << "," << pointC[1] << ")" << endl;


sideA = sqrt( (pointA[0]-pointB[0])*(pointA[0]-pointB[0]) + (pointA[1]-pointB[1])*(pointA[1]-pointB[1]));
	cout << "side A = " << sideA << endl;
sideB = sqrt( (pointA[0]-pointB[0])*(pointA[0]-pointB[0]) + (pointA[1]-pointB[1])*(pointA[1]-pointB[1]));
	cout << "side B = " << sideB << endl;
sideC = sqrt( (pointA[0]-pointB[0])*(pointA[0]-pointB[0]) + (pointA[1]-pointB[1])*(pointA[1]-pointB[1]));
	cout << "side C = " << sideC << endl;


area = 0.25 * sqrt ( (a+b+c) * (-a+b+c) * (a-b+c) * (a+b-c) );


system("pause");
    return 0;
}
Got it. Thanks for the help everyone!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    typedef double Vector2d[2];
    Vector2d pointA, pointB, pointC;
    double sideA, sideB, sideC;
    double area;
    
cout << "Enter x for point A: ";
cin >> pointA[0];
cout << "Enter y for point A: ";
cin >> pointA[1];
cout << "Point A = (" << pointA[0] << "," << pointA[1] << ")" << endl;

cout << "Enter x for point B: ";
cin >> pointB[0];
cout << "Enter y for point B: ";
cin >> pointB[1];
cout << "Point B = (" << pointB[0] << "," << pointB[1] << ")" << endl;

cout << "Enter x for point C: ";
cin >> pointC[0];
cout << "Enter y for point C: ";
cin >> pointC[1];
cout << "Point C = (" << pointC[0] << "," << pointC[1] << ")" << endl;


sideA = sqrt( (pointA[0]-pointB[0])*(pointA[0]-pointB[0]) + (pointA[1]-pointB[1])*(pointA[1]-pointB[1]));
	cout << "side A = " << sideA << endl;
sideB = sqrt( (pointA[0]-pointB[0])*(pointA[0]-pointB[0]) + (pointA[1]-pointB[1])*(pointA[1]-pointB[1]));
	cout << "side B = " << sideB << endl;
sideC = sqrt( (pointA[0]-pointB[0])*(pointA[0]-pointB[0]) + (pointA[1]-pointB[1])*(pointA[1]-pointB[1]));
	cout << "side C = " << sideC << endl;


area = 0.25 * sqrt ( sideA+sideB+sideC) * (-sideA+sideB+sideC) * (sideA-sideB+sideC) * (sideA+sideB-sideC) ;


system("pause");
    return 0;
}
Last edited on
Not a problem. please come by again...
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.
Topic archived. No new replies allowed.