Search code examples
pythonmathcoordinatesrectangles

Using python to find the missing coordinate of a rectangle


Problem: Given integer coordinates of three vertices of a rectangle whose sides are parallel to coordinate axes, find the coordinates of the fourth vertex of the rectangle.

I have written the code to answer the problem as follows (but it's not correct):

coord_1_x = int(input())
coord_1_y = int(input())

coord_2_x = int(input())
coord_2_y = int(input())

coord_3_x = int(input())
coord_3_y = int(input())

coord_4_x = 0
coord_4_y = 0

if coord_1_x == coord_2_x:
  coord_4_x = coord_3_x
  if coord_2_y > coord_1_y:
    coord_4_y = coord_2_y
  else:
    coord_4_y = coord_1_y
else:
  if coord_3_x == coord_1_x:
    coord_4_x = coord_2_x
    coord_4_y = coord_3_y
    
print(coord_4_x)
print(coord_4_y)

Here's some example inputs/outputs that the code should display:

Example input #1 - Three vertices given are (1, 5), (7, 5), (1, 10)

1
5
7
5
1
10

Example output #1

7
10

Example input #2 - Three vertices given are (1, 5), (7, 10), (1, 10)

1
5
7
10
1
10

Example output #2

7
5

Please can someone help me determine the correct code to answer this problem? (I've tried Googling/ reading previous Stack posts but can't find an answer)

Note: The code should only use if/else statements, not arrays or loops.


Solution

  • if coord1_x == coord2_x or coord1_x==coord3_x:
        if coord1_x == coord2_x:
               coord4_x=coord3_x
        else:
               coord4_x=coord2_x
    else:
        coord4_x=coord1_x
    if coord1_y == coord2_y or coord1_y==coord3_y:
        if coord1_y == coord2_y:
               coord4_y=coord3_y
        else:
               coord4_y=coord2_y
    else:
        coord4_y=coord1_y