给定二维平面上的两点坐标(x 1 , y 1 )和(x 2 , y 2 ) 。任务是找到 (x 1 , y 1 ) 在 (x 2 , y 2 ) 旋转 180 度时的反射。
例子:
Input : x1 = 0, y1 = 0, x2 = 1, y2 = 1
Output : (2, 2)
Input : x1 = 1, y1 = 1, x2 = 2, y2 = 2
Output : (3, 3)
令(x 1 , y 1 )点关于(x 2 , y 2 )的反射点为(x’, y’)。
对于 (x’, y’) 是点 (x 1 , y 1 ) 围绕点 (x 2 , y 2 ) 的 180 度旋转,它们都必须共线,即所有三个点必须位于同一条直线上。此外,观察 (x 2 , y 2 ) 将成为 (x 1 , y 1 ) 和 (x’, y’) 之间的中点。
所以,
x’ – x 2 = x 2 – x 1
y’ – y 2 = y 2 – y 1
x’ = 2 * x 2 – x 1
y’ = 2 * y 2 – y 1
下面是这个方法的实现:
C++
// CPP Program tof find the 180 degree reflection
// of one point around another point.
#include
using namespace std;
void findPoint(int x1, int y1, int x2, int y2)
{
cout << "(" << 2 * x2 - x1 << ", "
<< 2 * y2 - y1 << ")";
}
int main()
{
int x1 = 0, y1 = 0, x2 = 1, y2 = 1;
findPoint(x1, y1, x2, y2);
return 0;
}
Java
// Java Program to find the 180 degree
// reflection of one point around
// another point.
class GFG {
static void findPoint(int x1, int y1,
int x2, int y2)
{
System.out.println("(" + (int)(2 * x2 - x1)
+ "," + (int)(2 * y2 - y1 ) + " )");
}
// Driver code
public static void main(String args[])
{
int x1 = 0, y1 = 0, x2 = 1, y2 = 1;
findPoint(x1, y1, x2, y2);
}
}
// This code is contributed by Arnab Kundu.
Python3
# Python3 Program for find the 180
# degree reflection of one point
# around another point.
def findPoint(x1, y1, x2, y2):
print("(" , 2 * x2 - x1 , ",",
2 * y2 - y1 ,")");
# Driver Code
x1 = 0;
y1 = 0;
x2 = 1;
y2 = 1;
findPoint(x1, y1, x2, y2);
# This code is contributed by mits
C#
// C# Program to find the 180 degree reflection
// of one point around another point.
using System;
public class GFG {
static void findPoint(int x1, int y1,
int x2, int y2)
{
Console.WriteLine("(" + (int)(2 * x2 - x1)
+ "," + (int)(2 * y2 - y1 ) + " )");
}
// Driver code
static public void Main(String []args)
{
int x1 = 0, y1 = 0, x2 = 1, y2 = 1;
findPoint(x1, y1, x2, y2);
}
}
// This code is contributed by Arnab Kundu.
PHP
Javascript
输出:
(2, 2)
时间复杂度: O(1)