Math 确定点是否位于三角形内

Math 确定点是否位于三角形内,math,logic,hint,Math,Logic,Hint,程序需要读取三个坐标的值 P1(x1,y1) P2(x2,y2) P3(x3,y3) 以及另一个坐标p(x,y),并确定该点是否位于由上述3个点构成的三角形内。取三个给定点的平均值。这个新点P4将始终位于三角形内 现在检查p和P4是否位于三行中每一行的同一侧P1P2P2P3和P3P1。您可以通过检查叉积(P->P1)x(P->P2)和(P4->P1)x(P4->P2)(其中P->P1是从P到P1的向量)的符号,然后检查其他两对。正确的方法是计算给定三角形三个点的第四个点的符号。计算它们的公式

程序需要读取三个坐标的值

  • P1(x1,y1)
  • P2(x2,y2)
  • P3(x3,y3)

以及另一个坐标p(x,y),并确定该点是否位于由上述3个点构成的三角形内。

取三个给定点的平均值。这个新点P4将始终位于三角形内


现在检查p和P4是否位于三行中每一行的同一侧
P1P2
P2P3
P3P1
。您可以通过检查叉积
(P->P1)x(P->P2)
(P4->P1)x(P4->P2)
(其中P->P1是从P到P1的向量)的符号,然后检查其他两对。

正确的方法是计算给定三角形三个点的第四个点的符号。计算它们的公式在“转换为重心坐标”一节的末尾给出,但我希望在这里提供一个不太数学化的解释

为简单起见,假设您有一个结构,
point
,其值为
x
y
。您将您的点定义为:

point p1(x1, y1);
point p2(x2, y2);
point p3(x3, y3);

point p(x,y); // <-- You are checking if this point lies in the triangle.
如果所有
alpha
beta
gamma
都大于
0
,则点
p
位于由点
p1
p2
p3
组成的三角形内

这背后的解释是,可以使用三角形的点和三个系数(每个点一个,范围为[0,1])来描述三角形内的点:

重新排列这个函数会给出计算重心坐标的公式,但我觉得这样做的步骤可能超出了问题的范围。它们在我链接到顶部的维基百科页面上提供


因此,每个系数必须大于0,才能使点
p
位于三个点所描述的区域内。

假设点为A、B和C,而不是P1、P2和P3

          A(10,30)
            / \
           /   \
          /     \
         /   P   \      P'
        /         \
B (0,0) ----------- C(20,0) 
算法:

1) Calculate area of the given triangle, i.e., area of the triangle ABC in the above diagram. 

    Area A = [ x1(y2 - y3) + x2(y3 - y1) + x3(y1-y2)]/2

2) Calculate area of the triangle PAB. We can use the same formula for this. Let this area be A1.
3) Calculate area of the triangle PBC. Let this area be A2.
4) Calculate area of the triangle PAC. Let this area be A3.
5) If P lies inside the triangle, then A1 + A2 + A3 must be equal to A.
下面给出的是一个C语言程序:

#include <stdio.h>
#include <stdlib.h>

/* A utility function to calculate area of triangle formed by (x1, y1), 
   (x2, y2) and (x3, y3) */
float area(int x1, int y1, int x2, int y2, int x3, int y3)
{
   return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}

/* A function to check whether point P(x, y) lies inside the triangle formed 
   by A(x1, y1), B(x2, y2) and C(x3, y3) */
bool isInside(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{   
   /* Calculate area of triangle ABC */
   float A = area (x1, y1, x2, y2, x3, y3);

   /* Calculate area of triangle PBC */  
   float A1 = area (x, y, x2, y2, x3, y3);

   /* Calculate area of triangle PAC */  
   float A2 = area (x1, y1, x, y, x3, y3);

   /* Calculate area of triangle PAB */   
   float A3 = area (x1, y1, x2, y2, x, y);

   /* Check if sum of A1, A2 and A3 is same as A */
   return (A == A1 + A2 + A3);
}

/* Driver program to test above function */
int main()
{
   /* Let us check whether the point P(10, 15) lies inside the triangle 
      formed by A(0, 0), B(20, 0) and C(10, 30) */
   if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
     printf ("Inside");
   else
     printf ("Not Inside");

   return 0;
}
#包括
#包括
/*计算由(x1,y1)形成的三角形面积的效用函数,
(x2,y2)和(x3,y3)*/
浮动区(整数x1、整数y1、整数x2、整数y2、整数x3、整数y3)
{
返回abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2.0;
}
/*用于检查点P(x,y)是否位于形成的三角形内的函数
通过A(x1,y1),B(x2,y2)和C(x3,y3)*/
布尔isInside(整数x1、整数y1、整数x2、整数y2、整数x3、整数y3、整数x、整数y)
{   
/*计算三角形ABC的面积*/
浮点数A=面积(x1,y1,x2,y2,x3,y3);
/*计算三角形PBC*的面积
浮动A1=面积(x,y,x2,y2,x3,y3);
/*计算三角形的面积PAC*/
浮点数A2=面积(x1,y1,x,y,x3,y3);
/*计算三角形PAB的面积*/
浮点数A3=面积(x1,y1,x2,y2,x,y);
/*检查A1、A2和A3之和是否与A相同*/
返回(A==A1+A2+A3);
}
/*用于测试上述功能的驱动程序*/
int main()
{
/*让我们检查点P(10,15)是否位于三角形内
由A(0,0),B(20,0)和C(10,30)组成*/
if(isInside(0,0,20,0,10,30,10,15))
printf(“内部”);
其他的
printf(“非内部”);
返回0;
}
时间:O(1)



空格:O(1)

从什么地方读?为什么C++?这是作业吗?这是简单的数学,可能属于程序员或计算机科学。另外,你们似乎也有一个只要求解决家庭作业问题的名声。阅读whathaveyoutried.com,在谷歌上搜索“告诉一个点是否在三角形内”,这样似乎会阻止lmgtfy链接:(@Ben这不是一个家庭作业问题,这是我在我的IT书中找到的一个练习。我不仅仅要求解决问题,我还要求得到一个提示,就像标题所说的那样,你是如何计算这三个点的平均值的?我们这里有一个点的x和ycase@KevinKZ
((P1_x+P2_x+P3_x)/3,(P1_y+P2_y+P3_y)/3)
我想到了另一个想法:如果大三角形的面积等于较小三角形形成的面积之和(在这种情况下,较小三角形是由两个点(如P1、P2或P3和点P)形成的三角形)那么这意味着点位于三角形内。我说得对吗?这行吗?@KevinKZ是的,我想是的。它会说周长上的一点在三角形内。为了找到面积。我找到了另一个,我认为它更简单,使用英雄公式和距离公式。你认为呢?记住,我仍然是在高中的时候,很多关于重心坐标的事情我完全不知道。无论如何,谢谢:)这就是为什么我希望在答案中提供一个更清晰的解释:)你的解释真的很有帮助,我喜欢这个公式,我认为它不太复杂,我会尝试一下。感谢您的帮助:)请注意,计算alpha时公式中有一个错误,您将计算det(T)(请参阅:)。i、 例如,分母中的符号错误。如果分母为零,则有一个共线点,即三角形两点之间的直线上的点。为了解决这个问题,你需要单独计算分母,检查分母是否为零,并进行适当的处理。在比较浮点类型与
=
时要小心。在大多数情况下,如果我们事先知道所有输入点都是整数,那么在计算面积时,我们可以跳过除以2的步骤,这将消除浮点精度问题。但是如果点不是整数,那么我们可以在允许误差的区域上下定义一个小值ε。我非常喜欢这种方法。难道不可能有一个点P'与三角形内点的面积相同吗?
1) Calculate area of the given triangle, i.e., area of the triangle ABC in the above diagram. 

    Area A = [ x1(y2 - y3) + x2(y3 - y1) + x3(y1-y2)]/2

2) Calculate area of the triangle PAB. We can use the same formula for this. Let this area be A1.
3) Calculate area of the triangle PBC. Let this area be A2.
4) Calculate area of the triangle PAC. Let this area be A3.
5) If P lies inside the triangle, then A1 + A2 + A3 must be equal to A.
#include <stdio.h>
#include <stdlib.h>

/* A utility function to calculate area of triangle formed by (x1, y1), 
   (x2, y2) and (x3, y3) */
float area(int x1, int y1, int x2, int y2, int x3, int y3)
{
   return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}

/* A function to check whether point P(x, y) lies inside the triangle formed 
   by A(x1, y1), B(x2, y2) and C(x3, y3) */
bool isInside(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{   
   /* Calculate area of triangle ABC */
   float A = area (x1, y1, x2, y2, x3, y3);

   /* Calculate area of triangle PBC */  
   float A1 = area (x, y, x2, y2, x3, y3);

   /* Calculate area of triangle PAC */  
   float A2 = area (x1, y1, x, y, x3, y3);

   /* Calculate area of triangle PAB */   
   float A3 = area (x1, y1, x2, y2, x, y);

   /* Check if sum of A1, A2 and A3 is same as A */
   return (A == A1 + A2 + A3);
}

/* Driver program to test above function */
int main()
{
   /* Let us check whether the point P(10, 15) lies inside the triangle 
      formed by A(0, 0), B(20, 0) and C(10, 30) */
   if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
     printf ("Inside");
   else
     printf ("Not Inside");

   return 0;
}