Dadas dos coordenadas de puntos (x 1 , y 1 ) y (x 2 , y 2 ) en un plano 2D. La tarea es encontrar el reflejo de (x 1 , y 1 ) en una rotación de 180 grados de (x 2 , y 2 ).
Ejemplos:
Input : x1 = 0, y1 = 0, x2 = 1, y2 = 1 Output : (2, 2)
Input : x1 = 1, y1 = 1, x2 = 2, y2 = 2 Output : (3, 3)
Sea el punto de reflexión del punto (x 1 , y 1 ) sobre (x 2 , y 2 ) (x’, y’).
Para que (x’, y’) sea la rotación de 180 grados del punto (x 1 , y 1 ) alrededor del punto (x 2 , y 2 ), todos deben ser colineales, es decir, los tres puntos deben estar en la misma línea recta. Además, observe que (x 2 , y 2 ) se convertirá en el punto medio entre (x 1 , y 1 ) y (x’, y’).
Entonces,
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
A continuación se muestra la implementación de este Acercarse:
C++
// CPP Program tof find the 180 degree reflection // of one point around another point. #include <bits/stdc++.h> 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
<?php // PHP Program for find the 180 // degree reflection of one point // around another point. function findPoint($x1, $y1, $x2, $y2) { echo "(" , 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 anuj_67 ?>
Javascript
<script> // Javascript Program to find the 180 degree reflection // of one point around another point. function findPoint(x1, y1, x2, y2) { document.write("(" + 2 * (x2 - x1) + ", " + 2 * (y2 - y1) + ")"); } let x1 = 0, y1 = 0, x2 = 1, y2 = 1; findPoint(x1, y1, x2, y2); // This code is contributed by Mayank Tyagi </script>
(2, 2)
Complejidad de tiempo : O(1)
Complejidad espacial : O (1) ya que usa variables constantes