PDA

View Full Version : Algorithm for detecting triangles of integral area



Dara Javaherian
July 4th, 2010, 05:35 AM
Hello. I'm wondering if anyone would have any ideas for a method that returns whether or not a triangle has integral area. This is the code I have(Java):



public static boolean hasIntegralArea(int a, int b, int c){
long cosCh = ((a*a)+(b*b)-(c*c));
long cosCl = (2*a*b);
double cosC = (double)cosCh/(double)cosCl;
double C = Math.acos(cosC);
double result = (a*b*Math.sin(C))/2;
return result%1 == 0;
}


However, this seems to work only on low values (I think due to a loss in precision). Can anyone push me towards a better algorithm?

Dara Javaherian
July 5th, 2010, 12:20 AM
Hello. I'm wondering if anyone would have any ideas for a method that returns whether or not a triangle has integral area. This is the code I have(Java):



public static boolean hasIntegralArea(int a, int b, int c){
long cosCh = ((a*a)+(b*b)-(c*c));
long cosCl = (2*a*b);
double cosC = (double)cosCh/(double)cosCl;
double C = Math.acos(cosC);
double result = (a*b*Math.sin(C))/2;
return result%1 == 0;
}
However, this seems to work only on low values (I think due to a loss in precision). Can anyone push me towards a better algorithm?

Okay, I think I have a better solution:

#include <math.h>
#include <stdio.h>

// Calculate the area of an isoceles trangle given the length of the sides.
// 'side' is the length of the two equal sides, 'difference' is the difference between the unique side and the other two sides
double area(int side, int difference) {
double PI = 3.141592653589793238462643;

// Triangle for reference.
// ^
// /|\
// /B| \
// a/ | \
// / |c \
// / | \
// /C___A|_____\
// b

// Figure out all the angles.
double a = side,
b = (side+difference)/2,
c,
A = PI/2,
B, C,
sinA = sin(A),
sinB, sinC;

sinB = b/(a/sinA);
B = sinh(sinB);
C = PI-B-A;
sinC = sin(C);
c = sinC*(b/sinB);

return b*c;
}
int main(){
printf("%f",area(5,1));
}

However, this gives slight inaccuracies. Can anyone help?

pbrane
July 5th, 2010, 01:36 AM
Maybe this will work better. I'm not sure what you mean about slight inaccuracies. I promoted the variables to doubles. The isIntegral function is using the law of cosines to calculate the area of the triangle. I'm not sure if that will work on all triangles. Your second post's code is for isosceles triangles only.



#include <math.h>
#include <stdio.h>

#define SQR(a) ((a) == 0.0 ? (0.0) : ((a) * (a)))

int isIntegral(double a, double b, double c)
{
double cosCh = SQR(a) + SQR(b) - SQR(c);
double cosCl = (2*a*b);
double cosC = (double)cosCh/(double)cosCl;
double angle = acos(cosC);
double result = (a * b * sin(angle)) / 2.0;
return (fmod(result, 1.0) == 0.0);
}

int main(void)
{
double sideA = 21.05;
double sideB = 18.53;
double sideC = 21.05;
printf("This triangle %s integral area\n", isIntegral(sideA, sideB, sideC) ? "has" : "does not have");

return 0;
}

gmargo
July 5th, 2010, 01:57 AM
You can avoid the trigonometry by using Heron's Formula.
http://mathworld.wolfram.com/HeronsFormula.html