Check if a right-angled triangle can be formed by moving any one of the coordinates

Given three coordinates of a triangle (x1, y1), (x2, y2), (x3, y3). The task is to find out if the triangle can be transformed to a right-angled triangle by moving only one point exactly by the distance 1.
If it is possible to make the triangle right-angled, then print “POSSIBLE”, else print “NOT POSSIBLE”.

If the triangle is already right-angled, it should also be reported.

Examples:



Input:
x1 = -1, y1 = 0
x2 = 2, y2 = 0
x3 = 0, y3 = 1
Output: POSSIBLE

First co-ordinate (-1, 0) can be changed to (0, 0) and make it right-angled.
Input:
x1 = 36, y1 = 1
x2 = -17, y2 = -54
x3 = -19, y3 = 55
Output: POSSIBLE

Approach:
As it is known that for a triangle of sides a, b and c, the triangle will be right-angled if the following equation holds true : a2 + b2 = c2
So for every co-ordinates of the triangle, find out all the sides and for the 3 possible permutations of them check if it is already right-angle triangle and report it.

If the above condition doesn’t hold true, then the following operations need to be done-
We need to change all the co-ordinates by 1 one by one and check that is it a valid combination for a right-angled triangle.
Look that there can be 4 possible combinations to change every co-ordinates by 1. They are (-1, 0), (0, 1), (1, 0), (0, -1). So run a loop and apply those changes one by one for every co-ordinates and check that the formula a2 + b2 = c2 is true or not.
If it’s true then it is possible to transform the triangle to a right-angled triangle, otherwise not.

Below is the implementation of the above code:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Storing all the possible
// changes to make the triangle
// right-angled
int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, 1, 0, -1 };
  
// Function to check if the triangle
// is right-angled or not
int ifRight(int x1, int y1,
            int x2, int y2,
            int x3, int y3)
{
  
    int a = ((x1 - x2) * (x1 - x2))
            + ((y1 - y2) * (y1 - y2));
  
    int b = ((x1 - x3) * (x1 - x3))
            + ((y1 - y3) * (y1 - y3));
  
    int c = ((x2 - x3) * (x2 - x3))
            + ((y2 - y3) * (y2 - y3));
  
    if ((a == (b + c) && a != 0 && b != 0 && c != 0)
        || (b == (a + c) && a != 0 && b != 0 && c != 0)
        || (c == (a + b) && a != 0 && b != 0 && c != 0)) {
        return 1;
    }
  
    return 0;
}
  
// Function to check if the triangle
// can be transformed to right-angled
void isValidCombination(int x1, int y1,
                        int x2, int y2,
                        int x3, int y3)
{
    int x, y;
  
    // Boolean variable to
    // return true or false
    bool possible = 0;
  
    // If it is already right-angled
    if (ifRight(x1, y1,
                x2, y2,
                x3, y3)) {
        cout << "ALREADY RIGHT ANGLED";
        return;
    }
    else {
  
        // Applying the changes on the
        // co-ordinates
        for (int i = 0; i < 4; i++) {
  
            // Applying on the first
            // co-ordinate
            x = dx[i] + x1;
            y = dy[i] + y1;
  
            if (ifRight(x, y,
                        x2, y2,
                        x3, y3)) {
                cout << "POSSIBLE";
                return;
            }
  
            // Applying on the second
            // co-ordinate
            x = dx[i] + x2;
            y = dy[i] + y2;
  
            if (ifRight(x1, y1,
                        x, y,
                        x3, y3)) {
                cout << "POSSIBLE";
                return;
            }
  
            // Applying on the third
            // co-ordinate
            x = dx[i] + x3;
            y = dy[i] + y3;
  
            if (ifRight(x1, y1,
                        x2, y2,
                        x, y)) {
                cout << "POSSIBLE";
                return;
            }
        }
    }
  
    // If can't be transformed
    if (!possible)
        cout << "NOT POSSIBLE" << endl;
}
  
// Driver Code
int main()
  
{
    int x1 = -49, y1 = 0;
    int x2 = 0, y2 = 50;
    int x3 = 0, y3 = -50;
  
    isValidCombination(x1, y1,
                       x2, y2,
                       x3, y3);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
class GFG
{
  
// Storing all the possible
// changes to make the triangle
// right-angled
static int dx[] = { -1, 0, 1, 0 };
static int dy[] = { 0, 1, 0, -1 };
  
// Function to check if the triangle
// is right-angled or not
static boolean ifRight(int x1, int y1,
                       int x2, int y2,
                       int x3, int y3)
{
    int a = ((x1 - x2) * (x1 - x2)) + 
            ((y1 - y2) * (y1 - y2));
  
    int b = ((x1 - x3) * (x1 - x3)) + 
            ((y1 - y3) * (y1 - y3));
  
    int c = ((x2 - x3) * (x2 - x3)) + 
            ((y2 - y3) * (y2 - y3));
  
    if ((a == (b + c) && a != 0 && b != 0 && c != 0) || 
        (b == (a + c) && a != 0 && b != 0 && c != 0) || 
        (c == (a + b) && a != 0 && b != 0 && c != 0)) 
    {
        return true;
    }
    return false;
}
  
// Function to check if the triangle
// can be transformed to right-angled
static void isValidCombination(int x1, int y1,
                               int x2, int y2,
                               int x3, int y3)
{
    int x, y;
  
    // Boolean variable to
    // return true or false
    boolean possible = false;
  
    // If it is already right-angled
    if (ifRight(x1, y1, x2, y2, x3, y3))
    {
        System.out.print("ALREADY RIGHT ANGLED");
        return;
    }
    else
    {
  
        // Applying the changes on the
        // co-ordinates
        for (int i = 0; i < 4; i++) 
        {
  
            // Applying on the first
            // co-ordinate
            x = dx[i] + x1;
            y = dy[i] + y1;
  
            if (ifRight(x, y, x2, y2, x3, y3)) 
            {
                System.out.print("POSSIBLE");
                return;
            }
  
            // Applying on the second
            // co-ordinate
            x = dx[i] + x2;
            y = dy[i] + y2;
  
            if (ifRight(x1, y1, x, y, x3, y3))
            {
                System.out.print("POSSIBLE");
                return;
            }
  
            // Applying on the third
            // co-ordinate
            x = dx[i] + x3;
            y = dy[i] + y3;
  
            if (ifRight(x1, y1, x2, y2, x, y)) 
            {
                System.out.print("POSSIBLE");
                return;
            }
        }
    }
  
    // If can't be transformed
    if (!possible)
        System.out.println("NOT POSSIBLE");
}
  
// Driver Code
public static void main(String[] args) 
{
    int x1 = -49, y1 = 0;
    int x2 = 0, y2 = 50;
    int x3 = 0, y3 = -50;
  
    isValidCombination(x1, y1, x2, y2, x3, y3);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG 
  
    // Storing all the possible 
    // changes to make the triangle 
    // right-angled 
    static int []dx = { -1, 0, 1, 0 }; 
    static int []dy = { 0, 1, 0, -1 }; 
      
    // Function to check if the triangle 
    // is right-angled or not 
    static bool ifRight(int x1, int y1, 
                        int x2, int y2, 
                        int x3, int y3) 
    
        int a = ((x1 - x2) * (x1 - x2)) + 
                ((y1 - y2) * (y1 - y2)); 
      
        int b = ((x1 - x3) * (x1 - x3)) + 
                ((y1 - y3) * (y1 - y3)); 
      
        int c = ((x2 - x3) * (x2 - x3)) + 
                ((y2 - y3) * (y2 - y3)); 
      
        if ((a == (b + c) && a != 0 && b != 0 && c != 0) || 
            (b == (a + c) && a != 0 && b != 0 && c != 0) || 
            (c == (a + b) && a != 0 && b != 0 && c != 0)) 
        
            return true
        
        return false
    
      
    // Function to check if the triangle 
    // can be transformed to right-angled 
    static void isValidCombination(int x1, int y1, 
                                   int x2, int y2, 
                                   int x3, int y3) 
    
        int x, y; 
      
        // Boolean variable to 
        // return true or false 
        bool possible = false
      
        // If it is already right-angled 
        if (ifRight(x1, y1, x2, y2, x3, y3)) 
        
            Console.WriteLine("ALREADY RIGHT ANGLED"); 
            return
        
        else
        
      
            // Applying the changes on the 
            // co-ordinates 
            for (int i = 0; i < 4; i++) 
            
      
                // Applying on the first 
                // co-ordinate 
                x = dx[i] + x1; 
                y = dy[i] + y1; 
      
                if (ifRight(x, y, x2, y2, x3, y3)) 
                
                    Console.WriteLine("POSSIBLE"); 
                    return
                
      
                // Applying on the second 
                // co-ordinate 
                x = dx[i] + x2; 
                y = dy[i] + y2; 
      
                if (ifRight(x1, y1, x, y, x3, y3)) 
                
                    Console.WriteLine("POSSIBLE"); 
                    return
                
      
                // Applying on the third 
                // co-ordinate 
                x = dx[i] + x3; 
                y = dy[i] + y3; 
      
                if (ifRight(x1, y1, x2, y2, x, y)) 
                
                    Console.Write("POSSIBLE"); 
                    return
                
            
        
      
        // If can't be transformed 
        if (!possible) 
            Console.WriteLine("NOT POSSIBLE"); 
    
      
    // Driver Code 
    static public void Main () 
    
        int x1 = -49, y1 = 0; 
        int x2 = 0, y2 = 50; 
        int x3 = 0, y3 = -50; 
      
        isValidCombination(x1, y1, x2, y2, x3, y3); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

POSSIBLE


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.



Improved By : Rajput-Ji, AnkitRai01