text: Find Corners of Rectangle using mid points | C ++ 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
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; struct Point { float x , y ; Point ( ) { x = y = 0 ; } Point ( float a , float b ) { x = a , y = b ; } } ; void printCorners ( Point p , Point q , float l ) { Point a , b , c , d ; 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 { float m = ( p . x - q . x ) / float ( q . y - p . y ) ; float dx = ( l / sqrt ( 1 + ( m * m ) ) ) * 0.5 ; float 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 ; } cout << a . x << " , ▁ " << a . y << " ▁ n " << b . x << " , ▁ " << b . y << " n " ; << c . x << " , ▁ " << c . y << " ▁ n " << d . x << " , ▁ " << d . y << " nn " ; } int main ( ) { Point p1 ( 1 , 0 ) , q1 ( 1 , 2 ) ; printCorners ( p1 , q1 , 2 ) ; Point p ( 1 , 1 ) , q ( -1 , -1 ) ; printCorners ( p , q , 2 * sqrt ( 2 ) ) ; return 0 ; }