**3.27(几何:点是否在三角形内?)假设一个平面上有一个直角三角形。编写程序,提示用户输入一个点的x坐标和y坐标,然后判断这个点是否在该三角形内。 下面是运行示例: Enter a point’s x- and y-coordinates: 100.5 25.5 The point is in the triangle Enter a point’s x- and y-coordinates: 100.5 50.5 The point is not in the triangle
**3.27(Geometry: points in triangle?) Suppose a right triangle is placed in a plane. 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 are the sample runs: Enter a point’s x- and y-coordinates: 100.5 25.5 The point is in the triangle Enter a point’s x- and y-coordinates: 100.5 50.5 The point is not in the triangle
参考代码:
package chapter03; import java.util.Scanner; public class Code_27 { public static void main(String[] args) { double xCoordinate,yCoordinate; System.out.print("Enter a point's x- and y-coordinates: "); Scanner input = new Scanner(System.in); xCoordinate = input.nextDouble(); yCoordinate = input.nextDouble(); // Check if the point is in the triangle if(xCoordinate != 200) { if((-0.5 <= -1 * yCoordinate / (200 - xCoordinate) && -1 * yCoordinate / (200 - xCoordinate) <= 0) && (xCoordinate >= 0 && xCoordinate < 200)) System.out.println("The point is in the triangle"); else System.out.println("The point is not in the triangle"); } else { if(yCoordinate != 0) System.out.println("The point is not in the triangle"); else System.out.println("The point is in the triangle"); } input.close(); } } 结果显示: Enter a point's x- and y-coordinates: 100.5 25.5 The point is in the triangle Process finished with exit code 0