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:
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.
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.
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.
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.]
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.
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.