# Hackerrank Solution: Find the Point

Original Problem

Consider two points, $$p=(p_x, p_y)$$ and $$q=(q_x, q_y)$$. We consider the inversion or point reflection, $$r=(r_x, r_y)$$, of point $$p$$ across point $$q$$ to be a $$180^{\circ}$$ rotation of point $$p$$ around $$q$$.

Given $$n$$ sets of points $$p$$ and $$q$$, find $$r$$ for each pair of points and print two space-separated integers denoting the respective values of $$r_x$$ and $$r_y$$ on a new line.

Input Format

The first line contains an integer, $$n$$, denoting the number of sets of points.
Each of the $$n$$ subsequent lines contains four space-separated integers describing the respective values of $$p_x$$, $$p_y$$, $$q_x$$, and $$q_y$$ defining points $$p=(p_x,p_y)$$ and $$q=(q_x,q_y)$$.

Constraints

• $$1\leq n\leq 15$$
• $$-100\leq p_x,p_y,q_x,q_y\leq 100$$

Output Format

For each pair of points $$p$$ and $$q$$, print the corresponding respective values of $$r_x$$ and $$r_y$$ as two space-separated integers on a new line.

Sample Input

2
0 0 1 1
1 1 2 2


Sample Output

2 2
3 3


Explanation

The graphs below depict points $$p$$, $$q$$, and $$r$$ for the $$n=2$$ points given as Sample Input:

1. Thus, we print $$r_x$$ and $$r_y$$ as 2 2 on a new line.
2. Thus, we print $$r_x$$ and $$r_y$$ as 3 3 on a new line.

## Solution

The rotation matrix around $$180^\circ$$ is:

$$R(180^{\circ })={\begin{bmatrix}\cos 180^{\circ } &-\sin 180^{\circ } \\\sin 180^{\circ } &\cos 180^{\circ } \\\end{bmatrix}}={\begin{bmatrix}-1&0\\[3pt]0&-1\\\end{bmatrix}}$$

Now it's quite easy to translate by point $$q$$, rotate with matrix $$R$$ and translate back to $$q$$:

$$r = q + R(180^{\circ }) \cdot (p - q) = 2q - p$$

The final implementation then looks like this in Ruby:

gets.to_i.times{
x = gets.split.map(&:to_i)
print 2 * x - x, ' ', 2 * x - x, "\n"
}


« Back to problem overview

All images are copyright to Hackerrank