Triangle Check

I am trying to check if a triangle is a regular triangle in Java. This is part of what the tester class does:

    Triangle a = new Triangle(new Point(2, 2), new Point(6, 2), new Point(2, 6) );
    System.out.println(a.isRight()); //Expects True

Part of my triangular class takes values ​​and sets them to x1, y2, etc .:

    public Triangle(Point loc1, Point loc2, Point loc3) {
    x1 =  loc1.getX(); y1 =  loc1.getY();
    . . .

The first two code segments were provided to me. I have to make the last code segment that I did this:

    public boolean isRight() {
        if((Math.pow(side1,2)+Math.pow(side2,2)) == Math.pow(side3,2))
            return true;
        if((Math.pow(side2,2)+Math.pow(side1,2)) == Math.pow(side3,2))
            return true;
        if((Math.pow(side3,2)+Math.pow(side2,2)) == Math.pow(side1,2))
            return true;
        else
            return false;
    }

However, it still returns false. What am I doing wrong? Thanks a bunch!

Update: Thanks for the help. I seem to have made a pretty wrong mistake. I never thought that the doubles were not really accurate (sorry, I'm just trying to say what I did in simple words). I introduced this:

        if(Math.abs(side1*side1 + side2*side2 - side3*side3) < 0.2)
        return true;
    if(Math.abs(side1*side1 + side3*side3 - side2*side2) < 0.2)
        return true;
    if(Math.abs(side3*side3 + side2*side2 - side1*side1) < 0.2)
        return true;
    else
        return false;

Thanks again for the help!

+4
source share
5 answers

I see a number of problems:

  • a 2 + b 2= c 2 b 2 + a 2= c 2
  • , 2 + c 2= b 2.
  • Math.pow , . ( *.)
  • , β€” == β€” .
+6

, == . , :

private static boolean closeEnough(double a, double b) {
    return Math.abs(a - b) < 0x1p-32;
}

, 0x1p-32.

+2

isRight() 3 , , , , . " " , , -.

, . , ( ), , , , . , , , int , , , .

+1

, Side1, Side2 Side3 , , , , . ,

public boolean isRight() {

     return Math.pow(side1, 2) + Math.pow(side2, 2)) == Math.pow(side3, 2) ||
            Math.pow(side1, 2) + Math.pow(side3, 2)) == Math.pow(side2, 2) ||
            Math.pow(side3, 2) + Math.pow(side2, 2)) == Math.pow(side1, 2)

}
+1
source

If your requirement is to check the correct angle based on three different setpoints, you can refer to this below:

First, a class for passing two points. (If you already have the length of the sides, then you can skip this)

public class Point {
private double x;
private double y;

public double getX() {
    return x;
}

public void setX(double x) {
    this.x = x;
}

public double getY() {
    return y;
}

public void setY(double y) {
    this.y = y;
}

public Point(double x, double y) {
    this.x=x;
    this.y=y;
}

}

Class to search if the entered values ​​belong to a right triangle.

public class CheckRightAngledTriangle {

/**
 * Method to check if entered 3 sides belong to right angled triangle
 * 
 * @param a
 * @param b
 * @param c
 * @return
 */
boolean isRightAngledTriangle(Point a, Point b , Point c){
    Double lengthA = getLength(a,b);
    Double lengthB = getLength(b,c);
    Double lengthC = getLength(c,a);
    return  Math.pow(lengthA, 2) + Math.pow(lengthB, 2) == Math.pow(lengthC, 2) ||
            Math.pow(lengthA, 2) + Math.pow(lengthC, 2) == Math.pow(lengthB, 2) ||
            Math.pow(lengthC, 2) + Math.pow(lengthB, 2) == Math.pow(lengthA, 2);
}

/**
 * Method to get length between two points
 * 
 * @param point1
 * @param point2
 * @return
 */
public double getLength(Point point1 , Point point2){
    return Math.sqrt((point2.getX()-point1.getX())*(point2.getX()-point1.getX()) + (point2.getY()-point1.getY())*(point2.getY()-point1.getY()));

}

/**
 * Main method
 * 
 * @param args
 */
public static void main(String[] args) {
    Point pointA = new Point(5, 0);
    Point pointB = new Point(0, 0);
    Point pointC = new Point(1.8, 2.4);
    CheckRightAngledTriangle angledTriangle = new CheckRightAngledTriangle();
    System.out.println("Result is :"+angledTriangle.isRightAngledTriangle(pointA,pointB,pointC));

}
0
source

Source: https://habr.com/ru/post/1568261/


All Articles