给定三个坐标点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
输出 :
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-parallel-ogram
下面是上述方法的实现:
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
输出:
6, 4