Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to find the other two vertices of a square given two vertices. The square's sides are parallel to the axes, i.e., it's axis-aligned. So the square's edges are horizontal and vertical. Therefore the vertices are (x, y), (x+side, y), (x, y+side), (x+side, y+side) for some side length s (which could be positive or negative? Actually square sides are length, but s could be positive if we define orientation; but vertices can be any order. Important: the square is axis-aligned, meaning sides are parallel to x and y axes. So the square can be defined by min and max coordinates. Let's denote the square's sides: left x = L, right x = L + s, bottom y = B, top y = B + s, where s is the side length (positive if square has area, but s could be negative if we consider orientation; better to treat s as possibly positive or negative, but absolute value is side length). The four vertices are: (L, B), (L, B+s), (L+s, B), (L+s, B+s). So they consist of two x coordinates (L and L+s) and two y coordinates (B and B+s).
Given two points (x1,y1) and (x2,y2) distinct. They must be two vertices of this square. That means they are either:
sharing same x coordinate (both left or both right) OR sharing same y coordinate (both bottom or both top) OR they are opposite corners (diagonally opposite) i.e., one is (L,B) and the other is (L+s, B+s) OR (L, B+s) and (L+s, B). So there are several cases.
We need to find if there exists a square (axis-aligned) such that these two points are vertices. If yes, output the other two vertices (any order, but typically as x3 y3 x4 y4). Output -1 if not possible.
Constraints: Input coordinates between -100 and 100, output coordinates must be between -1000 and 1000. So we can output coordinates that fit.
We need to handle all cases.
Let's denote the two points: A = (x1, y1), B = (x2, y2). Since square is axis-aligned, the vertices have only two possible x-values and two possible y-values. So the two given points must have x coordinates either equal or different; if equal, then they share x coordinate, and then the side length must be |y2 - y1|, and the other two vertices will have x = that common x +/- side? Wait careful: If two points share same x coordinate, they lie on a vertical line. For them to be vertices of an axis-aligned square, they must be either left side or right side vertices. That means the square's left x = x_common? Actually if they share x coordinate, then that x could be either the left or right side. The other x coordinate would be x_common ± s, where s = difference in y (the vertical distance). But s must be positive? The square side length is absolute difference in y, because the vertical side length is difference. Since sides are parallel to axes, the square side length must be equal to the absolute difference between the y coordinates if they are on same vertical line (i.e., they are two vertices that share same x). However, they could also be the two opposite vertices? No, opposite vertices have different x and different y. So if x1 = x2, they are not opposite corners (since opposite corners would have different x). Also they could be top and bottom vertices on left side or right side. So then the square side length is d = |y2 - y1|. Then the other two vertices would have x coordinate = x1 ± d, and y coordinates the same as y1 and y2 respectively. But careful: If we take A and B as vertical pair, then they define the side length s = |y2 - y1|. The other two vertices must be either to the right or left of that vertical edge. So there are two possible squares: one with the missing vertices at (x1 + s, y1) and (x1 + s, y2) OR (x1 - s, y1) and (x1 - s, y2). Both are valid as long as coordinates within bounds? The problem only requires output any solution within -1000 to 1000. So we can pick either if it exists; but also we must ensure that the points are distinct? Yes, A and B are distinct, so s > 0. So if x1 = x2, we can output (x1 + d, y1) and (x1 + d, y2) as one solution, or the other. But we need to check if these coordinates are within allowed range -1000..1000; d up to 200 (since inputs are -100..100), so x1 ± d is between -300 and 300, which is within -1000..1000. So always fine. So for this case we can output something.
But wait: What if y1 == y2? Then they share same y coordinate, lying on horizontal line. Then similarly side length = |x2 - x1| = d. Then other vertices are (x1, y1 + d) and (x2, y2 + d) OR (x1, y1 - d) and (x2, y2 - d). Again two possible squares.
Now if x1 != x2 and y1 != y2, then they could be opposite corners of the square (diagonally opposite). In an axis-aligned square, the two opposite corners have coordinates (L, B) and (L+s, B+s) (or (L, B+s) and (L+s, B)). In such case, the vector difference (dx, dy) = (x2-x1, y2-y1). For them to be opposite corners of a square, we must have |dx| = |dy| because the diagonal of a square is at 45°? Actually in an axis-aligned square, the diagonal length is s√2. The vector between opposite corners has components (s, s) up to sign, i.e., absolute values equal. So condition: |x2 - x1| = |y2 - y1| and also not zero (since distinct points). If this holds, then we can treat them as opposite vertices, and the other two vertices are (x1, y2) and (x2, y1). Let's check: Suppose A = (L, B) and B = (L+s, B+s). Then the other vertices are (L, B+s) = (x1, y2) and (L+s, B) = (x2, y1). Similarly if A = (L, B+s) and B = (L+s, B), then the other vertices are (L, B) = (x1, y2?) Actually A = (L, B+s) so x1 = L, y1 = B+s; B = (L+s, B) so x2 = L+s, y2 = B. Then other vertices are (L, B) = (x1, y2) and (L+s, B+s) = (x2, y1). So indeed in both cases, the other two vertices are (x1, y2) and (x2, y1). So if |dx| = |dy| and dx,dy != 0 (since distinct), then we have a valid square with those two as opposite corners. So we can output (x1, y2) and (x2, y1). Note that these coordinates must be distinct from the given ones? They will be different as long as s != 0, which is true.
If x1 != x2, y1 != y2, but |dx| != |dy|, then it's impossible to form an axis-aligned square from these two points: they cannot be adjacent because if they were adjacent (sharing a side), they would share either x or y (since sides are axis-aligned: adjacent vertices either have same x or same y). Since they differ in both x and y, they could be opposite only if |dx| = |dy|. So if not equal, answer is -1.
Edge cases: What if the two points are the same? Problem says distinct, so not an issue.
What if side length zero? Distinct ensures s > 0.
Now, there is one more subtlety: The output coordinates must be within -1000 to 1000. Our computed coordinates for the first two cases: for x equal case: (x1 ± d, y1) and (x1 ± d, y2). d <= 200, x1 between -100 and 100, so within -300..300. For y equal case: similarly. For diagonal case: (x1, y2) and (x2, y1). These coordinates are within -100 to 100? Actually x1,y2 etc are within input ranges, so within -100..100, safe. So all within -1000..1000.
Thus algorithm is simple.
Check if x1 == x2:
d = |y2 - y1|
output (x1 + d, y1) (x1 + d, y2) (or x1 - d; we need to output any; we can choose one; but careful: if we choose plus, it's fine as long as within bounds. We could also output minus; it's okay)
else if y1 == y2:
d = |x2 - x1|
output (x1, y1 + d) (x2, y2 + d) (or y1 - d)
else:
if |x2 - x1| == |y2 - y1|:
output (x1, y2) (x2, y1)
else:
output -1
But wait: There is also possibility that the given two points are such that they are same x or same y, but also we could treat them as diagonal? If x1==x2, then |dx| = 0 and |dy| != 0, so condition |dx| = |dy| fails, so we don't consider diagonal. So it's fine.
But is there any case where x1 == x2 and y1 == y2? Not distinct.
Now, the problem statement mentions: "Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000)." Our computed coordinates are fine.
Also note: The order of output: "four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees." It doesn't matter which point is first. So we can output as we like.
But we need to be careful: For the case x1 == x2, we output the two missing vertices. However, there is a nuance: The two given points are on same vertical line; they could be top and bottom of the square. But what if we output (x1 + d, y1) and (x1 + d, y2)? That gives a square with vertices: (x1, y1), (x1, y2), (x1+d, y1), (x1+d, y2). This is indeed a square with side length d, assuming d > 0. But does it matter which y is larger? Not really; the points are distinct, so d>0. The square's sides are parallel to axes. So it's valid.
Similarly, for y1 == y2: output (x1, y1+d) and (x2, y2+d). But wait: For the horizontal case, if we output (x1, y1+d) and (x2, y2+d), that yields four points: (x1, y1), (x2, y1), (x1, y1+d), (x2, y1+d). This is a square only if d = |x2 - x1|. Since we set d = |x2 - x1|, that works. However, note that we must ensure that the side length d is positive; it is. So all good.
Now consider the diagonal case: (x1, y2) and (x2, y1) are the other two vertices. Is it always true that these four points form an axis-aligned square? Let's check: Suppose A = (0,0), B = (2,2). Then the others are (0,2) and (2,0). That square has side length 2, and is axis-aligned. Good. If A = (0,2), B = (2,0), then others are (0,0) and (2,2). Also works.
But what about sign? If dx = 2, dy = -2 (i.e., x1=0,y1=0, x2=2,y2=-2). Then |dx| = |dy| = 2, condition holds. The others: (0, -2) and (2, 0). Check if these form a square: Points: (0,0), (2,-2), (0,-2), (2,0). That's a square with side length 2? Compute distances: (0,0) to (2,0) is 2 horizontal; (2,0) to (2,-2) is 2 vertical; etc. Yes axis-aligned square. So works.
Thus algorithm stands.
But wait: There might be other solutions? The problem says if there is no solution print -1. There is also a possibility that the two given points could be the same? But it's not allowed.
Now check examples:
Input 0 0 0 1 => x equal => d = 1 => output 1 0 1 1 (chose plus). That matches.
Input 0 0 1 2 => |dx|=1, |dy|=2, not equal, not same x/y => -1 => matches.
All good.
Now, there is a corner case: What if the two points are like (0,0) and (0,0)? Not distinct so ignore.
Now, what about points like (0,0) and (0,0)? Not.
Now, think about the output range: -1000 to 1000. Our outputs will be within that.
But we need to consider if we choose plus or minus direction we must ensure coordinates are within range. For x equal case: x1 could be 100, d could be 200, so x1 + d = 300, within 1000. x1 - d = -100, also fine. For y equal case similarly.
Now, there is also a degenerate case: If the two points are exactly at positions such that when we choose plus direction, the missing points might be outside -1000? But maximum coordinate magnitude is 100 (input) plus d up to 200 = 300. Minimum -100 - 200 = -300. So always within -1000 to 1000. So safe.
Thus the solution is straightforward.
Now implementation: read four ints. Compute dx = x2 - x1, dy = y2 - y1.
If dx == 0:
d = abs(dy)
print x1 + d, y1, x1 + d, y2 (or choose minus; we can pick plus)
elif dy == 0:
d = abs(dx)
print x1, y1 + d, x2, y2 + d
else:
if abs(dx) == abs(dy):
print x1, y2, x2, y1
else:
print -1
But careful: In the first two cases, there is also possibility that we could output the other orientation (minus) but we output plus; it's fine. However, need to ensure that output coordinates are integers; they are.
Edge: When dx == 0 and we output (x1 + d, y1) and (x1 + d, y2), we must ensure that these are indeed distinct from the given ones? Possibly if d=0, but not. So okay.
One more nuance: In the horizontal case (dy == 0), if we output (x1, y1 + d) and (x2, y2 + d), note that (x2, y2 + d) is same as (x2, y1 + d) because y2 = y1. So that's fine.
Now, verify with example: Input 0 0 2 0 => dx=2, dy=0 => d=2 => output (0,2) and (2,2). That yields square with side length 2. Good.
Now, check with negative coordinates: e.g., Input -1 -1 -1 3 => dx=0, d =4 => output (-1+4, -1)= (3, -1) and (3,3)= (3,3). That square: vertices (-1,-1), (-1,3), (3,-1), (3,3). Side length 4, axis-aligned. Works.
What about diagonal with negative: (-2, -3) and (1, -6)? dx = 3, dy = -3 => |dx|=|dy|=3. Output (-2, -6) and (1, -3). Check distances: (-2,-3) to (1,-3) = 3 horizontal; (1,-3) to (1,-6) = 3 vertical; etc. Works.
Thus solution is correct.
Now, is there any scenario where the given points are such that they are same x or same y, but we also have another possible configuration like they could be opposite corners? No, because if they share x or y, they cannot be opposite corners because opposite corners have different x and different y. So the only valid configuration is as adjacent vertices, which we handle. But could there be a configuration where the two given points are not adjacent but still possible as part of a square? In an axis-aligned square, there are only three possible relationships between any two vertices: they could be the same (excluded), adjacent (share side: same x or same y), or opposite (different x and y and |dx|=|dy|). That's all. So our classification covers all possibilities.
Thus answer is correct.
Now implement in Python. We'll read ints.
Make sure to output -1 as just a single line with -1.
Edge: In case of dx == 0 and dy == 0? But not possible.
Now, just write the code.
But wait: I've seen a similar problem on Codeforces (CF 459A). Indeed it's from Codeforces: Pashmak and Garden. And the solution is as above. However, there is a nuance: The problem statement says "there is exactly one tree on each vertex of the square." So we have four trees. We are given two of them. We need to output the other two. The square sides are parallel to coordinate axes.
Thus final answer.
Let's produce final code:
code
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
x1, y1, x2, y2 = map(int, data)
dx = x2 - x1
dy = y2 - y1
if dx == 0:
d = abs(dy)
print(x1 + d, y1, x1 + d, y2)
elif dy == 0:
d = abs(dx)
print(x1, y1 + d, x2, y2 + d)
else:
if abs(dx) == abs(dy):
print(x1, y2, x2, y1)
else:
print(-1)
But we need to consider that in the first case, if we output (x1 + d, y1) and (x1 + d, y2) we need to ensure they are integers. They are.
Alternatively, some solutions output the other side (x1 - d) if x1 + d might be out of range, but it's not required; any solution within bounds is accepted. Since both are within bounds, we can choose plus. But what if x1 = 1000? Actually input is limited to -100..100, so x1 max 100, d max 200, so max 300. So safe.
Now, I'll produce final answer.
But wait: The problem statement: "Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000)." Our coordinates are within -300 to 300. So fine.
Now I'm going to write the final solution with explanation.
But the user asked: "Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones."
So we need to output code.
Given that it's a standard problem, answer is as above.
Now provide final response.
But the user might be expecting just the solution explanation, but since they posted the full problem statement, likely they want code. So I'll output Python code with explanation.
However, note: The conversation says "You are ChatGPT...". The user didn't explicitly request code; they just gave the problem statement. Usually in such scenarios, we need to provide a solution. Since it's a problem from Codeforces, it's typical to provide code. I'll provide a solution in Python and explain logic.
Thus final answer. ```python
import sys
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
x1, y1, x2, y2 = map(int, data)
code
dx = x2 - x1
dy = y2 - y1
# same x-coordinate → vertical side
if dx == 0:
d = abs(dy)
# choose the square to the right (could also choose left)
print(x1 + d, y1, x1 + d, y2)
# same y-coordinate → horizontal side
elif dy == 0:
d = abs(dx)
# choose the square above (could also choose below)
print(x1, y1 + d, x2, y2 + d)
else:
# opposite corners only possible if |dx| == |dy|
if abs(dx) == abs(dy):
print(x1, y2, x2, y1)
else:
print(-1)