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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

No

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.