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.
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 .
0, 0 0, 2 2, 2 2, 0 0, 2 -2, 0 0, -2 2, 0
This article is contributed by Ashutosh Kumar 😀 If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find if there is a rectangle in binary matrix with corners as 1
- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Coordinates of rectangle with given points lie inside
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Find the percentage change in the area of a Rectangle
- Find minimum area of rectangle with given set of coordinates
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices
- Find the largest rectangle of 1's with swapping of columns allowed
- Program to find Perimeter / Circumference of Square and Rectangle
- Find K Closest Points to the Origin
- Find N random points within a Circle
- Minimum number of points to be removed to get remaining points on one side of axis
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Find whether only two parallel lines contain all coordinates points or not
- Program to find line passing through 2 Points
- Find the maximum possible distance from origin using given points
- Find points at a given distance on a line of given slope
- Find Simple Closed Path for a given set of points
- Ways to choose three points with distance between the most distant points <= L