Contest Name:
CODICTION 2020
PROBLEM LINK:
Author: Srijan Agrawal
Tester: Mradul Rathore, Nehal Jain, Saniya Agrawal
Editorialist: Murtaza Ali
DIFFICULTY:
CAKEWALK.
PREREQUISITES:
Geometry.
PROBLEM:
Find a point on x-axis such that the reflection of (x_{2}, y_{2}) can be seen from the point (x_{1}, y_{1}).
QUICK EXPLANATION:
We have to place a mirror on (x, 0) such that the reflection of (x_{2}, y_{2}) in the mirror on (x, 0) can be seen from the point (x_{1}, y_{1}).
EXPLANATION:
The Law of Reflection states that the angle of incidence is equal to the angle of reflection. Let the ray passing through (x_{2}, y_{2}) and (x, 0) be the incident line and the ray from (x_{1}, y_{1}) and (x, 0) be the reflected line.
Let,
Angle of incidence = Angle of reflection = \theta
Slope of line of incidence = tan(\frac{\pi}{2} - \theta) = cot(\theta)
Slope of line of reflection = tan(\frac{\pi}{2} + \theta) = - cot(\theta)
Slope of line of incidence in two point form = \frac{y_{2} - 0}{x_{2} - x} = \frac{y_{2}}{x_{2} - x}
Slope of line of reflection in two point form = \frac{y_{1} - 0}{x_{1} - x} = \frac{y_{1}}{x_{1} - x}
By above equations, we can get,
\frac{y_{2}}{x_{2} - x} = - \frac{y_{1}}{x_{1} - x}
x = \frac{x_{2}*y_{1} + x_{1}*y_{2}}{y_{1} + y_{2}}
COMPLEXITY:
Time complexity: O(1) per test.
SOLUTIONS:
Setter's Solution
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--){
long long int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
double n=(x2*y1+y2*x1);
double ans=n/(y1+y2);
cout << fixed << setprecision(2)<<ans<<endl;
}
return 0;
}
ALTERNATE SOLUTION:
We can also solve this problem using Similarity of Triangles. If you want detailed explanation for this approach please let us know.
Alternate Solution by Tester
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int x1, y1, x2, y2;
cin>>x1>>y1;
cin>>x2>>y2;
float r = (float) abs(x1 - x2) / (y1 + y2);
if(x1 < x2){
r *= y1;
r += x1;
}
else{
r *= y2;
r += x2;
}
printf("%0.2f\n", r);
}
return 0;
}
Feel free to share your approach. If you have any queries, they are always welcome.