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

📅  最后修改于: 2021-05-04 21:16:37             🧑  作者: 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


输出:

4, -4
6, 4
-2, 1

时间复杂度: O(1)