We are given three points that are the midpoints of the sides of some triangle. We need to find coordinates of the triangle vertices.
This is a classic geometry problem: Given midpoints of sides of a triangle, the original triangle's vertices are found by solving: Let the vertices be A, B, C. The midpoints are D = midpoint of BC, E = midpoint of CA, F = midpoint of AB. Typically, D, E, F are given. Then we can compute A = E + F - D, B = F + D - E, C = D + E - F. That is because the centroid is common: The vector from midpoint of side to opposite vertex is twice etc.
But careful: The assignment of which midpoint corresponds to which side matters. However, the three given points are the midpoints of the sides, but we are not told which is which. But any ordering of the three points as D, E, F will produce a triangle that is the original triangle, but with possibly permuted vertex labels. However, we need to output the vertices (just three points). It doesn't matter which vertex is labeled which, as long as we output the three vertices of the triangle. The problem statement says: "Find coordinates of the triangle vertices." So we just need to compute the triangle.
Given any three non-collinear points that are midpoints of a triangle's sides, they uniquely determine the triangle up to translation? Actually, if we know the three midpoints, they uniquely define the triangle (the original triangle is determined, but there might be a sign? Let's think: If we have three points that are midpoints of sides of a triangle, then they themselves form the medial triangle, which is similar to the original and half size. The original triangle's vertices are the points such that each vertex is the sum of two midpoints minus the third. But the assignment matters: There are six possible ways to assign the three given points to D, E, F. However, the resulting set of three vertices will be the same set for all assignments? Let's test: Suppose we have points M1, M2, M3. If we set D=M1, E=M2, F=M3, then A = E+F-D = M2+M3-M1; B = F+D-E = M3+M1-M2; C = D+E-F = M1+M2-M3. If we permute the assignment, say D=M2, E=M1, F=M3, then A = M1+M3-M2, B = M3+M2-M1, C = M2+M1-M3. These sets are the same as previous but with vertices permuted. So indeed, the set of three vertices is invariant under permutation of the three midpoints. So we can compute A, B, C as above with any ordering. However, we must ensure that the points we compute are indeed the vertices of a triangle (non-collinear). The problem guarantees that the given points are midpoints of some triangle, so they are not collinear (since the medial triangle is a triangle unless original triangle degenerate). So it's fine.
Thus algorithm: Read three points m1, m2, m3 as doubles. Compute:
A = m2 + m3 - m1
B = m3 + m1 - m2
C = m1 + m2 - m3
These are vectors (x, y). These three points are the vertices. Then sort them according to criteria: first by x ascending, if x equal then by y ascending. Output each with exactly 4 digits after decimal point (fixed format with 4 decimal places). Input may have variable decimal digits but we read as double.
Constraints: coordinates between 0 and 1000. So numbers are not huge. Use double precision.
Check sample:
m1 = (5.00,3.00), m2 = (4.0,4.0), m3 = (5.0,5.000). Compute:
A = m2+m3-m1 = (4+5-5, 4+5-3) = (4, 6)
B = m3+m1-m2 = (5+5-4, 5+3-4) = (6, 4)
C = m1+m2-m3 = (5+4-5, 3+4-5) = (4, 2)
The three points: (4,2), (4,6), (6,4). Sort by x ascending: (4,2), (4,6) both x=4, then sort by y ascending: (4,2), (4,6), then (6,4). Output each with 4 decimals: 4.0000 2.0000, 4.0000 6.0000, 6.0000 4.0000. That matches sample.
Thus solution is straightforward.
But wait: There is nuance: The three midpoints are given but could be in any order, and we compute vertices via the formula. However, is it always that the three computed points are the vertices? Yes, because the medial triangle is half scale and rotated, but these formulas work. There is also possibility that the three given points are not the midpoints of a non-degenerate triangle? The problem says they are midpoints of some triangle, so it's valid.
Implementation: Use vector of pair<double,double> points. Read 3 points. Compute three vertex coordinates. Sort. Output with fixed << setprecision(4) for each coordinate.
Edge cases: Might be floating point inaccuracies; but using double should be fine. The sorting might treat nearly equal numbers due to rounding; but coordinates are exact rational numbers (sum/differences of numbers with at most 3 decimal places maybe). Input coordinates can be reals, but we can compute exactly if they have finite decimal representation, but double rounding is fine. We need to output exactly 4 digits after decimal. We'll use printf("%.4f") or cout with fixed and setprecision(4). Ensure rounding is correct.
We must also consider if the computed vertices might be negative? Constraints say input coordinates are between 0 and 1000, but computed vertices could be negative? For example, if midpoints are close to edges, but original triangle may extend beyond 0..1000? Possibly, but problem doesn't restrict output range, only input. So okay.
Double-check: With m1, m2, m3 as given, the formulas yield vertices that might not be within [0,1000] but that's fine.
Thus solution.
But wait: The problem statement says: "You are given 3 points - middles of the sides of some triangle. Find coordinates of the triangle vertices." Possibly there is also the case that the given points might be collinear? But it says "some triangle", so they are non-collinear.
Thus answer is simple.
But let's think deeper: Actually, the three midpoints of a triangle always form a triangle (the medial triangle) which is similar to the original and half the size. So given the medial triangle, the original triangle is uniquely determined (up to a 180-degree rotation? Actually, it's unique: if you reflect one vertex across the opposite midpoint, you get the other two? The formula we used yields the unique triangle whose medial triangle has those midpoints. There is no other triangle with same set of midpoints because the mapping is invertible. So it's fine.
Thus code is easy.
But some online judges might have input with more than 3 lines? No, exactly 3 lines.
We'll implement in C++.
Algorithm steps:
- Read three pairs (x1,y1), (x2,y2), (x3,y3) as double.
- Compute:
xA = x2 + x3 - x1; yA = y2 + y3 - y1;
xB = x3 + x1 - x2; yB = y3 + y1 - y2;
xC = x1 + x2 - x3; yC = y1 + y2 - y3;
- Store these three points in an array.
- Sort using comparator: first compare x with tolerance? But we can just compare doubles directly; due to rounding they should be exact if input numbers are multiples of 0.0001? Not necessarily; input may have up to 3 decimals? Sample shows varied. But computed values may be like 4.0 exactly, but floating point errors might cause 3.9999999. To avoid issues in sorting, we could add a small epsilon when comparing? But if we sort raw doubles, two numbers intended to be equal might not compare equal; but that's unlikely to cause wrong ordering if we treat them as is? If two x coordinates are mathematically equal, computed as double they might be slightly different due to floating errors, but that could affect sorting: one might be slightly less, leading to ordering that might not match the intended order (if they are truly equal, any order would be fine but must also sort y then). However, the required sorting is based on the true values, not floating errors. To ensure correct output, we might want to round the computed coordinates to a certain precision before sorting, or use an epsilon. But the problem expects output exactly 4 decimal places. So we could compute the coordinates as doubles, then for sorting, we could sort by the rounded values to 4 decimals? However, rounding to 4 decimals might produce ties that should be broken by y. But if two coordinates differ by less than 0.00005, rounding to 4 decimals might make them equal but their true values are different; the problem expects sorting based on the actual coordinates, not rounded. But the actual coordinates are determined by the midpoints; they might be exactly rational numbers with denominator a power of 2? Actually, given midpoints with decimal values, the vertices will be sums/differences, so they might be exactly representable in binary? Not necessarily. But the problem probably uses input values that are multiples of 0.0001? Not guaranteed. But typical contest problems often have exact rational numbers, and double representation may be close. However, to be safe, we can sort using a small epsilon, e.g., 1e-9, considering numbers within epsilon as equal. Or we could compute using exact arithmetic: Since input numbers are given with at most some decimals, we could parse them as fractions (by reading string). But that might be overkill.
Simplify: Since the formula is linear with coefficients 1,1,-1, if we read numbers as decimal strings, we could compute exactly as rational numbers with denominator power of 10. But we can also compute with double and then for sorting, use a custom comparator with epsilon. Then output with fixed 4 decimals, which will round to nearest 0.0001. The sorting order after rounding might differ from the order of the true values if two values are extremely close? The problem constraints guarantee that input numbers are between 0 and 1000, with presumably not too many decimals. The computed vertices should have distinct x coordinates in most cases; but there could be ties. In ties, we need to sort by y. So we must correctly identify ties. Using epsilon of 1e-9 is safe: if two x differ by <1e-9, treat as equal. That should be fine.
But we must also ensure that we output sorted vertices with exactly 4 decimals. The output rounding may cause two numbers that are mathematically different but after rounding become same? That would be a problem only if sorting requires them to be distinct, but they'd be output with same rounded coordinates? That would violate uniqueness maybe. But problem likely ensures uniqueness.
Thus implementation:
- Use vector<tuple<double,double>> or struct.
- Compute vertices.
- Sort with comparator:
if (fabs(p1.x - p2.x) > EPS) return p1.x < p2.x;
else return p1.y < p2.y;
where EPS = 1e-9.
- Then for each vertex, print with fixed << setprecision(4) << x << " " << y.
Double-check EPS: Some coordinates may be like 0.00005 difference? That is >1e-9, so fine.
Alternatively, we could compute vertices using exact arithmetic: multiply by 2? Let's derive a more straightforward method: Let vertices A, B, C. Then midpoints: (B+C)/2, (C+A)/2, (A+B)/2. Given three midpoints, we can solve: Adding all three: (B+C + C+A + A+B)/2 = (2A+2B+2C)/2 = A+B+C. So sum of vertices = sum of midpoints * 2? Actually, sum of midpoints = ( (B+C)/2 + (C+A)/2 + (A+B)/2 ) = (2A+2B+2C)/2 = A+B+C. So sum of vertices equals sum of midpoints. Not directly helpful.
From there we can get: A = (midpoint of CA + midpoint of AB - midpoint of BC). That's our formula. So it's linear.
So double fine.
Potential pitfalls: Input may contain whitespace lines. We'll just use cin.
Now, check for degenerate case: If three midpoints are collinear? Then original triangle is degenerate (line) but problem says triangle, so ignore.
Thus we can code.
Time complexity trivial.
I'll write a C++ solution.
But wait: The problem says "Vertices should be sorted in first coordinate ascending order. If two points have the same first coordinate than they should have second coordinate ascending order." So we sort.
One nuance: The computed vertices might have floating point errors that cause sorting to be inconsistent with the true order. But we can mitigate by rounding to 1e-9.
But maybe we can avoid epsilon by computing vertices as (2 * (midpoint combinations) - ...) Actually, the formula can also be: A = 2 * (midpoint of CA + midpoint of AB - midpoint of BC)?? Wait, our formula is A = E+F-D. Since E = (C+A)/2, F = (A+B)/2, D = (B+C)/2, so E+F-D = (C+A + A+B - B - C)/2? Let's compute: E+F-D = (C+A)/2 + (A+B)/2 - (B+C)/2 = (C+A+A+B - B - C)/2 = (2A)/2 = A. So correct. So it's exact.
Thus A, B, C computed from exact rational numbers (midpoints). If midpoints are given with finite decimal representation, then A, B, C will be rational numbers with denominator dividing 2? Actually, consider midpoints have decimals; they might be multiples of 0.5? Not necessarily; if midpoints are arbitrary, the vertices might have decimals to more places, but still rational with denominator 2 or something? Let's examine: Suppose midpoints coordinates are arbitrary real numbers; the formula shows A = m2 + m3 - m1. So the vertices are just linear combinations. So if midpoints are exact in input (as doubles), the vertices are computed as doubles; but we want to output with 4 decimal places. The computed double may have rounding errors, but the formatted output with 4 decimals will be correct if we use proper rounding.
Potential issue: When sorting, if two x's are extremely close (like 1.00005 vs 1.00005), but due to rounding errors one becomes slightly less. But if they are mathematically equal, they should be treated as equal. But are there cases where the vertices might have exactly equal x coordinates? Possibly, e.g., isosceles triangle? Could have two vertices with same x coordinate. Then we need to sort them by y. So we need to detect equality properly. Since mathematically they are equal, but double computation from rational numbers with denominator powers of 2? Actually, if midpoints are given as decimals with a finite number of digits, they are rational numbers with denominator a power of 10. Then A = m2+m3-m1 is also a rational with denominator 10 (since sum/difference of numbers with denominator 10). So A is multiple of 0.1? Actually, if midpoints have up to 3 decimals, denominator is 1000? Wait: Input numbers can be reals, but typical input will have limited decimals. They might be like "4.0" which is 4, denominator 1. If they have "5.00", denominator 100. So the vertices will have denominator up to 100 maybe? Actually, if m1,m2,m3 have at most d decimal places, then they can be expressed as integers divided by 10^d. Then A = (int2/10^d + int3/10^d - int1/10^d) = (int2+int3-int1)/10^d, which is an integer divided by 10^d. So exactly representable in decimal, but not necessarily in binary. However, double representation of decimal numbers with finite decimal representation may not be exact (e.g., 0.1 is not exact). But the input reading as double might have small rounding error. Then the linear combination may accumulate error. However, the error is likely small (on the order of 1e-15). For equality, we can use EPS=1e-9, which is larger than such errors. So it's safe.
Thus proceed.
Now, code skeleton:
#include
#include
#include
#include
#include
using namespace std;
struct Point {
double x, y;
Point(double x=0, double y=0): x(x), y(y) {}
};
bool cmp(const Point& a, const Point& b) {
const double EPS = 1e-9;
if (fabs(a.x - b.x) > EPS) return a.x < b.x;
return a.y < b.y;
}
int main() {
vector mid(3);
for (int i = 0; i < 3; ++i) {
cin >> mid[i].x >> mid[i].y;
}
vector vert(3);
vert[0].x = mid[1].x + mid[2].x - mid[0].x;
vert[0].y = mid[1].y + mid[2].y - mid[0].y;
vert[1].x = mid[2].x + mid[0].x - mid[1].x;
vert[1].y = mid[2].y + mid[0].y - mid[1].y;
vert[2].x = mid[0].x + mid[1].x - mid[2].x;
vert[2].y = mid[0].y + mid[1].y - mid[2].y;
sort(vert.begin(), vert.end(), cmp);
cout << fixed << setprecision(4);
for (int i = 0; i < 3; ++i) {
cout << vert[i].x << " " << vert[i].y << endl;
}
return 0;
}
Check sample.
But note: We used mid[0], mid[1], mid[2] as m1,m2,m3. The order doesn't matter as argued.
Potential improvement: To avoid negative zero etc? Not needed.
Thus solution is correct.
Now, maybe there is a twist: The points might be given in unspecific order, but the formula yields correct vertices. However, one might worry about which point corresponds to which side: Actually, the three midpoints could correspond to the three sides, but the mapping to D, E, F is arbitrary; but the triangle we get is the same set of vertices regardless, as we said. Let's verify: Suppose we have three midpoints M_AB, M_BC, M_CA (midpoints of sides AB, BC, CA). If we pick any of them as D (midpoint of BC), we need to assign the other two appropriately to E and F to get vertices. But if we assign them arbitrarily, we might get a different triangle? Let's test with example: Let the original triangle vertices: (0,0), (2,0), (0,2). Then midpoints: M_AB = (1,0), M_BC = (1,1), M_CA = (0,1). Now if we take D = M_AB, E = M_BC, F = M_CA. Then compute A = E+F-D = (1+0-1, 1+1-0) = (0,2) which is actually vertex C? Wait, that's (0,2) which is C, not A. B = F+D-E = (0+1-1, 1+0-1) = (0,0) which is A? Actually A originally (0,0)? It's (0,0) which is A. C = D+E-F = (1+1-0, 0+1-1) = (2,0) which is B. So we get the same set but permuted. So indeed any ordering yields the same set. So it's fine.
Thus solution is simple.
Now, let's think if there is any alternative interpretation: Some might think that the given points are the midpoints of the sides but not necessarily in any order, and we need to output the original triangle vertices sorted. That's exactly what we did.
Thus answer.
Edge Cases: Input like 0 0, 0 0, 0 0? That would be degenerate, but not given.
So we are good.
Now, writing final answer.
But wait: The problem might be from SPOJ or CodeChef? The sample uses decimals. Ensure we handle trailing spaces, etc.
Double-check output format: "Output 3 lines with 2 space-separated reals - coordinates of the triangle vertices. Each number should have exactly 4 digits after dot." So we use fixed and setprecision(4). That will produce e.g., "4.0000 2.0000". Good.
Thus final code.
But there is a nuance: When printing double, if the value is -0.0000, we might get "-0.0000". Could that happen? Possibly if vertices compute to something like -0.0 due to rounding. But since input coordinates are between 0 and 1000, and midpoints are between 0 and 1000, vertices might be negative? Possibly if midpoints are near edges. Example: midpoints (0.0,0.0), (0.0,1.0), (1.0,0.0). Then vertices: (0+1-0,0+0-0?) Actually compute: using formula: m1=(0,0), m2=(0,1), m3=(1,0). Then vertices: A = m2+m3-m1 = (0+1-0,1+0-0) = (1,1), B = m3+m1-m2 = (1+0-0,0+0-1) = (1,-1), C = m1+m2-m3 = (0+0-1,0+1-0) = (-1,1). So indeed negative numbers can occur. Then output would be negative. That's fine. But double might produce -0.0 for exactly zero? For vertex with x=0, it might be -0.0 if computation yields -0.0 due to rounding. But since we compute sum/difference, if result is exactly zero, double might represent as +0 or -0 depending. But we can avoid this by adding a small epsilon? Not needed; printing "-0.0000" might be considered incorrect? Usually judges accept -0.0000 as equivalent to 0.0000? They might check string equality? Might be problematic. To be safe, we can add a tiny offset? Better to use printf with format specifier that doesn't produce signed zero? Actually printf("%.4f", -0.0) prints "-0.0000". Some judges may not accept that if expected "0.0000". But if the true coordinate is exactly zero, we want "0.0000". Our computation may yield -0.0 due to operations like (0.0 - 0.0) maybe yields 0.0, but if we subtract a number from something and get -0? For example, if we compute -0.0 as result of (a - b) where a and b are equal but one is -0.0? But input coordinates are positive and read as positive zero? They might be read as 0.0. The subtraction might yield exactly 0.0; standard IEEE: 0.0 - 0.0 = 0.0, not -0.0. However, operations like -x where x is 0.0 yields -0.0. But we don't have unary minus. Our formulas are additions and subtractions: vert[0].x = m1.x + m2.x - m3.x etc. If m1.x + m2.x equals m3.x exactly, then vert[0].x = 0.0. But if m1.x + m2.x and m3.x are both positive, the result is 0.0 with sign positive? Actually, 5 - 5 = +0.0? In IEEE, 5.0 - 5.0 yields +0.0. Because both operands are positive, the result is +0.0. So we shouldn't get -0.0 unless we subtract something from a negative zero? But our inputs are non-negative. So probably no -0.0. However, if some vertex coordinate is exactly zero, it will be +0.0. So safe.
Thus final code.
Now, maybe there is a requirement to sort with tolerance? But we already use epsilon.
Now, one more check: The problem statement: "You are given 3 points - middles of the sides of some triangle." Could there be multiple triangles with given midpoints? No, the triangle is uniquely determined. The only ambiguity is labeling, but we don't care.
Thus final answer is this.
I'll craft the explanation accordingly.
But wait: Could there be a case where the three points are given not as the midpoints of a triangle but as some points that could be midpoints of multiple triangles? Actually, given three points, if they are not collinear, there is exactly one triangle for which they are the midpoints. So it's unique.
Thus we are done.
Now, for completeness, ensure we handle input with possible extra spaces or newlines. Use cin.
Now, write final solution.
But let's also consider if the points might be given in a different order that could produce a different triangle if we don't assign them correctly? But we argued invariance. Let's confirm with algebra: Let the three midpoints be P, Q, R. If we take any permutation, say we compute A = Q+R-P, B = R+P-Q, C = P+Q-R. If we choose a different permutation, say A' = R+P-Q, B' = P+Q-R, C' = Q+R-P. That's just a permutation of {A,B,C}. So set is same.
Thus fine.
Edge Cases: Might be that the computed vertices are not distinct (degenerate)? But then midpoints collinear, not given.
Thus code is correct.
One more nuance: The problem may require sorting by first coordinate ascending and second ascending, but if there are ties, we need to ensure that we output the sorted order. Using our comparator works.
I'll produce the final answer as the code.
Now, I'll provide an explanation: The triangle vertices can be found by solving the system: Let the midpoints be D, E, F. Then vertices: A = E+F-D, B = F+D-E, C = D+E-F. Sorting and output.
Thus final. ```cpp
#include
#include
#include
#include
#include
using namespace std;
struct Point {
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
};
// comparator: first by x, then by y (with a small epsilon for safety)
bool cmp(const Point& a, const Point& b) {
const double EPS = 1e-9;
if (fabs(a.x - b.x) > EPS) return a.x < b.x;
return a.y < b.y;
}
int main() {
vector mid(3);
for (int i = 0; i < 3; ++i) {
cin >> mid[i].x >> mid[i].y;
}
vector<Point> vert(3);
// A = m2 + m3 - m1
vert[0].x = mid[1].x + mid[2].x - mid[0].x;
vert[0].y = mid[1].y + mid[2].y - mid[0].y;
// B = m3 + m1 - m2
vert[1].x = mid[2].x + mid[0].x - mid[1].x;
vert[1].y = mid[2].y + mid[0].y - mid[1].y;
// C = m1 + m2 - m3
vert[2].x = mid[0].x + mid[1].x - mid[2].x;
vert[2].y = mid[0].y + mid[1].y - mid[2].y;
sort(vert.begin(), vert.end(), cmp);
cout << fixed << setprecision(4);
for (int i = 0; i < 3; ++i) {
cout << vert[i].x << " " << vert[i].y << "\n";
}
return 0;
}