We need to solve: For each query point p, find the reflection of p across line through p1p2? Wait the description: "For given three points p1, p2, p, find the reflection point x of p onto p1p2." And there is an image: probably means reflection of point p across the line defined by p1 and p2. Yes reflection point x is the mirror (symmetric) point of p with respect to line L passing through p1 and p2. So we need to compute coordinate of the reflected point.
Given p1, p2 define line L. For each p, compute reflection p' such that line L is the perpendicular bisector of segment pp'. Equivalent to project p onto L, then reflect.
We can compute using vector formulas. Let A = p1, B = p2. Let vector v = B - A. For point p, let vector ap = p - A. Projection of ap onto v is: t = (ap · v) / (v · v). Then the foot of perpendicular H = A + t * v. Then reflected point x = p + 2*(H - p) = 2H - p.
Thus compute x = 2*H - p.
We need output with high precision (error < 0.00000001). Input coordinates are integers up to 10000. Number of queries up to 1000. So we can compute with double and output with 10 decimal places or more. Example output shows 10 decimals. So we'll use double (or long double) and printf with %.10f.
But careful: Input coordinates can be negative, but within range. Simple.
But wait: The second example: p1 (0,0), p2 (3,4). Query points: (2,5), (1,4), (0,3). Outputs: (4.24, 3.32), (3.56, 2.08), (2.88, 0.84). Let's test with formula: v = (3,4). Norm squared = 25. For p=(2,5): ap=(2,5). t = (23+54)/25 = (6+20)/25 = 26/25 = 1.04. H = (0,0)+1.04*(3,4) = (3.12, 4.16). Then x = 2H - p = (6.24-2, 8.32-5) = (4.24, 3.32). Correct. Next: p=(1,4): ap=(1,4); t=(13+44)/25 = (3+16)/25=19/25=0.76; H=(2.28,3.04); x=(4.56-1, 6.08-4) = (3.56,2.08). Good. Next: p=(0,3): ap=(0,3); t=(0+12)/25=12/25=0.48; H=(1.44,1.92); x=(2.88-0, 3.84-3) = (2.88,0.84). Good.
Thus solution: read p1, p2; compute vx, vy, v2 = vx^2+vy^2 (double). For each query, compute apx, apy, t = (apxvx+apyvy)/v2, Hx = xp1 + tvx, Hy = yp1 + tvy; then X = 2Hx - px, Y = 2Hy - py. Print with 10 decimal places.
Edge Cases: v2 could be zero but p1 and p2 are distinct per constraints, so v2>0. Use double.
Implementation in C++:
#include
#include
using namespace std;
int main() {
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
double vx = x2 - x1, vy = y2 - y1;
double v2 = vxvx + vyvy;
int q;
cin >> q;
while (q--) {
double px, py;
cin >> px >> py;
double apx = px - x1, apy = py - y1;
double t = (apxvx + apyvy) / v2;
double hx = x1 + t * vx;
double hy = y1 + t * vy;
double rx = 2hx - px;
double ry = 2hy - py;
printf("%.10f %.10f\n", rx, ry);
}
return 0;
}
Alternatively use fixed and setprecision.
Make sure to handle large numbers but double is fine.
Note: Input coordinates are integers but using double.
Ok. We should produce final answer with explanation and code.
But the problem description says "reflection point x of p onto p1p2". Could also be interpreted as reflection onto segment? But typical geometric problem is reflection across line. The examples confirm.
Thus solution is straightforward.
Now produce final answer in the required format (maybe just code). However the prompt likely expects me to provide a solution with explanation? Actually the conversation: user is providing the problem statement and expects solution? The system is to respond with code? Typically we output solution code.
Given the context, I'm ChatGPT, but might need to answer with solution. Since it's a coding problem, output code in C++. However the conversation history shows previous examples? I think just output final code.
But instructions: "You are an AI programming assistant. Follow the user's requirements carefully." The user gave a problem statement with constraints, input, output, examples. So user likely wants a solution. So I should provide explanation and code. Usually in these competitive programming help scenarios, I provide code.
Thus answer: Provide algorithm description and code.
I'll output the code in C++ with commentary.
Make sure to include iostream and cstdio. Possibly we need to set precision.
Also note that in some judges, using endl could cause flushing, but printf is fine.
I'll deliver.
But maybe we should handle input using scanf and output using printf for speed. However q <= 1000, not large. Either is fine.
We'll produce.
Now final answer. ```cpp
#include
int main() {
double x1, y1, x2, y2;
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
double vx = x2 - x1, vy = y2 - y1;
double v2 = vx * vx + vy * vy; // squared length, always > 0
int q;
scanf("%d", &q);
while (q--) {
double px, py;
scanf("%lf %lf", &px, &py);
double apx = px - x1, apy = py - y1;
double t = (apx * vx + apy * vy) / v2; // scalar projection
double hx = x1 + t * vx; // foot of perpendicular
double hy = y1 + t * vy;
double rx = 2 * hx - px; // reflected point
double ry = 2 * hy - py;
printf("%.10f %.10f\n", rx, ry);
}
return 0;
}