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.
Example:
Input: A = (0, 0), B = (10, 30), C = (20, 0), P(10, 15)
Output: Inside
Explanation:
B(10,30)
/ \
/ \
/ \
/ P \ P'
/ \
A(0,0) ----------- C(20,0)
Input: A = (0, 0), B = (10, 30), C = (20, 0), P(30, 15)
Output: Outside
Explanation:
B(10,30)
/ \
/ \
/ \
/ \ P
/ \
A(0,0) ----------- C(20,0)
Solution:
Let the coordinates of the three corners be (x1, y1), (x2, y2), and (x3, y3). And coordinates of the given point P be (x, y)
- Calculate the area of the given triangle, i.e., the area of the triangle ABC in the above diagram.
Area A = [ x1(y2 – y3) + x2(y3 – y1) + x3(y1-y2)]/2
- Calculate the area of the triangle PAB. We can use the same formula for this. Let this area be A1.
- Calculate the area of the triangle PBC. Let this area be A2.
- Calculate the area of the triangle PAC. Let this area be A3.
- If P lies inside the triangle, then A1 + A2 + A3 must be equal to A.
C++
#include <bits/stdc++.h>
using namespace std;
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);
}
bool isInside( int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{
float A = area (x1, y1, x2, y2, x3, y3);
float A1 = area (x, y, x2, y2, x3, y3);
float A2 = area (x1, y1, x, y, x3, y3);
float A3 = area (x1, y1, x2, y2, x, y);
return (A == A1 + A2 + A3);
}
int main()
{
if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
cout << "Inside" ;
else
cout << "Not Inside" ;
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
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);
}
bool isInside( int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{
float A = area (x1, y1, x2, y2, x3, y3);
float A1 = area (x, y, x2, y2, x3, y3);
float A2 = area (x1, y1, x, y, x3, y3);
float A3 = area (x1, y1, x2, y2, x, y);
return (A == A1 + A2 + A3);
}
int main()
{
if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
printf ( "Inside" );
else
printf ( "Not Inside" );
return 0;
}
|
Java
import java.util.*;
class GFG {
static double area( int x1, int y1, int x2, int y2,
int x3, int y3)
{
return Math.abs((x1*(y2-y3) + x2*(y3-y1)+
x3*(y1-y2))/ 2.0 );
}
static boolean isInside( int x1, int y1, int x2,
int y2, int x3, int y3, int x, int y)
{
double A = area (x1, y1, x2, y2, x3, y3);
double A1 = area (x, y, x2, y2, x3, y3);
double A2 = area (x1, y1, x, y, x3, y3);
double A3 = area (x1, y1, x2, y2, x, y);
return (A == A1 + A2 + A3);
}
public static void main(String[] args)
{
if (isInside( 0 , 0 , 20 , 0 , 10 , 30 , 10 , 15 ))
System.out.println( "Inside" );
else
System.out.println( "Not Inside" );
}
}
|
Python
def area(x1, y1, x2, y2, x3, y3):
return abs ((x1 * (y2 - y3) + x2 * (y3 - y1)
+ x3 * (y1 - y2)) / 2.0 )
def isInside(x1, y1, x2, y2, x3, y3, x, y):
A = area (x1, y1, x2, y2, x3, y3)
A1 = area (x, y, x2, y2, x3, y3)
A2 = area (x1, y1, x, y, x3, y3)
A3 = area (x1, y1, x2, y2, x, y)
if (A = = A1 + A2 + A3):
return True
else :
return False
if (isInside( 0 , 0 , 20 , 0 , 10 , 30 , 10 , 15 )):
print ( 'Inside' )
else :
print ( 'Not Inside' )
|
C#
using System;
class GFG {
static double area( int x1, int y1, int x2,
int y2, int x3, int y3)
{
return Math.Abs((x1 * (y2 - y3) +
x2 * (y3 - y1) +
x3 * (y1 - y2)) / 2.0);
}
static bool isInside( int x1, int y1, int x2,
int y2, int x3, int y3,
int x, int y)
{
double A = area(x1, y1, x2, y2, x3, y3);
double A1 = area(x, y, x2, y2, x3, y3);
double A2 = area(x1, y1, x, y, x3, y3);
double A3 = area(x1, y1, x2, y2, x, y);
return (A == A1 + A2 + A3);
}
public static void Main()
{
if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
Console.WriteLine( "Inside" );
else
Console.WriteLine( "Not Inside" );
}
}
|
Javascript
<script>
function area(x1, y1, x2, y2, x3, y3)
{
return Math.abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}
function isInside(x1, y1, x2, y2, x3, y3, x, y)
{
let A = area (x1, y1, x2, y2, x3, y3);
let A1 = area (x, y, x2, y2, x3, y3);
let A2 = area (x1, y1, x, y, x3, y3);
let A3 = area (x1, y1, x2, y2, x, y);
return (A == A1 + A2 + A3);
}
if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
document.write( "Inside" );
else
document.write( "Not Inside" );
</script>
|
PHP
<?php
function area( $x1 , $y1 , $x2 ,
$y2 , $x3 , $y3 )
{
return abs (( $x1 * ( $y2 - $y3 ) +
$x2 * ( $y3 - $y1 ) +
$x3 * ( $y1 - $y2 )) / 2.0);
}
function isInside( $x1 , $y1 , $x2 , $y2 ,
$x3 , $y3 , $x , $y )
{
$A = area ( $x1 , $y1 , $x2 , $y2 , $x3 , $y3 );
$A1 = area ( $x , $y , $x2 , $y2 , $x3 , $y3 );
$A2 = area ( $x1 , $y1 , $x , $y , $x3 , $y3 );
$A3 = area ( $x1 , $y1 , $x2 , $y2 , $x , $y );
return ( $A == $A1 + $A2 + $A3 );
}
if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
echo "Inside" ;
else
echo "Not Inside" ;
?>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
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.
Another Approach – Using Barycentric Coordinate Method: Below is the algorithm to check if a point P lies inside a triangle ABC using the Barycentric Coordinate Method:
- Define a function “isInsideTriangle” that takes four input parameters: A, B, C, and P.
- Calculate the barycentric coordinates of point P with respect to the triangle ABC. To do this, we first need to calculate the area of the triangle ABC. We can use the cross product to find the area of the triangle ABC as:
Area(ABC) = 0.5 * ||AB x AC||, where ||AB x AC|| is the magnitude of the cross product of vectors AB and AC.
- Then, we can calculate the barycentric coordinates of point P as:
- a = 0.5 * ||PB x PC|| / Area(ABC)
- b = 0.5 * ||PC x PA|| / Area(ABC)
- c = 0.5 * ||PA x PB|| / Area(ABC), where PB, PC, and PA are vectors from point P to vertices B, C, and A, respectively.
- If all three barycentric coordinates are non-negative, then the point P lies inside the triangle ABC. Return “Inside”. Otherwise, point P lies outside the triangle ABC. Return “Outside”.
Below is the implementation of the above approach:
C++
#include<iostream>
#include<vector>
using namespace std;
string isInsideTriangle(vector< int > A, vector< int > B, vector< int > C, vector< int > P) {
double denominator = ((B[1] - C[1]) * (A[0] - C[0]) + (C[0] - B[0]) * (A[1] - C[1]));
double a = ((B[1] - C[1]) * (P[0] - C[0]) + (C[0] - B[0]) * (P[1] - C[1])) / denominator;
double b = ((C[1] - A[1]) * (P[0] - C[0]) + (A[0] - C[0]) * (P[1] - C[1])) / denominator;
double c = 1 - a - b;
if (a >= 0 && b >= 0 && c >= 0) {
return "Inside" ;
} else {
return "Outside" ;
}
}
int main() {
vector< int > A = {0, 0};
vector< int > B = {10, 30};
vector< int > C = {20, 0};
vector< int > P = {10, 15};
string result = isInsideTriangle(A, B, C, P);
cout << result << endl;
return 0;
}
|
Java
import java.awt.Point;
class Main
{
public static String isInsideTriangle(Point A, Point B, Point C, Point P)
{
double denominator = ((B.y - C.y) * (A.x - C.x) +
(C.x - B.x) * (A.y - C.y));
double a = ((B.y - C.y) * (P.x - C.x) +
(C.x - B.x) * (P.y - C.y)) / denominator;
double b = ((C.y - A.y) * (P.x - C.x) +
(A.x - C.x) * (P.y - C.y)) / denominator;
double c = 1 - a - b;
if (a >= 0 && b >= 0 && c >= 0 ) {
return "Inside" ;
} else {
return "Outside" ;
}
}
public static void main(String[] args) {
Point A = new Point( 0 , 0 );
Point B = new Point( 10 , 30 );
Point C = new Point( 20 , 0 );
Point P = new Point( 10 , 15 );
String result = isInsideTriangle(A, B, C, P);
System.out.println(result);
}
}
|
Python3
def isInsideTriangle(A, B, C, P):
denominator = ((B[ 1 ] - C[ 1 ]) * (A[ 0 ] - C[ 0 ]) +
(C[ 0 ] - B[ 0 ]) * (A[ 1 ] - C[ 1 ]))
a = ((B[ 1 ] - C[ 1 ]) * (P[ 0 ] - C[ 0 ]) +
(C[ 0 ] - B[ 0 ]) * (P[ 1 ] - C[ 1 ])) / denominator
b = ((C[ 1 ] - A[ 1 ]) * (P[ 0 ] - C[ 0 ]) +
(A[ 0 ] - C[ 0 ]) * (P[ 1 ] - C[ 1 ])) / denominator
c = 1 - a - b
if a > = 0 and b > = 0 and c > = 0 :
return "Inside"
else :
return "Outside"
A = ( 0 , 0 )
B = ( 10 , 30 )
C = ( 20 , 0 )
P = ( 10 , 15 )
result = isInsideTriangle(A, B, C, P)
print (result)
|
C#
using System;
public class PointInsideTriangle
{
public static string IsInsideTriangle( int [] A, int [] B, int [] C, int [] P)
{
double denominator = ((B[1] - C[1]) * (A[0] - C[0]) + (C[0] - B[0]) * (A[1] - C[1]));
double a = ((B[1] - C[1]) * (P[0] - C[0]) + (C[0] - B[0]) * (P[1] - C[1])) / denominator;
double b = ((C[1] - A[1]) * (P[0] - C[0]) + (A[0] - C[0]) * (P[1] - C[1])) / denominator;
double c = 1 - a - b;
if (a >= 0 && b >= 0 && c >= 0)
{
return "Inside" ;
}
else
{
return "Outside" ;
}
}
public static void Main( string [] args)
{
int [] A = { 0, 0 };
int [] B = { 10, 30 };
int [] C = { 20, 0 };
int [] P = { 10, 15 };
string result = IsInsideTriangle(A, B, C, P);
Console.WriteLine(result);
}
}
|
Javascript
function isInsideTriangle(A, B, C, P) {
let denominator = ((B[1] - C[1]) * (A[0] - C[0]) +
(C[0] - B[0]) * (A[1] - C[1]));
let a = ((B[1] - C[1]) * (P[0] - C[0]) +
(C[0] - B[0]) * (P[1] - C[1])) / denominator;
let b = ((C[1] - A[1]) * (P[0] - C[0]) +
(A[0] - C[0]) * (P[1] - C[1])) / denominator;
let c = 1 - a - b;
if (a >= 0 && b >= 0 && c >= 0) {
return "Inside" ;
} else {
return "Outside" ;
}
}
let A = [0, 0];
let B = [10, 30];
let C = [20, 0];
let P = [10, 15];
let result = isInsideTriangle(A, B, C, P);
console.log(result);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!