Find Corners of Rectangle using mid points
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++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG
{
static class Point
{
float x, y;
Point()
{
x = y = 0 ;
}
Point( float a, float b)
{
x = a;
y = b;
}
};
static void printCorners(Point p, Point q, float l)
{
Point a = new Point(), b = new Point(),
c = new Point(), d = new Point();
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;
}
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;
}
else
{
float m = (p.x - q.x) / (q.y - p.y);
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" );
}
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 )));
}
}
|
Python3
import math
class Point:
def __init__( self , a = 0 , b = 0 ):
self .x = a
self .y = b
def printCorners(p, q, l):
a, b, c, d = Point(), Point(), Point(), 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 :
m = (p.x - q.x) / (q.y - p.y)
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 ()
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 ))
|
C#
using System;
class GFG
{
public class Point
{
public float x, y;
public Point()
{
x = y = 0;
}
public Point( float a, float b)
{
x = a;
y = b;
}
};
static void printCorners(Point p, Point q, float l)
{
Point a = new Point(), b = new Point(),
c = new Point(), d = new Point();
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;
}
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;
}
else
{
float m = (p.x - q.x) / (q.y - p.y);
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" );
}
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)));
}
}
|
Javascript
<script>
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 + " <br>"
+ b.x + ", " + b.y + "<br>"
+ c.x + ", " + c.y + " <br>"
+ d.x + ", " + d.y + "<br>" );
}
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)));
</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 😀
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...