You're having it the wrong way around: your function says it's a triangle when it's not and says it's not a triangle when it is.

Well one problem is:

double r=(c.x-a.x)/ab.x;

This can be a divide-by-zero.

This triangle test can be done without a division. Are you familiar with the cross-product? If two vectors have a cross-product equal to zero, they are collinear, and thus cannot form a triangle.

I don't see how that is, Aranarth. It says all sets of points are collinear, no matter what. It hasn't once said that a set of points were a triangle. And mike, that's not my code, that's the code that Aranarth posted. I'll re post my code.

#include <iostream>
#include <cmath>
#include "PointHeader.h"
using namespace std;

int triangle_test (point,point,point);
double perimeter (point,point,point);
double area (double,point,point,point);

int main()
{
    double p;
    double a;
    point point1;
    point point2;
    point point3;
    cout << "Point 1:" << endl;
    point1.prompt_values();
    cout << "Point 2:" << endl;
    point2.prompt_values();
    cout << "Point 3:" << endl;
    point3.prompt_values();
    if ( triangle_test ( point1, point2, point3 ) == 1 )
    {
        cout << "The points form a triangle" << endl;
        p = perimeter ( point1, point2, point3 );
        cout << "The perimeter of the triangle is " << p << endl;
        a = area ( p, point1, point2, point3 );
        cout << "The area of the triangle is " << a << endl;
    }
    else cout << "The points you entered are colinear" << endl;
    cin.ignore();
    cin.get();
    return 0;
}

int triangle_test ( point point1, point point2, point point3 )
{
    double side1 = point1.distance ( point2 );
    double side2 = point2.distance ( point3 );
    double side3 = point3.distance ( point1 );
    if ( side1 > side2 + side3 + 0.00000001 || side2 > side3 + side1 + 0.00000001 || side3 > side1 + side2 + 0.00000001 ) return 1;
    else return 0;
}
double perimeter ( point point1, point point2, point point3 )
{
    return point1.distance(point2)+point2.distance(point3)+point3.distance(point1);
}
double area ( double p, point point1, point point2, point point3 )
{
    double side1 = point1.distance ( point2 );
    double side2 = point2.distance ( point3 );
    double side3 = point3.distance ( point1 );
    return sqrt ( p * ( p - side1 ) * ( p - side2 ) * ( p - side3 ) );
}

Well. If the points are colinear, then one "side" equals the sum of the other two.
I don't think it is ever possible that the long side is greater, so the function will always return 0. If you subtract the delta value instead of adding it you're handling the case that both operands are equal correctly - still, when the condition is fulfilled, it's not a triangle and you need to return false (not true/1).

return !(side1 > side2 + side3 - 0.00000001 || side2 > side3 + side1 - 0.00000001 || side3 > side1 + side2 - 0.00000001);

The return type of triangle_test is still wrong, by the way. It should be bool.

This can be a divide-by-zero.

True. The line before that was supposed to prevent this, but I realized it doesn't.
Calculating the cross-product would be more straight-forward, but apparently the OP isn't allowed to use alternative methods anyway.

Man... this is just a typo:

//this line:
if ( triangle_test ( point1, point2, point3 ) == 1 )
//should be this
if ( triangle_test ( point1, point2, point3 ) == 0 ) //notice 0 not 1!

Aranarth is right, this test for triangle only makes sense if you have three sides to start with, not three points. For three points, it will never be greater.

So with both these corrections. I think you'll be set.

Well, making sense doesn't really matter, I'm just following the homework prompt. And no, that isn't a typo, it's supposed to test to see if it is a triangle (equals 1). And yes, it can be greater than the sum of the two other sides, that's the only way it can be a triangle.

And yes, it can be greater than the sum of the two other sides, that's the only way it can be a triangle.

Since you seem to know it better, I guess you shouldn't need any help on this...
but I'll give you three points to think about:
A(0|0|0) B(0|1|0) C(1|0|0)
a) do they form a triangle?
b) if so, what are the lengths of its sides and the ratio to the sum of the other sides?

If you hadn't been an asshole, Aranarth, I never would have caught the problem: It's supposed to be testing if the sum of the two sides is greater than the other, not vice versa. Yet another one of my stupid mistakes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.