📌  相关文章
📜  检查是否可以通过移动任一坐标形成直角三角形

📅  最后修改于: 2021-10-23 08:48:32             🧑  作者: Mango

给定三角形(x1, y1)(x2, y2)(x3, y3) 的三个坐标。任务是找出是否三角形可以通过距离1恰好仅移动一个点被转换成直角三角形
如果可以使三角形成为直角,则打印“POSSIBLE” ,否则打印“NOT POSSIBLE”
如果三角形已经是直角的,也应该报告。
例子:

方法:
众所周知,对于边为abc的三角形,如果以下等式成立,则该三角形将是直角的: a 2 + b 2 = c 2
因此,对于三角形的每一个坐标,找出所有的边,并为他们的3个可能的排列检查它是否已经直角三角形和报告。
如果上述条件不成立,则需要进行以下操作——
我们需要将所有坐标一一更改,并检查它是否是直角三角形的有效组合。
看看有 4 种可能的组合可以将每个坐标更改 1。它们是(-1, 0), (0, 1), (1, 0), (0, -1) 。因此,运行一个循环并对每个坐标逐一应用这些更改,并检查公式a 2 + b 2 = c 2是否正确。
如果这是真的,那么它可以将三角形转变为直角三角形,否则不是。
下面是上面代码的实现:

C++
// C++ implementation of
// the above approach
#include 
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;
}


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


Python3
# Python3 implementation of the above approach
 
# Storing all the possible
# changes to make the triangle
# right-angled
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
 
# Function to check if the triangle
# is right-angled or not
def ifRight(x1, y1, x2, y2, x3, y3):
 
    a = ((x1 - x2) * (x1 - x2)) + \
        ((y1 - y2) * (y1 - y2))
 
    b = ((x1 - x3) * (x1 - x3)) + \
        ((y1 - y3) * (y1 - y3))
 
    c = ((x2 - x3) * (x2 - x3)) + \
        ((y2 - y3) * (y2 - y3))
 
    if ((a == (b + c) and a != 0 and b != 0 and c != 0) or
        (b == (a + c) and a != 0 and b != 0 and c != 0) or
        (c == (a + b) and a != 0 and b != 0 and c != 0)):
        return 1
 
# Function to check if the triangle
# can be transformed to right-angled
def isValidCombination(x1, y1, x2, y2, x3, y3):
 
    x, y = 0, 0
 
    # Boolean variable to
    # return true or false
    possible = 0
 
    # If it is already right-angled
    if (ifRight(x1, y1, x2, y2, x3, y3)):
        print("ALREADY RIGHT ANGLED")
        return
 
    else:
 
        # Applying the changes on the
        # co-ordinates
        for i in range(4):
 
            # Applying on the first
            # co-ordinate
            x = dx[i] + x1
            y = dy[i] + y1
 
            if (ifRight(x, y, x2, y2, x3, y3)):
                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)):
                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)):
                print("POSSIBLE")
                return
 
    # If can't be transformed
    if (possible == 0):
        print("NOT POSSIBLE")
 
# Driver Code
x1 = -49
y1 = 0
x2 = 0
y2 = 50
x3 = 0
y3 = -50
 
isValidCombination(x1, y1, x2, y2, x3, y3)
 
# This code is contributed by Mohit Kumar


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


Javascript


输出:
POSSIBLE

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程