Triangles...

closed account (jvqpDjzh)
//can you tell me where the problems are?
//this function should set the sides and the corresponding angles of a triangle, and also the area and the perimeter ...

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
const int sumOfAngles = 180;
	
double side1, side2, side3;
double angle1, angle2, angle3;
double s;
double area, perimeter;


bool set3Sides(double side1, double side2, double side3) {
		bool isCorrect = false;
		
		if( (side1 > 0) && (side2 > 0) && (side3 > 0) ) {
			
			//using cosine theorem => errors? => very likely ... 
			angle1 = acos( (side1*side1 - side2*side2 - side3*side3) / ( (-2) * side2 * side3 ) );
			angle2 = acos( (side2*side2 - side1*side1 - side3*side3) / ( (-2) * side1 * side3 ) );
			angle3 = acos( (side3*side3 - side2*side2 - side3*side3) / ( (-2) * side1 * side2 ) );
			//
			if(angle1 + angle2 + angle3 == sumOfAngles) {
				this->side1 = side1;
				this->side2 = side2;
				this->side3 = side3;
				
				perimeter = side1 + side2 + side3;
				s = (side1 + side2 + side3) / 2;
				area = sqrt(s * (s - side1) * (s - side2) * (s - side3));
				
				isCorrect = true;
			}
		}
		return isCorrect;
}
Last edited on
closed account (jvqpDjzh)
Ok, I had used "this.something" instead of "this->somethingelse" because I was creating this function in java...
Line 13: Where is sumOfAngles defined?

Lines 14-16: You're referencing this which implies this function is part of a class, but it's not defined as a member of a class at line 3.

Lines 14-18: this is a pointer. You're not defererencing the pointer. Should be this-> assuming this function is even part of a class.

Note: Reviewed only for syntax. Not commenting on the math.



Last edited on
closed account (jvqpDjzh)
ok, this function (or method or whatever you want) is part of a class ...
That's fine. However line 9 (was line 3) must indicate the class of which it's a member. e.g.

 
bool Triangle::set3Sides(double side1, double side2, double side3)


As revised, lines 1-6 are globals, not members of a class.
Last edited on
closed account (jvqpDjzh)
yes, I've just done the translation from java, you know... i can't find the math error ... theoretically the arc cosino of a cosino should be the angle, if I am not wrong in my reasoning: the arc cosine is the reverse function of the cosine: with the calculator: cos (^ -1) ? !
Topic archived. No new replies allowed.