Find Corners of Rectangle using mid points | Python3 program to find corner points of a rectangle using given length and middle points . ; Structure to represent a co - ordinate point ; This function receives two points and length of the side of rectangle and prints the 4 corner points of the rectangle ; Horizontal rectangle ; Vertical rectangle ; Slanted rectangle ; Calculate slope of the side ; Calculate displacements along axes ; Driver code
import math NEW_LINE class Point : NEW_LINE INDENT def __init__ ( self , a = 0 , b = 0 ) : NEW_LINE INDENT self . x = a NEW_LINE self . y = b NEW_LINE DEDENT DEDENT def printCorners ( p , q , l ) : NEW_LINE INDENT a , b , c , d = Point ( ) , Point ( ) , Point ( ) , Point ( ) NEW_LINE if ( p . x == q . x ) : NEW_LINE INDENT a . x = p . x - ( l / 2.0 ) NEW_LINE a . y = p . y NEW_LINE d . x = p . x + ( l / 2.0 ) NEW_LINE d . y = p . y NEW_LINE b . x = q . x - ( l / 2.0 ) NEW_LINE b . y = q . y NEW_LINE c . x = q . x + ( l / 2.0 ) NEW_LINE c . y = q . y NEW_LINE DEDENT elif ( p . y == q . y ) : NEW_LINE INDENT a . y = p . y - ( l / 2.0 ) NEW_LINE a . x = p . x NEW_LINE d . y = p . y + ( l / 2.0 ) NEW_LINE d . x = p . x NEW_LINE b . y = q . y - ( l / 2.0 ) NEW_LINE b . x = q . x NEW_LINE c . y = q . y + ( l / 2.0 ) NEW_LINE c . x = q . x NEW_LINE DEDENT else : NEW_LINE INDENT m = ( p . x - q . x ) / ( q . y - p . y ) NEW_LINE dx = ( l / math . sqrt ( 1 + ( m * m ) ) ) * 0.5 NEW_LINE dy = m * dx NEW_LINE a . x = p . x - dx NEW_LINE a . y = p . y - dy NEW_LINE d . x = p . x + dx NEW_LINE d . y = p . y + dy NEW_LINE b . x = q . x - dx NEW_LINE b . y = q . y - dy NEW_LINE c . x = q . x + dx NEW_LINE c . y = q . y + dy NEW_LINE DEDENT print ( int ( a . x ) , " , ▁ " , int ( a . y ) , sep = " " ) NEW_LINE print ( int ( b . x ) , " , ▁ " , int ( b . y ) , sep = " " ) NEW_LINE print ( int ( c . x ) , " , ▁ " , int ( c . y ) , sep = " " ) NEW_LINE print ( int ( d . x ) , " , ▁ " , int ( d . y ) , sep = " " ) NEW_LINE print ( ) NEW_LINE DEDENT p1 = Point ( 1 , 0 ) NEW_LINE q1 = Point ( 1 , 2 ) NEW_LINE printCorners ( p1 , q1 , 2 ) NEW_LINE p = Point ( 1 , 1 ) NEW_LINE q = Point ( - 1 , - 1 ) NEW_LINE printCorners ( p , q , 2 * math . sqrt ( 2 ) ) NEW_LINE