The problem
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. Here is a java examples run:
Enter a point's x- and y-coordinates: 100.5 25.5
The point is in the triangle
Breaking it down
private static final double SLOPE = -0.5D;
public static void main(String[] strings) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a point's x- and y-coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();
input.close();
double y1 = calculateY1(x, y);
if ((x > 200.0D) || (x < 0.0D) || (y > 100.0D) || (y < 0.0D)) {
System.out.println("The point is not in the triangle.");
} else {
if ((y1 <= 100.0D)) {
System.out.println("The point is in the triangle");
} else {
System.out.println("The point is not in the triangle.");
}
}
}
private static double calculateY1(double x, double y) {
double y1 = y + -x * SLOPE;
return y1;
}
Output
Enter a point's x- and y-coordinates: 100.5 50.5
The point is not in the triangle.