📌  相关文章
📜  从两个给定的顶点中找到一个正方形的剩余顶点

📅  最后修改于: 2021-04-29 16:12:15             🧑  作者: Mango

给定正方形(X 1 ,Y 1 )(X 2 ,Y 2 )的任何两个顶点的坐标,任务是找到其他两个顶点的坐标。如果不能使用这两个顶点形成正方形,请打印-1

例子:

方法:该方法基于以下事实:正方形的所有边的长度均相等。如果无法获得所有边的长度都相等的顶点,则打印“ -1” 。请按照以下步骤解决问题:

  • 给定的两个顶点可以是正方形侧面的顶点,也可以是对角线的顶点。
  • 如果给定两个顶点的x坐标相等,则其他两个顶点的坐标为:
  • 如果给定两个顶点的y坐标相等,则其他两个顶点的坐标为:
  • 否则,给定的坐标是正方形的对角线。因此,其他两个顶点的坐标为:

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
#include 
using namespace std;
 
// Function to find the remaining
// vertices of a square
void findVertices(int x1, int y1,
                  int x2, int y2)
{
    // Check if the x-coordinates
    // are equal
    if (x1 == x2) {
        cout << (x1 + y2 - y1)
             << ", " << y1 << endl;
 
        cout << (x2 + y2 - y1)
             << ", " << y2;
    }
 
    // Check if the y-coordinates
    // are equal
    else if (y1 == y2) {
        cout << x1 << ", "
             << (y1 + x2 - x1)
             << endl;
 
        cout << x2 << ", "
             << (y2 + x2 - x1);
    }
 
    // If the the given coordinates
    // forms a diagonal of the square
    else if (abs(x2 - x1)
             == abs(y2 - y1)) {
 
        cout << x1 << ", " << y2
             << endl;
        cout << x2 << ", " << y1;
    }
 
    // Otherwise
    else
 
        // Square does not exist
        cout << "-1";
}
 
// Driver Code
int main()
{
    // Given two vertices
    int x1 = 1, y1 = 2;
    int x2 = 3, y2 = 4;
    findVertices(x1, y1, x2, y2);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the remaining
// vertices of a square
static void findVertices(int x1, int y1,
                         int x2, int y2)
{
     
    // Check if the x-coordinates
    // are equal
    if (x1 == x2)
    {
        System.out.print((x1 + y2 - y1) +
                        ", " + y1 + "\n");
 
        System.out.print((x2 + y2 - y1) +
                        ", " + y2);
    }
 
    // Check if the y-coordinates
    // are equal
    else if (y1 == y2)
    {
        System.out.print(x1 + ", " +
                        (y1 + x2 - x1) + "\n");
 
        System.out.print(x2 + ", " +
                        (y2 + x2 - x1));
    }
 
    // If the the given coordinates
    // forms a diagonal of the square
    else if (Math.abs(x2 - x1) ==
             Math.abs(y2 - y1))
    {
        System.out.print(x1 + ", " + y2 + "\n");
        System.out.print(x2 + ", " + y1);
    }
 
    // Otherwise
    else
 
        // Square does not exist
        System.out.print("-1");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given two vertices
    int x1 = 1, y1 = 2;
    int x2 = 3, y2 = 4;
     
    findVertices(x1, y1, x2, y2);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to find the remaining
# vertices of a square
def findVertices(x1, y1, x2, y2):
     
    # Check if the x-coordinates
    # are equal
    if (x1 == x2):
        print((x1 + y2 - y1), ",", y1)
 
        print((x2 + y2 - y1), ",", y2)
 
    # Check if the y-coordinates
    # are equal
    elif (y1 == y2):
        print(x1, ",", (y1 + x2 - x1))
 
        print(x2, ",", (y2 + x2 - x1))
 
    # If the the given coordinates
    # forms a diagonal of the square
    elif (abs(x2 - x1) == abs(y2 - y1)):
        print(x1, ",", y2)
        print(x2, ",", y1)
 
    # Otherwise
    else:
 
        # Square does not exist
        print("-1")
 
# Driver Code
if __name__ == '__main__':
     
    # Given two vertices
    x1 = 1
    y1 = 2
    x2 = 3
    y2 = 4
     
    findVertices(x1, y1, x2, y2)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the remaining
// vertices of a square
static void findVertices(int x1, int y1,
                         int x2, int y2)
{   
  // Check if the x-coordinates
  // are equal
  if (x1 == x2)
  {
    Console.Write((x1 + y2 - y1) +
                  ", " + y1 + "\n");
 
    Console.Write((x2 + y2 - y1) +
                  ", " + y2);
  }
 
  // Check if the y-coordinates
  // are equal
  else if (y1 == y2)
  {
    Console.Write(x1 + ", " +
                  (y1 + x2 - x1) + "\n");
 
    Console.Write(x2 + ", " +
                  (y2 + x2 - x1));
  }
 
  // If the the given coordinates
  // forms a diagonal of the square
  else if (Math.Abs(x2 - x1) ==
           Math.Abs(y2 - y1))
  {
    Console.Write(x1 + ", " + y2 + "\n");
    Console.Write(x2 + ", " + y1);
  }
 
  // Otherwise
  else
 
    // Square does not exist
    Console.Write("-1");
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given two vertices
  int x1 = 1, y1 = 2;
  int x2 = 3, y2 = 4;
  findVertices(x1, y1, x2, y2);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出
1, 4
3, 2

时间复杂度: O(1)
辅助空间: O(1)