Find Corners of Rectangle using mid points
Last Updated :
10 Apr, 2025
Consider a rectangle ABCD, we're given the co-ordinates of the mid points of side AD and BC (p and q respectively) along with their length L (AD = BC = L). Now given the parameters, we need to print the co-ordinates of the 4 points A, B, C and D.

Examples:
Input : p = (1, 0)
q = (1, 2)
L = 2
Output : (0, 0), (0, 2), (2, 2), (2, 0)
Explanation:
The printed points form a rectangle which
satisfy the input constraints.
Input : p = (1, 1)
q = (-1, -1)
L = 2*sqrt(2)
Output : (0, 2), (-2, 0), (0, -2), (2, 0)
From the problem statement 3 cases can arise :
- The Rectangle is horizontal i.e., AD and BC are parallel to X-axis
- The Rectangle is vertical i.e., AD and BC are parallel to Y-axis
- The Rectangle is inclined at a certain angle with the axes
The first two cases are trivial and can easily be solved using basic geometry. For the third case we need to apply some mathematical concepts to find the points.
Consider the above diagram for clarity. We have the co-ordinates of p and q. Thus we can find the slope of AD and BC (As pq is perpendicular to AD). Once we have the slope of AD, we can find the equation of straight line passing through AD. Now we can apply distance formula to obtain the displacements along X and Y axes.
If slope of AD = m, then
m = (p.x- q.x)/(q.y - p.y)
and displacement along X axis, dx =
L/(2*sqrt(1+m*m))
Similarly, dy = m*L/(2*sqrt(1+m*m))
Now we can simply find the co-ordinates of 4 corners by simply adding and subtracting the displacements obtained accordingly.
Below is the implementation .
C++
// C++ program to find corner points of
// a rectangle using given length and middle
// points.
#include <bits/stdc++.h>
using namespace std;
// Structure to represent a co-ordinate point
struct Point
{
float x, y;
Point()
{
x = y = 0;
}
Point(float a, float b)
{
x = a, y = b;
}
};
// This function receives two points and length
// of the side of rectangle and prints the 4
// corner points of the rectangle
void printCorners(Point p, Point q, float l)
{
Point a, b, c, d;
// horizontal rectangle
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;
}
// vertical rectangle
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;
}
// slanted rectangle
else
{
// calculate slope of the side
float m = (p.x-q.x)/float(q.y-p.y);
// calculate displacements along axes
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";
}
// Driver code
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;
}
Java
// Java program to find corner points of
// a rectangle using given length and middle
// points.
class GFG
{
// Structure to represent a co-ordinate point
static class Point
{
float x, y;
Point()
{
x = y = 0;
}
Point(float a, float b)
{
x = a;
y = b;
}
};
// This function receives two points and length
// of the side of rectangle and prints the 4
// corner points of the rectangle
static void printCorners(Point p, Point q, float l)
{
Point a = new Point(), b = new Point(),
c = new Point(), d = new Point();
// horizontal rectangle
if (p.x == q.x)
{
a.x = (float) (p.x - (l / 2.0));
a.y = p.y;
d.x = (float) (p.x + (l / 2.0));
d.y = p.y;
b.x = (float) (q.x - (l / 2.0));
b.y = q.y;
c.x = (float) (q.x + (l / 2.0));
c.y = q.y;
}
// vertical rectangle
else if (p.y == q.y)
{
a.y = (float) (p.y - (l / 2.0));
a.x = p.x;
d.y = (float) (p.y + (l / 2.0));
d.x = p.x;
b.y = (float) (q.y - (l / 2.0));
b.x = q.x;
c.y = (float) (q.y + (l / 2.0));
c.x = q.x;
}
// slanted rectangle
else
{
// calculate slope of the side
float m = (p.x - q.x) / (q.y - p.y);
// calculate displacements along axes
float dx = (float) ((l / Math.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;
}
System.out.print((int)a.x + ", " + (int)a.y + " \n"
+ (int)b.x + ", " + (int)b.y + "\n"
+ (int)c.x + ", " + (int)c.y + " \n"
+ (int)d.x + ", " + (int)d.y + "\n");
}
// Driver code
public static void main(String[] args)
{
Point p1 = new Point(1, 0), q1 = new Point(1, 2);
printCorners(p1, q1, 2);
Point p = new Point(1, 1), q = new Point(-1, -1);
printCorners(p, q, (float) (2 * Math.sqrt(2)));
}
}
// This code contributed by Rajput-Ji
Python
# Python3 program to find corner points of
# a rectangle using given length and middle
# points.
import math
# Structure to represent a co-ordinate point
class Point:
def __init__(self, a = 0, b = 0):
self.x = a
self.y = b
# This function receives two points and length
# of the side of rectangle and prints the 4
# corner points of the rectangle
def printCorners(p, q, l):
a, b, c, d = Point(), Point(), Point(), Point()
# Horizontal rectangle
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
# Vertical rectangle
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
# Slanted rectangle
else:
# Calculate slope of the side
m = (p.x - q.x) / (q.y - p.y)
# Calculate displacements along axes
dx = (l / math.sqrt(1 + (m * m))) * 0.5
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
print(int(a.x), ", ", int(a.y), sep = "")
print(int(b.x), ", ", int(b.y), sep = "")
print(int(c.x), ", ", int(c.y), sep = "")
print(int(d.x), ", ", int(d.y), sep = "")
print()
# Driver code
p1 = Point(1, 0)
q1 = Point(1, 2)
printCorners(p1, q1, 2)
p = Point(1, 1)
q = Point(-1, -1)
printCorners(p, q, 2 * math.sqrt(2))
# This code is contributed by shubhamsingh10
C#
// C# program to find corner points of
// a rectangle using given length and middle
// points.
using System;
class GFG
{
// Structure to represent a co-ordinate point
public class Point
{
public float x, y;
public Point()
{
x = y = 0;
}
public Point(float a, float b)
{
x = a;
y = b;
}
};
// This function receives two points and length
// of the side of rectangle and prints the 4
// corner points of the rectangle
static void printCorners(Point p, Point q, float l)
{
Point a = new Point(), b = new Point(),
c = new Point(), d = new Point();
// horizontal rectangle
if (p.x == q.x)
{
a.x = (float) (p.x - (l / 2.0));
a.y = p.y;
d.x = (float) (p.x + (l / 2.0));
d.y = p.y;
b.x = (float) (q.x - (l / 2.0));
b.y = q.y;
c.x = (float) (q.x + (l / 2.0));
c.y = q.y;
}
// vertical rectangle
else if (p.y == q.y)
{
a.y = (float) (p.y - (l / 2.0));
a.x = p.x;
d.y = (float) (p.y + (l / 2.0));
d.x = p.x;
b.y = (float) (q.y - (l / 2.0));
b.x = q.x;
c.y = (float) (q.y + (l / 2.0));
c.x = q.x;
}
// slanted rectangle
else
{
// calculate slope of the side
float m = (p.x - q.x) / (q.y - p.y);
// calculate displacements along axes
float dx = (float) ((l / Math.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;
}
Console.Write((int)a.x + ", " + (int)a.y + " \n"
+ (int)b.x + ", " + (int)b.y + "\n"
+ (int)c.x + ", " + (int)c.y + " \n"
+ (int)d.x + ", " + (int)d.y + "\n");
}
// Driver code
public static void Main(String[] args)
{
Point p1 = new Point(1, 0), q1 = new Point(1, 2);
printCorners(p1, q1, 2);
Point p = new Point(1, 1), q = new Point(-1, -1);
printCorners(p, q, (float) (2 * Math.Sqrt(2)));
}
}
// This code has been contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to find corner points of
// a rectangle using given length and middle
// points.
// Structure to represent a co-ordinate point
class Point
{
constructor(a,b)
{
this.x=a;
this.y=b;
}
}
// This function receives two points and length
// of the side of rectangle and prints the 4
// corner points of the rectangle
function printCorners(p,q,l)
{
let a = new Point(), b = new Point(),
c = new Point(), d = new Point();
// horizontal rectangle
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;
}
// vertical rectangle
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;
}
// slanted rectangle
else
{
// calculate slope of the side
let m = (p.x - q.x) / (q.y - p.y);
// calculate displacements along axes
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 + " <br>"
+ b.x + ", " + b.y + "<br>"
+ c.x + ", " + c.y + " <br>"
+ d.x + ", " + d.y + "<br>");
}
// Driver code
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)));
// This code is contributed by rag2127
</script>
Output:
0, 0
0, 2
2, 2
2, 0
0, 2
-2, 0
0, -2
2, 0
Time Complexity: O(1)
Auxiliary Space: O(1)
Reference:
StackOverflow
This article is contributed by Aarti_Rathi and Ashutosh Kumar :D
Similar Reads
Coordinates of rectangle with given points lie inside
Given two arrays X[] and Y[] with n-elements, where (Xi, Yi) represent a point on coordinate system, find the smallest rectangle such that all points from given input lie inside that rectangle and sides of rectangle must be parallel to Coordinate axis. Print all four coordinates of an obtained recta
6 min read
Minimum rectangles to cover all points
Given a 2D array points[][], where points[i] = [xi, yi]. The task is to find the minimum number of rectangles of width `w` or less needed to cover all points in a given 2D array, where each rectangle's lower end is at (x1, 0) and upper end at (x2, y2) with x1 = 0. A point is covered if it's within o
4 min read
Find minimum area of rectangle with given set of coordinates
Given an array arr of set of points in the X-Y plane. The task is to find the minimum area of a rectangle that can be formed from these points. The sides of the rectangle should be parallel to the X and Y axes. If a rectangle cannot be formed with the given points then print 0 .Examples: Input: arr[
5 min read
Check if a point lies inside a rectangle | Set-2
Given coordinates of bottom-left and top-right corners of a rectangle. Check if a point (x, y) lies inside this rectangle or not. Examples: Input: bottom-left: (0, 0), top-right: (10, 8), point: (1, 5) Output: Yes Input: bottom-left: (-1, 4), top-right:(2, 3), point:(0, 4) Output: No This problem is
6 min read
Maximize area of triangle formed by points on sides of given rectangle
Given a rectangle [(x1, y1), (x2, y2)] denoting the coordinates of bottom-left corner and top-right corner whose sides are parallel to coordinates axes and N points on its perimeter (at least one on each side). The task is to maximize the area of a triangle formed by these points. Examples: Input: r
9 min read
Number of rectangles in a circle of radius R
Given a circular sheet of radius R and the task is to find the total number of rectangles with integral length and width that can be cut from the circular sheet, one at a time. Examples: Input: R = 2 Output: 8 8 rectangles can be cut from a circular sheet of radius 2. These are: 1x1, 1x2, 2x1, 2x2,
9 min read
Number of points lying inside a rectangle as well as a triangle
Given two 2D arrays rectangle[][] and triangle[][], representing the coordinates of vertices of a rectangle and a triangle respectively, and another array points[][] consisting of N coordinates, the task is to count the number of points that lies inside both the rectangle and the triangle. Examples:
15 min read
Find the percentage change in the area of a Rectangle
Given two integers P and Q which represents the percentage change in the length and breadth of the rectangle, the task is to print the percentage change in the area of the rectangle. Examples: Input: P = 10, Q = 20 Output: 32 Explanation: Let the initial length of the rectangle be 100 and breadth be
4 min read
Area of the largest Rectangle without a given point
Given the length L and breadth B of a rectangle and the position of a hole in the rectangle as (X, Y) coordinate, the task is to find the area of largest Rectangle within the given Rectangle such that it does not contain the hole.Note: The rectangle is placed at the origin by two of its side touchin
6 min read
Number of squares of maximum area in a rectangle
Given a rectangle of sides m and n. Cut the rectangle into smaller identical pieces such that each piece is a square having maximum possible side length with no leftover part of the rectangle. Print number of such squares formed.Examples: Input: 9 6 Output: 6 Rectangle can be cut into squares of siz
6 min read