Point Reflection

Overview

In this article we will try to solve a mathematical problem from Hacker Rank called Find the Point.

Consider two points, p = (px, py) and q = (qx, qy). We consider the inversion or point reflection, r = (rx, ry), of point p across point q to be a 180o rotation of point p around q.

For (rx, ry) to be the 180o degree rotation of point (px, py) around (qx, qy), all the three point should lie on the same straight line. Point (qx, qy) should also be the middle point of (px, py) and (qx, qy).

So `qx = (px + rx) / 2` and `qy = (py + ry) / 2`

Code

Below is the Java function which find the point reflection of the given two points:

```package com.zia.mathematics.fundamentals;

import java.util.Scanner;

public class FindThePoint {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter x coordinates of the point P");
int px = scanner.nextInt();
System.out.println("Enter y coordinates of the point P");
int py = scanner.nextInt();
System.out.println("Enter x coordinates of the point Q");
int qx = scanner.nextInt();
System.out.println("Enter y coordinates of the point Q");
int qy = scanner.nextInt();
final int[] reflectionPoint = findReflectionPoint(px, py, qx, qy);
System.out.println(String.format("R (%s, %s)", reflectionPoint[0], reflectionPoint[1]));
}

static int[] findReflectionPoint(int px, int py, int qx, int qy) {
int x = 2*qx - px;
int y = 2*qy - py;
return new int[]{x,y};
}
}
```

Output

Below is the sample output:

```Enter x coordinates of the point P
1
Enter y coordinates of the point P
1
Enter x coordinates of the point Q
2
Enter y coordinates of the point Q
2
Reflection point R = (3, 3)
```