Open In App

Check whether triangle is valid or not if three points are given

Improve
Improve
Like Article
Like
Save
Share
Report

Given coordinates of three points in a plane P1, P2 and P3, the task is to check if the three points form a triangle or not
Examples: 
 

Input: P1 = (1, 5), P2 = (2, 5), P3 = (4, 6) 
Output: Yes
Input: P1 = (1, 1), P2 = (1, 4), P3 = (1, 5) 
Output: No 
 


 


Approach: The key observation in the problem is three points form a triangle only when they don’t lie on the straight line, that is an area formed by the triangle of these three points is not equal to zero. 
\text{Area of Triangle }= \frac{1}{2}*(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
The above formula is derived from shoelace formula.
So we will check if the area formed by the triangle is zero or not.
Below is the implementation of the above approach: 
 

C++

// C++ implementation to check
// if three points form a triangle
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if three
// points make a triangle
void checkTriangle(int x1, int y1, int x2,
                   int y2, int x3, int y3)
{
 
    // Calculation the area of
    // triangle. We have skipped
    // multiplication with 0.5
    // to avoid floating point
    // computations
    int a = x1 * (y2 - y3)
            + x2 * (y3 - y1)
            + x3 * (y1 - y2);
 
    // Condition to check if
    // area is not equal to 0
    if (a == 0)
        cout << "No";
    else
        cout << "Yes";
}
 
// Driver Code
int main()
{
    int x1 = 1, x2 = 2, x3 = 3,
        y1 = 1, y2 = 2, y3 = 3;
    checkTriangle(x1, y1, x2,
                  y2, x3, y3);
    return 0;
}

                    

Java

// Java implementation to check
// if three points form a triangle
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function to check if three
// points make a triangle
static void checkTriangle(int x1, int y1,
                          int x2, int y2,
                          int x3, int y3)
{
 
    // Calculation the area of
    // triangle. We have skipped
    // multiplication with 0.5
    // to avoid floating point
    // computations
    int a = x1 * (y2 - y3) +
            x2 * (y3 - y1) +
            x3 * (y1 - y2);
 
    // Condition to check if
    // area is not equal to 0
    if (a == 0)
        System.out.println("No");
    else
        System.out.println("Yes");
}
 
// Driver code
public static void main(String[] args)
{
    int x1 = 1, y1 = 1,
        x2 = 2, y2 = 2,
        x3 = 3, y3 = 3;
    checkTriangle(x1, y1, x2, y2, x3, y3);
}
}
 
// This code is contributed by coder001

                    

Python3

# Python3 implementation to check
# if three points form a triangle
 
# Function to check if three
# points make a triangle
def checkTriangle(x1, y1, x2, y2, x3, y3):
     
    # Calculation the area of
    # triangle. We have skipped
    # multiplication with 0.5
    # to avoid floating point
    # computations
    a = (x1 * (y2 - y3) +
         x2 * (y3 - y1) +
         x3 * (y1 - y2))
         
    # Condition to check if
    # area is not equal to 0
    if a == 0:
        print('No')
    else:
        print('Yes')
         
# Driver code
if __name__=='__main__':
     
    (x1, x2, x3) = (1, 2, 3)
    (y1, y2, y3) = (1, 2, 3)
     
    checkTriangle(x1, y1, x2, y2, x3, y3)
     
# This code is contributed by rutvik_56

                    

C#

// C# implementation to check
// if three points form a triangle
using System;
 
class GFG {
     
// Function to check if three
// points make a triangle
static void checkTriangle(int x1, int y1,
                          int x2, int y2,
                          int x3, int y3)
{
    // Calculation the area of
    // triangle. We have skipped
    // multiplication with 0.5
    // to avoid floating point
    // computations
    int a = x1 * (y2 - y3) +
            x2 * (y3 - y1) +
            x3 * (y1 - y2);
 
    // Condition to check if
    // area is not equal to 0
    if (a == 0)
        Console.WriteLine("No");
    else
        Console.WriteLine("Yes");
}
 
// Driver code
public static void Main()
{
    int x1 = 1, y1 = 1,
        x2 = 2, y2 = 2,
        x3 = 3, y3 = 3;
         
    checkTriangle(x1, y1, x2, y2, x3, y3);
}
}
 
//This code is contributed by AbhiThakur

                    

Javascript

<script>
// Javascript implementation to check
// if three points form a triangle
 
// Function to check if three
// points make a triangle
function checkTriangle(x1, y1, x2,
                y2, x3, y3)
{
 
    // Calculation the area of
    // triangle. We have skipped
    // multiplication with 0.5
    // to avoid floating point
    // computations
    let a = x1 * (y2 - y3)
            + x2 * (y3 - y1)
            + x3 * (y1 - y2);
 
    // Condition to check if
    // area is not equal to 0
    if (a == 0)
        document.write("No");
    else
        document.write("Yes");
}
 
// Driver Code
    let x1 = 1, x2 = 2, x3 = 3,
        y1 = 1, y2 = 2, y3 = 3;
    checkTriangle(x1, y1, x2,
                y2, x3, y3);
     
// This code is contributed by Mayank Tyagi
 
</script>

                    

Output
No

Time Complexity: O(1)

Auxiliary Space : O(1)

Approach#2: Using the Triangle Inequality Theorem

One way to check if a triangle is valid is to use the triangle inequality theorem. According to this theorem, the sum of the lengths of any two sides of a triangle must be greater than the length of the third side. Therefore, if we calculate the lengths of all three sides of the triangle and check if this condition is satisfied, we can determine if the triangle is valid or not.

Algorithm

1. Define a function valid_triangle(p1, p2, p3) that takes the three points as input.
2. Calculate the lengths of all three sides of the triangle using the distance formula.
3. Check if the sum of the lengths of any two sides is greater than the length of the third side.
4. If this condition is satisfied for all three combinations of sides, the triangle is valid; otherwise, it is invalid.

C++

#include <bits/stdc++.h>
using namespace std;
 
// Find the distance between two points.
double distance(double p1[], double p2[]) {
    return sqrt(pow(p2[0] - p1[0], 2) + pow(p2[1] - p1[1], 2));
}
 
// Checks whether the triangle is valid,
// by checking if the sum of two sides is greater than the third side.
bool validTriangle(double p1[], double p2[], double p3[]) {
    double d1 = distance(p1, p2);
    double d2 = distance(p2, p3);
    double d3 = distance(p3, p1);
    return d1 + d2 > d3 && d2 + d3 > d1 && d3 + d1 > d2;
}
 
// Example usage
int main() {
    double P1[] = {1, 5};
    double P2[] = {2, 5};
    double P3[] = {4, 6};
    cout << boolalpha << validTriangle(P1, P2, P3) << endl; // Output: true
 
    double P4[] = {1, 1};
    double P5[] = {1, 4};
    double P6[] = {1, 5};
    cout << boolalpha << validTriangle(P4, P5, P6) << endl; // Output: false
 
    return 0;
}

                    

Java

// java code addition
import java.io.*;
 
public class Main {
    // Find the distance between two points.
    public static double distance(double[] p1, double[] p2) {
        return Math.sqrt(Math.pow(p2[0] - p1[0], 2) + Math.pow(p2[1] - p1[1], 2));
    }
 
    // checks whether the triangle is valid,
    // by checking if sum of two sides is greater then the third side.
    public static boolean validTriangle(double[] p1, double[] p2, double[] p3) {
        double d1 = distance(p1, p2);
        double d2 = distance(p2, p3);
        double d3 = distance(p3, p1);
        return d1 + d2 > d3 && d2 + d3 > d1 && d3 + d1 > d2;
    }
 
    // Example usage
    public static void main(String[] args) {
        double[] P1 = {1, 5};
        double[] P2 = {2, 5};
        double[] P3 = {4, 6};
        System.out.println(validTriangle(P1, P2, P3)); // Output: true
 
        double[] P4 = {1, 1};
        double[] P5 = {1, 4};
        double[] P6 = {1, 5};
        System.out.println(validTriangle(P4, P5, P6)); // Output: false
    }
}
 
// The code is contributed by Arushi Goel.

                    

Python3

from math import sqrt
 
def distance(p1, p2):
    return sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
 
def valid_triangle(p1, p2, p3):
    d1 = distance(p1, p2)
    d2 = distance(p2, p3)
    d3 = distance(p3, p1)
    return d1 + d2 > d3 and d2 + d3 > d1 and d3 + d1 > d2
 
# Example usage
P1 = (1, 5)
P2 = (2, 5)
P3 = (4, 6)
print(valid_triangle(P1, P2, P3)) # Output: True
 
P1 = (1, 1)
P2 = (1, 4)
P3 = (1, 5)
print(valid_triangle(P1, P2, P3)) # Output: False

                    

C#

using System;
 
class GFG {
    // Find the distance between two points.
    public static double Distance(double[] p1, double[] p2)
    {
        return Math.Sqrt(Math.Pow(p2[0] - p1[0], 2)
                         + Math.Pow(p2[1] - p1[1], 2));
    }
 
    // Checks whether the triangle is valid,
    // by checking if the sum of two sides is greater than
    // the third side.
    public static bool
    ValidTriangle(double[] p1, double[] p2, double[] p3)
    {
        double d1 = Distance(p1, p2);
        double d2 = Distance(p2, p3);
        double d3 = Distance(p3, p1);
        return d1 + d2 > d3 && d2 + d3 > d1 && d3 + d1 > d2;
    }
 
    // Example usage
    public static void Main(string[] args)
    {
        double[] P1 = { 1, 5 };
        double[] P2 = { 2, 5 };
        double[] P3 = { 4, 6 };
        Console.WriteLine(
            ValidTriangle(P1, P2, P3)); // Output: True
 
        double[] P4 = { 1, 1 };
        double[] P5 = { 1, 4 };
        double[] P6 = { 1, 5 };
        Console.WriteLine(
            ValidTriangle(P4, P5, P6)); // Output: False
    }
}

                    

Javascript

// javascript code addition
 
// Find the distance between two points.
function distance(p1, p2) {
  return Math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2);
}
 
// checks whether the triangle is valid,
// by checking if sum of two sides is greater then the third side.
function validTriangle(p1, p2, p3) {
  const d1 = distance(p1, p2);
  const d2 = distance(p2, p3);
  const d3 = distance(p3, p1);
  return d1 + d2 > d3 && d2 + d3 > d1 && d3 + d1 > d2;
}
 
// Example usage
const P1 = [1, 5];
const P2 = [2, 5];
const P3 = [4, 6];
console.log(validTriangle(P1, P2, P3)); // Output: true
 
const P4 = [1, 1];
const P5 = [1, 4];
const P6 = [1, 5];
console.log(validTriangle(P4, P5, P6)); // Output: false
 
// The code is contributed by Arushi Goel.

                    

Output
True
False

Time Complexity: O(1)
Space Complexity: O(1)



Last Updated : 26 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads