Saturday 29 July 2017

INLAB 2



Given two adjacent vertices of a square, write an algorithm and the subsequent Python code to determine the length of the diagonal. Pythagorean theorem states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. For example, if the coordinates of the adjacent vertices of a square are given as (10, 15) and (20, 15) then the other two vertices in sorted order are (10, 25) and (20, 25) and length of the diagonal is 14.14. Print only two decimal places for length of the diagonal.
Input Format
X-coordinate of first vertex of the square
Y-coordinate of first vertex of the square
X-coordinate of second vertex of the square
Y-coordinate of second vertex of the square
Output Format
Other two vertices of the square as a tuple, one in each line
Diagonal of the square with two decimal places only
Code:
x1=int(input())
y1=int(input())
x2=int(input())
y2=int(input())
s=int(((x1-x2)**2+(y1-y2)**2)**0.5)
d=(s**2+s**2)**0.5
if(y1==y2):
tup1=(x1,y1+s)
tup2=(x2,y2+s)
else:
tup1=(x1+s,y1)
tup2=(x2+s,y2)
print(tup1)
print(tup2)
print(round(d,2))

Input:
x-coordinate of first vertex
y-coordinate of first vertex
x-coordinate of second vertex
y-coordinate of second vertex

Processing:
if(y1==y2):
tup1=(x1,y1+s)
tup2=(x2,y2+s)
else:
tup1=(x1+s,y1)
tup2=(x2+s,y2)

Output:
Other two vertices of the square as a tuple, one in each line
Diagonal of the square with two decimal places only

Pseudo Code:
1) Read the x and y coordinates of the two given vertices
2) Find the other two vertices by:
           V3 =(x1,y1+s)
   V4 =(x2,y2+s)
3) Print the other two vertices as tuples
4) Print the diagonal of the square

******************************************************************************************

No comments:

Post a Comment