Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not.
For example, consider the following program, the function should return true for P(10, 15) and false for P'(30, 15)
B(10,30) / \ / \ / \ / P \ P' / \ A(0,0) ----------- C(20,0)
Let the coordinates of three corners be (x1, y1), (x2, y2) and (x3, y3). And coordinates of the given point P be (x, y)
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.
Exercise: Given coordinates of four corners of a rectangle, and a point P. Write a function to check whether P lies inside the given rectangle or not.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- How to check if a given point lies inside or outside a polygon?
- Check whether a given point lies on or inside the rectangle | Set 3
- Check whether a given point lies inside a rectangle or not
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a point lies inside a sphere or not
- Find if a point lies inside a Circle
- Find a point that lies inside exactly K given squares
- Find if a point lies inside, outside or on the circumcircle of three points A, B, C
- Triangle with no point inside
- Check whether the point (x, y) lies on a given line
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Check if a point is inside, outside or on the ellipse
- Check if a point is inside, outside or on the parabola
- Check if a circle lies inside another circle or not
- Program to print a Hollow Triangle inside a Triangle
- Count of integral coordinates that lies inside a Square
- Area of a triangle inside a parallelogram
- Count Integral points inside a Triangle
- Find intersection point of lines inside a section
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle