📜  找出平行四边形的所有可能坐标

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

从给定的三个坐标中找到所有可能的坐标,以制作非零区域的平行四边形。
我们称 A、B、C 是三个给定的点。我们只能有三种可能的情况:

(1) AB and AC are sides, and BC a diagonal
(2) AB and BC are sides, and AC a diagonal 
(3) BC and AC are sides, and AB a diagonal 

因此,如果给定三个坐标,我们可以说只有 3 个坐标是可能的,我们可以从中生成平行四边形。
为了证明所有三点都不同,让我们假设它是错误的。不失一般性,假设在 AD 和 BC 的情况下得到的分数相等。

考虑这些点相等的两个方程组:

Bx + Cx - Ax = Ax + Cx - Bx
By + Cy - Ay = Ay + Cy - By

It can be simplified as-

Ax = Bx
Ay = By

我们得到了一个矛盾,因为所有的点 A、B、C 都是不同的。

例子:

Input  : A = (0 0)
         B = (1 0)
         C = (0 1)
Output :  1 -1
         -1 1
          1 1

Input  : A = (-1 -1)
         B = (0 1)
         C = (1 1)
Output : -2 -1
          0 -1
          2 3

由于对边相等,AD = BC 和 AB = CD,我们可以计算缺失点 (D) 的坐标为:

AD = BC
(Dx - Ax, Dy - Ay) = (Cx - Bx, Cy - By)
Dx = Ax + Cx - Bx 
Dy = Ay + Cy - By

对角线为 AD 和 BC、CD 和 AB 的情况以相同方式处理。
参考: https : //math.stackexchange.com/questions/1322535/how-many-different-parallelograms-can-be-drawn-if-given-three-co-ordinates-in-3d

下面是上述方法的实现:

C++
// C++ program to all possible points
// of a parallelogram
#include 
using namespace std;
  
// main method
int main()
{
   int ax = 5, ay = 0; //coordinates of A
   int bx = 1, by = 1; //coordinates of B
   int cx = 2, cy = 5; //coordinates of C
    cout << ax + bx - cx << ", "
         << ay + by - cy <


Java
// Java program to all possible
// points of a parallelogram
public class ParallelogramPoints{
     
    // Driver code
    public static void main(String[] s)
    {
        int ax = 5, ay = 0; //coordinates of A
        int bx = 1, by = 1; //coordinates of B
        int cx = 2, cy = 5; //coordinates of C
        System.out.println(ax + bx - cx + ", "
                           + (ay + by - cy));
        System.out.println(ax + cx - bx + ", "
                           + (ay + cy - by));
        System.out.println(cx + bx - ax + ", "
                           + (cy + by - ax));
    }
}
 
// This code is contributed by Prerna Saini


Python3
# Python3 program to find all possible points
# of a parallelogram
 
ax = 5
ay = 0 #coordinates of A
bx = 1
by = 1 #coordinates of B
cx = 2
cy = 5 #coordinates of C
print(ax + bx - cx, ", ", ay + by - cy)
print(ax + cx - bx, ", ", ay + cy - by)
print(cx + bx - ax, ", ", cy + by - ax)


C#
// C# program to all possible
// points of a parallelogram
using System;
 
public class ParallelogramPoints
{
     
    // Driver code
    public static void Main()
    {
         
        //coordinates of A
        int ax = 5, ay = 0;
         
        //coordinates of B
        int bx = 1, by = 1;
         
        //coordinates of C
        int cx = 2, cy = 5;
         
        Console.WriteLine(ax + bx - cx + ", "
                        + (ay + by - cy));
        Console.WriteLine(ax + cx - bx + ", "
                        + (ay + cy - by));
        Console.WriteLine(cx + bx - ax + ", "
                        + (cy + by - ax));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

4, -4
6, 4
-2, 1

时间复杂度: O(1)

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