determine whether point lies inside triangle

后端 未结 3 618
天命终不由人
天命终不由人 2020-12-12 14:59

The program needs to read the values of three coordinates

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

as well as another coordinate P(x,

3条回答
  •  [愿得一人]
    2020-12-12 15:35

    Take the average of the three given points. This new point P4 will always lie inside the triangle.

    Now check if P and P4 lie on the same side of each of the three lines P1P2 P2P3 and P3P1. You can do this by checking the signs of the cross products (P -> P1) x (P -> P2) and (P4 -> P1) x (P4 -> P2) (where P->P1 is the vector from P to P1), and then the other two pairs.

提交回复
热议问题