Find Corners of Rectangle using mid 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
class Point { constructor ( a , b ) { this . x = a ; this . y = b ; } } function printCorners ( p , q , l ) { let a = new Point ( ) , b = new Point ( ) , c = new Point ( ) , d = new Point ( ) ; if ( p . x == q . x ) { a . x = ( p . x - ( l / 2.0 ) ) ; a . y = p . y ; d . x = ( p . x + ( l / 2.0 ) ) ; d . y = p . y ; b . x = ( q . x - ( l / 2.0 ) ) ; b . y = q . y ; c . x = ( q . x + ( l / 2.0 ) ) ; c . y = q . y ; } else if ( p . y == q . y ) { a . y = ( p . y - ( l / 2.0 ) ) ; a . x = p . x ; d . y = ( p . y + ( l / 2.0 ) ) ; d . x = p . x ; b . y = ( q . y - ( l / 2.0 ) ) ; b . x = q . x ; c . y = ( q . y + ( l / 2.0 ) ) ; c . x = q . x ; } else { let m = ( p . x - q . x ) / ( q . y - p . y ) ; let dx = ( ( l / Math . sqrt ( 1 + ( m * m ) ) ) * 0.5 ) ; let dy = m * dx ; a . x = p . x - dx ; a . y = p . y - dy ; d . x = p . x + dx ; d . y = p . y + dy ; b . x = q . x - dx ; b . y = q . y - dy ; c . x = q . x + dx ; c . y = q . y + dy ; } document . write ( a . x + " " + a . y + " " + b . x + " " + b . y + " " + c . x + " " + c . y + " " + d . x + " " + d . y + " " ) ; } let p1 = new Point ( 1 , 0 ) , q1 = new Point ( 1 , 2 ) ; printCorners ( p1 , q1 , 2 ) ; let p = new Point ( 1 , 1 ) , q = new Point ( - 1 , - 1 ) ; printCorners ( p , q , ( 2 * Math . sqrt ( 2 ) ) ) ;