Square with missing coordinates

Hello, I was trying to solve a problem but I just can't seem to get the idea of how it should be solved, like I know to get the coordinates of a square you need the 2 corners to get the other 2 but this just fails, here is the problem:


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


I REALLY don't get why the last one can't be solved, although it could be
0 2 1 0
as output also here is a code that solved the problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <math.h>
int main(){
    int x1,x2,x3,x4,y1,y2,y3,y4;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

    if (abs(x2-x1) == abs(y2 - y1)){
            y4 = y2;
            y3= y1;
            x4=x1;
            x3=x2;
            printf("%d %d %d %d", x4 ,y4, x3, y3);

    }else if (x1 == x2){

        x4= abs(y2-y1)+x1;
        x3=x4;
        y4=y1;y3=y2;
        printf("%d %d %d %d", x4 ,y4, x3, y3);
    }else if( y2 == y1){
            x3=x1;
            x4=x2;
            y4 = abs(x2-x1) + y2;
            y3 = y4;
            printf("%d %d %d %d", x4 ,y4, x3, y3);
    }else {

        printf("-1");
    }

    return 0;
}



And if someone can explain the concept behind using such maths or such methods it'd be really appreciated! :)
The last case can't be solved in the context of the problem because the sides of the square are stated to be parallel to the coordinate axes.

If they are diagonally-opposed points then the absolute difference in x values must equal the absolute difference in y coordinates. This is the first 'if' case.

If the x values are the same then they are adjacent points along a vertical line. The distance between them gives the side of the square, and hence the displacement in the x direction to the other side of the square. This is the next if statement. If the y values are the same then you get the last if case.

Anything else will not give a square with sides parallel to coordinate axes.

Since the square may be completed on either side, the abs() in lines 16 and 23 are actually unnecessary.
Last edited on
@lastchance:
1
2
3
4
5
6
else if (x1 == x2){

        x4= abs(y2-y1)+x1;
        x3=x4;
        y4=y1;
        y3=y2;


why is the +x1 used? when if we subtract the difference in y's we can get the x what's the point of adding x1? also i dont get the coord numbers and their relations with each square and by that i mean

is it
2 3

1 4

or
4 3

1 2

i am in a big confusion honestly xD

If you have an indepth explanation to this part it'll make me understand the rest pretty well ^^
Last edited on
For the 'if' block that you state, with the same x coordinates:
- points 1 and 2 will be on the same vertical side (one top, one bottom, depending on y coordinates);
- abs(y2-y1) is the length of this side, hence the side of the square;
- to get to the other vertical side of the square you can simply add this length to the two x coordinates (but since the original x coordinates were equal you only need to do this addition once).

For what is written (x1=x2) you will have EITHER
1 4

2 3

OR

2 3

1 4

depending on whether point 1 or 2 had the bigger y coordinate (it does not actually matter which).


Do you take all the problems that you post from some quaint Computer Science version of the Arabian Nights BTW?
Last edited on
@lastchance:

actually no idk anything with arabian nights or w/e that is called, its codeforces.com

http://codeforces.com/problemset/problem/459/A is the problem link for what i posted above and there are plenty of websites like codeforces.com like a2oj.com .. uva .. hackerearth.. many programming contests websites, its a good practice imo and i really enjoy it ^^

also, on topic:

saying x4= abs(y2-y1);
instead of x4= abs(y2-y1)+x1; wouldn't it be the same? cause imo i see the y2-y2 doing the deed, i tried drawing it on a paper to see if y2-y2 is not correct but it is, idk if thats code related lol.
@Bliink
They would only be the same if x1 equals 0.

In general, for this case you have to start at x1 and move distance side-of-square. Line 16 in your original sample code is correct.
@lastchance
For what is written (x1=x2) you will have EITHER
1 4

2 3

OR

2 3

1 4


So if it was the other case with y1 = y2 it'd be

1 2

4 3

?

If it was y1 equals y2 then the points would become (from your code):

3 4

1 2

OR

4 3

2 1

depending on whether points 1 or 2 had the bigger x coordinate.

Note that in the mathematical cartesian system y points positive up; not like your computer console where you tend to count down from the top.

I'm at a loss to describe it in any other way, @Bliink.

BTW I've just noticed that your code is outputting x4 y4 before x3 y3, which may add to the confusion.
Last edited on
Yea it's outputting x4 y4 x3 y3 because it considers the square points x4 and y4 before x3 and y3 making them x3 and y3 before x4 and y4 in actual but in different variable names if you get what i mean, it was a typo that was just continued upon rather than renaming xP

@lastchance
Topic archived. No new replies allowed.