📜  寻找平行四边形的缺失点

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

给定三个坐标点 A、B 和 C,找到缺失的点 D,使得 ABCD 可以是平行四边形。
例子 :

Input : A = (1, 0)
        B = (1, 1)
        C = (0, 1)
Output : 0, 0
Explanation:
The three input points form a unit
square with the point (0, 0)

Input : A = (5, 0)
        B = (1, 1)
        C = (2, 5)
Output : 6, 4

如下图所示,可以有多种可能的输出,我们需要打印其中的任何一种。

平行四边形

如果四边形的对边平行且长度相等,则称四边形是平行四边形。

由于给定了平行四边形的三个点,我们可以找到缺失边的斜率及其长度。
该算法可以解释如下
让 R 成为缺失点。现在从定义,我们有

  • PR的长度=QS的长度=L1(对边相等)
  • PR 的斜率 = QS 的斜率 = M1(对边平行)
  • PQ的长度=RS的长度=L2(对边相等)
  • PQ的斜率= RS的斜率= M2(对边平行)

因此,我们可以找到距 P 距离为 L1 且具有斜率 M1 的点,如下文所述:
在给定斜率的线上找到给定距离处的点。
现在其中一个点将满足上述条件,可以轻松检查(使用条件 3 或 4)

下面是上述方法的实现:

C++
// C++ program to find missing point of a
// parallelogram
#include 
using namespace std;
  
// struct to represent a co-ordinate point
struct Point {
    float x, y;
    Point()
    {
        x = y = 0;
    }
    Point(float a, float b)
    {
        x = a, y = b;
    }
};
  
// given a source point, slope(m) of line
// passing through it this function calculates
// and return two points at a distance l away
// from the source
pair findPoints(Point source,
                              float m, float l)
{
    Point a, b;
  
    // slope is 0
    if (m == 0) {
        a.x = source.x + l;
        a.y = source.y;
  
        b.x = source.x - l;
        b.y = source.y;
    }
  
    // slope if infinity
    else if (m == std::numeric_limits::max()) {
        a.x = source.x;
        a.y = source.y + l;
  
        b.x = source.x;
        b.y = source.y - l;
    }
  
    // normal case
    else {
        float dx = (l / sqrt(1 + (m * m)));
        float dy = m * dx;
        a.x = source.x + dx, a.y = source.y + dy;
        b.x = source.x - dx, b.y = source.y - dy;
    }
  
    return pair(a, b);
}
  
// given two points, this function calculates
// the slope of the line/ passing through the
// points
float findSlope(Point p, Point q)
{
    if (p.y == q.y)
        return 0;
    if (p.x == q.x)
        return std::numeric_limits::max();
    return (q.y - p.y) / (q.x - p.x);
}
  
// calculates the distance between two points
float findDistance(Point p, Point q)
{
    return sqrt(pow((q.x - p.x), 2) + pow((q.y - p.y), 2));
}
  
// given three points, it prints a point such
// that a parallelogram is formed
void findMissingPoint(Point a, Point b, Point c)
{
    // calculate points originating from a
    pair d = findPoints(a, findSlope(b, c),
                                      findDistance(b, c));
  
    // now check which of the two points satisfy
    // the conditions
    if (findDistance(d.first, c) == findDistance(a, b))
        cout << d.first.x << ", " << d.first.y << endl;
    else
        cout << d.second.x << ", " << d.second.y << endl;
}
  
// Driver code
int main()
{
    findMissingPoint(Point(1, 0), Point(1, 1), Point(0, 1));
    findMissingPoint(Point(5, 0), Point(1, 1), Point(2, 5));
    return 0;
}


C++
// C++ program to find missing point
// 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 + cx - bx << ", "
         << ay + cy - by;
    return 0;
}


Java
// Java program to 
// find missing point
// of a parallelogram
import java.io.*;
  
class GFG 
{
public static void main (String[] args) 
{
  
    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 + (cx - bx) + ", " +
                       ay + (cy - by));
}
}
  
// This code is contributed by m_kit


Python 3
# Python 3 program to find missing point
# of a parallelogram
  
# Main method
if __name__ == "__main__":
      
    # coordinates of A
      
    ax, ay = 5, 0
      
    # coordinates of B
      
    bx ,by = 1, 1
      
    # coordinates of C
      
    cx ,cy = 2, 5
      
    print(ax + cx - bx , ",", ay + cy - by)
  
# This code is contributed by Smitha


C#
// C# program to 
// find missing point
// of a parallelogram
using System;
  
class GFG
{
    static public void 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
        Console.WriteLine(ax + (cx - bx) + ", " +
                             ay + (cy - by));
    }
}
  
// This code is contributed by ajit


PHP


Javascript


输出 :

0, 0
6, 4

替代方法:

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

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

参考资料: https : //math.stackexchange.com/questions/887095/find-the-4th-vertex-of-the-parallelogram
下面是上述方法的实现:

C++

// C++ program to find missing point
// 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 + cx - bx << ", "
         << ay + cy - by;
    return 0;
}

Java

// Java program to 
// find missing point
// of a parallelogram
import java.io.*;
  
class GFG 
{
public static void main (String[] args) 
{
  
    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 + (cx - bx) + ", " +
                       ay + (cy - by));
}
}
  
// This code is contributed by m_kit

Python3

# Python 3 program to find missing point
# of a parallelogram
  
# Main method
if __name__ == "__main__":
      
    # coordinates of A
      
    ax, ay = 5, 0
      
    # coordinates of B
      
    bx ,by = 1, 1
      
    # coordinates of C
      
    cx ,cy = 2, 5
      
    print(ax + cx - bx , ",", ay + cy - by)
  
# This code is contributed by Smitha

C#

// C# program to 
// find missing point
// of a parallelogram
using System;
  
class GFG
{
    static public void 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
        Console.WriteLine(ax + (cx - bx) + ", " +
                             ay + (cy - by));
    }
}
  
// This code is contributed by ajit

PHP


Javascript


输出:

6, 4

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