Odd Output in counsel

Hi, i am doing an assignment that requires me to determine if a triangle is a right triangle, acute or obtuse given 3 points. So far I calculated the sides of the triangle given the points. I am not trying to calculate the angles given the sides, but when I output the angles (given random points) I get -nan(ind). I am not sure what this means or what i am doing wrong. Here is my code so far:

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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;


int main()
{



		double point1_x;
		double point2_x;
		double point3_x;
		double point1_y;
		double point2_y;
		double point3_y;

		cin >> point1_x >> point1_y >> point2_x >> point2_y >> point3_x >> point3_y;

		double side_A = sqrt(pow(point2_x - point1_x, 2) + pow(point2_y - point1_y, 2));
		double side_B = sqrt(pow(point3_x - point2_x, 2) + pow(point3_y - point2_y, 2));
		double side_C = sqrt(pow(point1_x - point3_x, 2) + pow(point1_y - point3_y, 2));

		double angle_A = (acos(pow(side_B,2) + pow(side_C,2) - pow(side_A,2))) / 2.0 * side_B * side_C;
		double angle_B = (acos(pow(side_C, 2) + pow(side_A, 2) - pow(side_B, 2))) / 2.0 * side_C * side_A;
		double angle_C = 180 - angle_A - angle_B;
		cout << angle_A << endl;
		cout << angle_B << endl;
		cout << angle_C << endl;




	return 0;

}
Last edited on
Two problems leap out without even running it.


- Inverting the cosine rule requires you to bracket more carefully:
cos A = ( b2 + c2 - a2 ) / (2bc)
so
A = acos( ( b2 + c2 - a2 ) / (2bc) )


- Trigonometric functions in any serious scientific programming language work in radians, not degrees, so
angle_C = 180 - angle_A - angle_B;
won't work unless you either convert the angles on the right hand side to degrees or replace the 180 by 2.pi


Last edited on
Topic archived. No new replies allowed.