Find the coordinates of the fourth vertex of a square with given 3 vertices using XOR
Last Updated : 7 Dec, 2023
Given the coordinates of three points in a plane, find the coordinates of the fourth point that forms a square, suppose take the first vertex as (x1, y1), the second vertex as (x2, y2), the third vertex as (x3, y3) and find the fourth vertex (x4, y4).
Examples:
Input: p1 = { 20, 10 }, p2 = { 10, 20 }, p3 = { 20, 20 } Output: the fourth point is: {10, 10} Explanation:
Example-1
Input: p1 = {25, 24}, p2 = {10, 16}, p3 = {10, 24} Output: the fourth point is: {25, 16}
Approach: To solve the problem using XOR follow the below steps:
XOR of the same number gives a result of zero.
In the same case. A square has four coordinates if we XOR all coordinates we get a result of zero.
we have given three coordinates if we XOR these three coordinates we will get the fourth coordinates.
XOR all x coordinates we get the x coordinate of a fourth point.
XOR all y coordinates we get the y coordinate of a fourth point.
Below is the implementation for the above aprpoach:
C++
// C++ program to find fourth co-ordinates of the point#include<bits/stdc++.h>usingnamespacestd;structPoint{intx,y;};intmain(){Pointp1={20,10},p2={10,20},p3={20,20};// XOR all x coordinates to get fourth x coordinateintX=(p1.x^p2.x^p3.x);// XOR all y coordinates to get fourth x coordinateintY=(p1.y^p2.y^p3.y);cout<<"fouth point is : "<<"{ "<<X<<", "<<Y<<" }"<<endl;return0;}
Java
/*package whatever //do not write package name here */// Java program to find the fourth co-ordinates pointimportjava.io.*;publicclassGFG{staticclassPoint{intx,y;publicPoint(intx,inty){this.x=x;this.y=y;}};publicstaticvoidmain(String[]args){Pointp1=newPoint(20,10),p2=newPoint(10,20),p3=newPoint(20,20);// XOR all x coordinates to get fourth x coordinateintX=(p1.x^p2.x^p3.x);// XOR all y coordinates to get fourth x coordinateintY=(p1.y^p2.y^p3.y);System.out.println("fourth point is : { "+X+", "+Y+" }");}}// This is contributed by spbabaraheem
Python3
classPoint:def__init__(self,x,y):self.x=xself.y=y# Given three pointsp1=Point(20,10)p2=Point(10,20)p3=Point(20,20)# XOR all x coordinates to get the fourth x coordinateX=p1.x^p2.x^p3.x# XOR all y coordinates to get the fourth y coordinateY=p1.y^p2.y^p3.yprint(f"Fourth point is: {{{X}, {Y}}}")
C#
usingSystem;structPoint{publicintx,y;}classProgram{staticvoidMain(){Pointp1=newPoint{x=20,y=10};Pointp2=newPoint{x=10,y=20};Pointp3=newPoint{x=20,y=20};// XOR all x coordinates to get the fourth x coordinateintX=(p1.x^p2.x^p3.x);// XOR all y coordinates to get the fourth y coordinateintY=(p1.y^p2.y^p3.y);Console.WriteLine("Fourth point is: { "+X+", "+Y+" }");}}
JavaScript
// JavaScript code for the above approach:// Define a structure to represent a PointclassPoint{constructor(x,y){this.x=x;this.y=y;}}functionfindFourthPoint(p1,p2,p3){// XOR all x coordinates to get the fourth x coordinateconstX=p1.x^p2.x^p3.x;// XOR all y coordinates to get the fourth y coordinateconstY=p1.y^p2.y^p3.y;console.log("fourth point is : { "+X+", "+Y+" }");}// Driver codeconstp1=newPoint(20,10);constp2=newPoint(10,20);constp3=newPoint(20,20);// Call the function to find the fourth pointfindFourthPoint(p1,p2,p3);