如何使用坐标检查点是否位于三角形内部?

假设将一个直角三角形放置在一个平面上,如下所示。直角点放置在(0, 0)处,另外两个点放置在(200, 0)和(0, 100)处。编写一个程序,提示用户输入带有 x 和 y 坐标的点,并确定该点是否在三角形内部。


    `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


白板的微信
浏览 117回答 2
2回答

阿晨1998

基本上你有一个线性公式 y = 100 - x/2 其中 x 在 0 到 200 之间,所以我们可以为此创建简单的方法static double calculateY(double x) {&nbsp; &nbsp; return 100.0 - x / 2.0;}然后将 x 与边界进行比较,将 y 与公式进行比较&nbsp;if (x < 0 || x > 200 || y < 0) {&nbsp; &nbsp; &nbsp;System.out.print("The point isn't in the triangle");&nbsp;} else if ( y <= calculateY(x)) {&nbsp; &nbsp; &nbsp;System.out.print("The point is in the the triangle");&nbsp;} else&nbsp; &nbsp; &nbsp;System.out.print("The point isn't in the triangle");&nbsp;}

慕慕森

考虑一个顶点位于 (0, 0)、(1, 0) 和 (0, 1) 的三角形。编写一个程序,询问用户 x 和 y 坐标,然后输出该点是在三角形内部、在三角形边界上还是在三角形外部。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java