Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查找点是否在三角形内_Java_Geometry - Fatal编程技术网

Java 查找点是否在三角形内

Java 查找点是否在三角形内,java,geometry,Java,Geometry,我已经研究了几个小时,尝试不同的方法来研究几乎每个问题。也许我完全错了,但我觉得我的数学计算是正确的,但无论我输入什么数字,我都会得到相同的输出。我的密码在某个地方被关闭了,我必须在午夜前把它交上来 这是一个非常有趣的问题:找出一个点是否在三角形代码内。(适用于初学者) 如果你画一幅画,你可以看到点必须满足简单的不等式(在某些直线的右下方/上方/右侧)。无论“在边缘”是进入还是退出,我将留给您: Y > 0 (above the X axis) X > 0 (to the right

我已经研究了几个小时,尝试不同的方法来研究几乎每个问题。也许我完全错了,但我觉得我的数学计算是正确的,但无论我输入什么数字,我都会得到相同的输出。我的密码在某个地方被关闭了,我必须在午夜前把它交上来

这是一个非常有趣的问题:找出一个点是否在三角形代码内。(适用于初学者)


如果你画一幅画,你可以看到点必须满足简单的不等式(在某些直线的右下方/上方/右侧)。无论“在边缘”是进入还是退出,我将留给您:

Y > 0 (above the X axis)
X > 0 (to the right of the Y axis)
X + 2* Y < 200 (below the hypotenuse)
Y>0(X轴上方)
X>0(在Y轴的右侧)
X+2*Y<200(斜边以下)
围绕这三个问题写一个if语句,你就完成了:

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");
if((y>0)和&(x>0)和&(x+2*y<200))
System.out.println(“点在三角形中”);
其他的
System.out.println(“点不在三角形中”);

您在公式中输入了错误的值顺序;因此,结果是错误的。如果3个顶点如下所示:

A(x1, y1) B(x2, y2), C(x3, y3)
然后将面积计算为

double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2;
在这之后,您只需将每个顶点替换为输入点,我们将得到以下三角形:PBC、APC、ABP

把所有的东西放在一起,我们会得到正确的

int x1 = 0, y1 = 0;
int x2 = 0, y2 = 100;
int x3 = 200, y3 = 0;

// no need to divide by 2.0 here, since it is not necessary in the equation
double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
double ABP = Math.abs (x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2));
double APC = Math.abs (x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y));
double PBC = Math.abs (x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2));

boolean isInTriangle = ABP + APC + PBC == ABC;

作为调试的一部分,输出您认为读入的值可能是值得的……我非常感谢您。我总是想得太多了。你让我的周末过得很愉快!
int x1 = 0, y1 = 0;
int x2 = 0, y2 = 100;
int x3 = 200, y3 = 0;

// no need to divide by 2.0 here, since it is not necessary in the equation
double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
double ABP = Math.abs (x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2));
double APC = Math.abs (x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y));
double PBC = Math.abs (x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2));

boolean isInTriangle = ABP + APC + PBC == ABC;