Check whether a given point lies inside a Triangle


Three points of a triangle are given; another point P is also given to check whether the point P is inside the triangle or not.

To solve the problem, let consider the points of the triangle are A, B, and C. When the area of triangle Δ𝐴𝐵𝐶 = Δ𝐴𝐵𝑃 + Δ𝑃𝐵𝐶 + Δ𝐴𝑃𝐶, then the point P is inside the triangle.

Input and Output

Input:
Points of the triangle {(0, 0), (20, 0), (10, 30)} and point p (10, 15) to check.
Output:
Point is inside the triangle.

Algorithm

isInside(p1, p2, p3, p)

Input: Three points of a triangle, the point p to check.

Output: True, when p is inside the triangle.

Begin
   area := area of triangle(p1, p2, p3)
   area1 := area of triangle(p, p2, p3)
   area2 := area of triangle(p1, p, p3)
   area3 := area of triangle(p1, p2, p)
   if area = (area1 + area2 + area3), then
      return true
   else return false
End

Example

#include <iostream>
#include<cmath>
using namespace std;

struct Point {
   int x, y;
};

float triangleArea(Point p1, Point p2, Point p3) {         //find area of triangle formed by p1, p2 and p3
   return abs((p1.x*(p2.y-p3.y) + p2.x*(p3.y-p1.y)+ p3.x*(p1.yp2.y))/2.0);
}

bool isInside(Point p1, Point p2, Point p3, Point p) {     //check whether p is inside or outside
   float area = triangleArea (p1, p2, p3);          //area of triangle ABC
   float area1 = triangleArea (p, p2, p3);         //area of PBC
   float area2 = triangleArea (p1, p, p3);         //area of APC
   float area3 = triangleArea (p1, p2, p);        //area of ABP

   return (area == area1 + area2 + area3);        //when three triangles are forming the whole triangle
}
 
int main() {
   Point p1={0, 0}, p2={20, 0}, p3={10, 30};
   Point p = {10, 15};
   if (isInside(p1, p2, p3, p))
      cout << "Point is inside the triangle.";
   else
      cout << "Point is not inside the triangle";
}

Output

Point is inside the triangle.

Updated on: 17-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements