-1

Suppose a right triangle is placed in a plane as shown below. The right-angle point is placed at (0, 0), and the other two points are placed at (200, 0), and (0, 100). Write a program that prompts the user to enter a point with x- and y-coordinates and determines whether the point is inside the triangle.

    `String xInput, yInput;
    double x, y;
    xInput = JOptionPane.showInputDialog("Enter the x-coordinate of the point");
    yInput = JOptionPane.showInputDialog("Enter the y-coordinate of the point");
    x = Double.parseDouble(xInput);
    y = Double.parseDouble(yInput);
    if (x <= 200 && x >= 0 && y <= 100 && y >= 0) {
        if (y = roundup(x/2))
            System.out.print("The point is in the the triangle");
        else
            System.out.print("The point isn't in the triangle");
    }else
        System.out.print("The point isn't in the triangle");`

The output is an error in the second if saying that a double can't be a boolean

This is the figure for clarifications

2 Answers2

0

Basically you have a linear formula that is y = 100 - x/2 where x is between 0 and 200 so we can create simple method for that

static double calculateY(double x) {
    return 100.0 - x / 2.0;
}

and then compare x against the boundaries and y against the formula

 if (x < 0 || x > 200 || y < 0) {
     System.out.print("The point isn't in the triangle");
 } else if ( y <= calculateY(x)) {
     System.out.print("The point is in the the triangle");
 } else
     System.out.print("The point isn't in the triangle");
 }
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
0

Consider a triangle with vertices at (0, 0), (1, 0), and (0, 1). Write a program that asks the user for x and y coordinates and then outputs whether the point is inside the triangle, on the border of the triangle or outside the triangle.