查找一个点是否在三角形内 [英] Finding whether a point is within a triangle

查看:144
本文介绍了查找一个点是否在三角形内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这上面待了好几个小时,试着用不同的方法来看待每一个问题。也许我完全错了,但我觉得我的数学是正确的,但无论输入什么数字,我都会得到相同的输出。我的代码已关闭,我必须在午夜之前将其关闭。



它非常有趣:查找一个点是否在三角形代码中。 (适用于初学者)

$ $ p $ import java.util.Scanner;

public class PointsTriangle {

//检查输入的点是否在三角形内
//给定的三角形点是(0,0)(0,100)( 200,0)
public static void main(String [] args){
//从用户$ b $获取点(x,y)System.out.print(输入一个点的x-和y坐标:);
扫描仪输入=新扫描仪(System.in);
double x = input.nextDouble();
double y = input.nextDouble();

//找到给定点的三角形面积
double ABC =((0 *(100-0)+ 0 *(0-0)+ 200 *(0-100)) /2.0);
double PAB =((x *(0-100)+ 0 *(100-y)+0 *(y-0))/2.0);
double PBC =((x *(100-0)+ 0 *(0-y)+ 200 *(y-100))/2.0);
double PAC =((x *(0-100)+ 0 *(100-y)+ 200 *(y-0))/2.0);

boolean isInTriangle = PAB + PBC + PAC == ABC;

if(isInTriangle)
System.out.println(点在三角形中);
else
System.out.println(该点不在三角形中);
} //结束主
} //结束PointsTriangle


解决方案

如果您绘制图片,您可以看到该点必须满足简单的不等式(在特定线条右侧的下方/上方/)。无论是在边缘还是在外,我都会留给你:

  Y> 0(在X轴上方)
X> 0(在Y轴的右边)
X + 2 * Y < 200(在斜边以下)

在这三个地方写一条if语句, ((y> 0)&(x> 0)&&(x + 2 * y))(b>

  < 200))
System.out.println(点在三角形中);
else
System.out.println(该点不在三角形中);


I have been on this for hours, attempting different methods looking at just about every question. Perhaps I have it completely wrong, but I feel that I have my math of it correct, but no matter what numbers I input, I get the same output. My code is off somewhere and I have to turn it in by midnight.

It is the all so fun: Find if a point is within a triangle code. (for beginners)

import java.util.Scanner;

public class PointsTriangle {

    // checks if point entered is within the triangle
    //given points of triangle are (0,0) (0,100) (200,0)
    public static void main (String [] args) {
        //obtain point (x,y) from user
        System.out.print("Enter a point's x- and y-coordinates: ");
        Scanner input = new Scanner(System.in);
        double x = input.nextDouble();
        double y = input.nextDouble();

        //find area of triangle with given points
        double ABC = ((0*(100-0  )+0*(0  -0)+200*(0-100))/2.0);
        double PAB = ((x*(0  -100)+0*(100-y)+0  *(y-  0))/2.0);
        double PBC = ((x*(100-0  )+0*(0  -y)+200*(y-100))/2.0);
        double PAC = ((x*(0  -100)+0*(100-y)+200*(y-  0))/2.0);

        boolean isInTriangle = PAB + PBC + PAC == ABC;

        if (isInTriangle)
            System.out.println("The point is in the triangle");
        else
            System.out.println("The point is not in the triangle");
    }//end main
}//end PointsTriangle

解决方案

If you draw a picture, you can see the point has to satisfy simple inequalities (below / above / to the right of certain lines). Whether "on the edge" is in or out I will leave up to you:

Y > 0 (above the X axis)
X > 0 (to the right of the Y axis)
X + 2* Y < 200 (below the hypotenuse)

Write an if statement around these three and you're done:

if( (y > 0) && (x > 0) && (x + 2*y < 200) ) 
  System.out.println("The point is in the triangle");
else
  System.out.println("The point is not in the triangle");

这篇关于查找一个点是否在三角形内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆