//
//	This program finds the area of a triangle given the points of the triangle. It computes
//	the area of the triangle using three different formulas, each will result in the same answer.
//
//	Area1 = 1/2 | (x1y2 - x2y1) + (x2y3 - x3y2) + (x3y1 - x1y3) |
//	(distance)^2 = (x-difference)^2 + (y-difference)^2
//	Area2 = sqrt( s(s-a)(s-b)(s-c) )
//	c^2= A^2 + B^2 - 2 A B cos(theta)
//	
//  C = sqrt( pow(A, 2) + pow(B, 2) - 2*A*B*cos(theta)
//  
//	Area3 = 0.5*A*B*sin(theta)
//

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()

{
	float x1, x2, x3, y1, y2, y3, Area1, Area2, Area3, s, a, b, c, Perimeter, theta;

//	The user needs to input the points of the triangle.

		cout << "Please enter the value of x1: ";
		
		cin >> x1;

		cout << "Please enter the value of y1: ";

		cin >> y1;

		cout << "Please enter the value of x2: ";

		cin >> x2;

		cout << "Please enter the value of y2: ";

		cin >> y2;

		cout << "Please enter the value of x3: ";

		cin >> x3;

		cout << "Please enter the value of y3: ";

		cin >> y3;

// Find Area1 using the first formula.

		Area1 =  0.5 * abs((x1*y2 - x2*y1) + (x2*y3 - x3*y2) + (x3*y1 - x1*y3));

// Use this formula to find the three sides of the triangle.

		a = sqrt( pow(x2-x1, 2) +  pow(y2-y1, 2) );

		b = sqrt( pow(x3-x2, 2) +  pow(y3-y2, 2) );

		c = sqrt( pow(x3-x1, 2) +  pow(y3-y1, 2) );

// Find perimeter of the triangle using the formula for perimeter.

		Perimeter = a + b + c;

// Semiperimeter is half the perimeter.

		s = Perimeter / 2;

//	Find Area2 using the given formula.

		Area2 = sqrt ( s*(s-a)*(s-b)*(s-c) );
		
//	Solve for theta in the formula c^2= A^2 + B^2 - 2 A B cos(theta).

		theta = acos(( pow(a, 2) + pow(b, 2) - pow(c, 2) ) / ( 2.00 * a * b ) );

// Find Area3 using the given formula.

		Area3 = 0.5*( a*b*sin(theta) );

// Display the three areas after being calculated by the three formulas.

		cout << "Area1 is: " << setprecision(12) << Area1 << "\nArea2 is: " << setprecision(12) << Area2 << "\nArea3 is: " << setprecision(12) << Area3 << endl;


		return 0;
}

These are the vertices:

(0,2), (0,0), (1.73205, 1)
(-1.8,-1), (0.7,-1), (-1.1,1.4)
(1,1), (15,6), (3,11)

The question is:

One would expect that since all the integers in the third batch of vertices have exact representations in the machine, with ho round-off errors, that they should cause the least problems. Yet a glance at the output should see it has the biggest errors. Can you explain why?

The most common formula for the area of a triangle does not appear among the methods above, because it assumes the base is easy to find. Unfortunately, it's only really easy to find in analytic geometry if it runs parallel to the x-axis, which does not always happen.
For be able to use this equation for a more general case, consider the following figure.


And, this one:


. (x3,y3)
/
/ |
b / |
(x1,y1) . ------------- / | h
/ |
/ |
. (x2,y2)
The area of the triangle formed from these three points can be found with the usual ½bh, if one gets the correct value for b.
The right-end of that line segment would appear on the line connecting Points 2 and 3.
Suppose we call it (x4, y4).

Both of these representations of the slope of the line must be the same:

y3 - y2 y4 - y2
------- = -------
x3 - x2 x4 - x2
Use this information find and output the point (x4, y4) and the triangle area computed from it.
(Your program would then output four area calculations instead of three.)

At least give me the answer for the first question:

The question is:

One would expect that since all the integers in the third batch of vertices have exact representations in the machine, with ho round-off errors, that they should cause the least problems. Yet a glance at the output should see it has the biggest errors. Can you explain why?

Q1: The program does not do any integer math -- its all floats, and may floats can not be represented exactly. What makes you think there are any errors in the computations? Here are the results I got

Please enter the value of x1: 1
Please enter the value of y1: 1
Please enter the value of x2: 15
Please enter the value of y2: 6
Please enter the value of x3: 3
Please enter the value of y3: 11
Area1 is: 65
Area2 is: 65.0000152588
Area3 is: 65
Press any key to continue . . .

Edited 6 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.