Need Help with triangle area calculator problem

Hi, I needed help with the following problem for an assignment.

Write a C++ program that reads in three points from the keyboard
(x1,y1), (x2,y2), and (x3,y3). The program then displays on the screen the area of the triangle formed by these three points. The output of the program should be formatted to 3 decimal digits. If the area of the triangle is zero, the program should instead display a message saying that the three points are aligned in a straight line, and display the length of such line.

I'm having trouble with the distance formula that I'm using for the distance1, distance2, and distance3 lengths. I'm trying to use the Heron's formula to find the area of the triangle. I'm doing this by first finding the 3 side lengths using the distance formula for each and then using the Heron's formula which can be seen explained in more detail at the following link:

http://fahad-cprogramming.blogspot.ca/2013/02/cplusplus-program-to-find-the-area-of-triangle-using-herons-fromula.html

Edited

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
44
45
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double x1, y1, x2, y2, x3, y3, area, distance1, distance2, distance3, s;
	
	cout << "Enter value for x1: ";
		cin >> x1;

	cout << "Enter value for y1: ";
		cin >> y1;

	cout << "Enter value for x2: ";
		cin >> x2;

	cout << "Enter value for y2: ";
		cin >> y2;

	cout << "Enter value for x3: ";
		cin >> x3;

	cout << "Enter the value for y3: ";
		cin >> y3;

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

		distance2 = sqrt((x3 - x2)*(x3 - x2)) + ((y3 - y2)*(y3 - y2));

		distance3 = sqrt((x3 - x1)*(x3 - x1)) + ((y3 - y1)*(y3 - y1));

		s = (distance1 + distance2 + distance3) / 2;

	area = sqrt(s*(s - distance1)*(s - distance2)*(s - distance3));

		if (area > 0)
			cout << "The area of the triangle is: " << area;

		else if (area == 0)
			cout << "The points are aligned in a straight line.";
			cout << "The length of the such line is: " << distance1;
			return 0;
}
Last edited on
closed account (48T7M4Gy)
The problem is ^ is not the way to square a number in C++ use pow() or x*x
Thanks for helping me solve the squaring problem. But, could you or anyone maybe tell me how to change the code or fix it to make it work right as currently it doesn't work the way it should. Could anyone read over the problem and then maybe modify my code and repost it, telling me where I went wrong. Thanks.
closed account (48T7M4Gy)
If you want to square (a1 - a2) for example it is (a2-x1)*(a2-x1) which you can use to replace (a1 -a2)^2 in your code.

Alternatively if you want to use the more complicated pow() function the read http://www.cplusplus.com/reference/cmath/pow/

You may as well do that for yourself. If you still have trouble show us your revised code and we'll help if we can. :)
I have posted the revised code replacing the original one which fixed the square problem. The program runs but doesn't work the way it should which is what I need help on now. Thanks.
closed account (48T7M4Gy)
OK I'll run it and get back, but it would help if you explain what went wrong.

eg What values for x1, x2 and x3 did you use?

What output was there? Numbers, error messages?

In other words, what did you expect, did you check any answers on a calculator?

Debugging and testing is a sweat??
closed account (48T7M4Gy)
As a start, you have the brackets in the wrong place

should be sqrt ( (...) *(...) + (...)*(...) )

eg distance1 = sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) );
Last edited on
So, the problem I'm getting is that once I put in the values for the x and y's, its gives me the area but right after, it tells me the length of a side which is not required. The length should only be displayed if the area is equal to zero. It is explained more clearly in the problem that I posted, it describes how it should work. I really appreciate that your trying to solve the problem.

Write a C++ program that reads in three points from the keyboard
(x1,y1), (x2,y2), and (x3,y3). The program then displays on the screen the area of the triangle formed by these three points. The output of the program should be formatted to 3 decimal digits. If the area of the triangle is zero, the program should instead display a message saying that the three points are aligned in a straight line, and display the length of such line.

Thanks.
Last edited on
closed account (48T7M4Gy)
Try this, note the brackets and that there is only a single area test required (forgetting about -ve areas unless you want to add to that line in the if statement)

1
2
3
4
5
6
7
if (area == 0) // area <= 0 maybe
{
   cout << "The points are aligned in a straight line.";
   cout << "The length of the line is: " << distance1;
}
else
   cout << "The area of the triangle is: " << area;


[BTW You might need to check that distance1 is in fact the length of the line between the collinear points.]
Last edited on
Thank you so much! I tried it and I think it works fine. I really appreciate it. Thanks.
closed account (48T7M4Gy)
Cheers :)
Yea I was wondering if theres a better way of fixing that as distance1 only seems to be the distance between (x1,y1) and (x2,y2). Did you try running the entire the program? Would u no how it would be possible to show the length of the straight line if the area is zero? Thanks.
closed account (48T7M4Gy)
I ran the program and knew it was working after the bracket change and that it gave the right result for the area.

I didn't take much notice of collinearity so I haven't checked that aspect.

The problem with the straight line is there are three sub-lines on the main-line and they might overlap. I haven't put my mind to sorting that problem out. It will require the program to sort out the relative positions of the points on the line or it might better be checked as the greatest distance between any two points, ie max(distance1, 2 or 3) which is easier to determine.

Have a go. :)
Topic archived. No new replies allowed.