Tutorialspoint.dev

Find the Missing Point of Parallelogram

Given three coordinate points A, B and C, find the missing point D such that ABCD can be a parallelogram.

Examples :

Input : A = (1, 0)
        B = (1, 1)
        C = (0, 1)
Output : 0, 0
Explanation:
The three input points form a unit
square with the point (0, 0)

Input : A = (5, 0)
        B = (1, 1)
        C = (2, 5)
Output : 6, 4

As shown in below diagram, there can be multiple possible outputs, we need to print any one of them.
Parallelogram


A quadrilateral is said to be a parallelogram if its opposite sides are parallel and equal in length.

As we’re given three points of the parallelogram, we can find the slope of the missing sides as well as their lengths.
The algorithm can be explained as follows



Let R be the missing point. Now from definition, we have

  • Length of PR = Length of QS = L1 (Opposite sides are equal)
  • Slope of PR = Slope of QS = M1 (Opposite sides are parallel)
  • Length of PQ = Length of RS = L2 (Opposite sides are equal)
  • Slope of PQ= Slope of RS = M2 (Opposite sides are parallel)

Thus we can find the points at a distance L1 from P having slope M1 as mentioned in below article :
Find points at a given distance on a line of given slope.

Now one of the points will satisfy the above conditions which can easily be checked (using either condition 3 or 4).
Below is the program for the same.

C++

// C++ program to find missing point of a
// parallelogram
#include <bits/stdc++.h>
using namespace std;
  
// struct to represent a co-ordinate point
struct Point {
    float x, y;
    Point()
    {
        x = y = 0;
    }
    Point(float a, float b)
    {
        x = a, y = b;
    }
};
  
// given a source point, slope(m) of line
// passing through it this function calculates
// and return two points at a distance l away
// from the source
pair<Point, Point> findPoints(Point source,
                              float m, float l)
{
    Point a, b;
  
    // slope is 0
    if (m == 0) {
        a.x = source.x + l;
        a.y = source.y;
  
        b.x = source.x - l;
        b.y = source.y;
    }
  
    // slope if infinity
    else if (m == std::numeric_limits<float>::max()) {
        a.x = source.x;
        a.y = source.y + l;
  
        b.x = source.x;
        b.y = source.y - l;
    }
  
    // normal case
    else {
        float dx = (l / sqrt(1 + (m * m)));
        float dy = m * dx;
        a.x = source.x + dx, a.y = source.y + dy;
        b.x = source.x - dx, b.y = source.y - dy;
    }
  
    return pair<Point, Point>(a, b);
}
  
// given two points, this function calculates
// the slope of the line/ passing through the
// points
float findSlope(Point p, Point q)
{
    if (p.y == q.y)
        return 0;
    if (p.x == q.x)
        return std::numeric_limits<float>::max();
    return (q.y - p.y) / (q.x - p.x);
}
  
// calculates the distance between two points
float findDistance(Point p, Point q)
{
    return sqrt(pow((q.x - p.x), 2) + pow((q.y - p.y), 2));
}
  
// given three points, it prints a point such
// that a parallelogram is formed
void findMissingPoint(Point a, Point b, Point c)
{
    // calculate points originating from a
    pair<Point, Point> d = findPoints(a, findSlope(b, c),
                                      findDistance(b, c));
  
    // now check which of the two points satisfy
    // the conditions
    if (findDistance(d.first, c) == findDistance(a, b))
        cout << d.first.x << ", " << d.first.y << endl;
    else
        cout << d.second.x << ", " << d.second.y << endl;
}
  
// Driver code
int main()
{
    findMissingPoint(Point(1, 0), Point(1, 1), Point(0, 1));
    findMissingPoint(Point(5, 0), Point(1, 1), Point(2, 5));
    return 0;
}


Output :

0, 0
6, 4

Alternative Approach:

Since the opposite sides are equal, AD = BC and AB = CD, we can calculate the co-ordinates of the missing point (D) as:

AD = BC
(Dx - Ax, Dy - Ay) = (Cx - Bx, Cy - By)
Dx = Ax + Cx - Bx 
Dy = Ay + Cy - By

References: https://math.stackexchange.com/questions/887095/find-the-4th-vertex-of-the-parallelogram

Below is the implementation of above approach:

C++

// C++ program to find missing point
// of a parallelogram
#include <bits/stdc++.h>
using namespace std;
  
// main method
int main()
{
   int ax = 5, ay = 0; //coordinates of A
   int bx = 1, by = 1; //coordinates of B
   int cx = 2, cy = 5; //coordinates of C
    cout << ax + cx - bx << ", "
         << ay + cy - by;
    return 0;
}

Java

// Java program to 
// find missing point
// of a parallelogram
import java.io.*;
  
class GFG 
{
public static void main (String[] args) 
{
  
    int ax = 5, ay = 0; //coordinates of A
    int bx = 1, by = 1; //coordinates of B
    int cx = 2, cy = 5; //coordinates of C
    System.out.println(ax + (cx - bx) + ", " +
                       ay + (cy - by));
}
}
  
// This code is contributed by m_kit

Python 3

# Python 3 program to find missing point
# of a parallelogram
  
# Main method
if __name__ == "__main__":
      
    # coordinates of A
      
    ax, ay = 5, 0
      
    # coordinates of B
      
    bx ,by = 1, 1
      
    # coordinates of C
      
    cx ,cy = 2, 5
      
    print(ax + cx - bx , ",", ay + cy - by)
  
# This code is contributed by Smitha

C#

// C# program to 
// find missing point
// of a parallelogram
using System;
  
class GFG
{
    static public void Main ()
    {
        int ax = 5, ay = 0; //coordinates of A
        int bx = 1, by = 1; //coordinates of B
        int cx = 2, cy = 5; //coordinates of C
        Console.WriteLine(ax + (cx - bx) + ", " +
                             ay + (cy - by));
    }
}
  
// This code is contributed by ajit

PHP

<?php
// PHP program to find missing
// point of a parallelogram
  
// Driver Code
$ax = 5;
$ay = 0; //coordinates of A
$bx = 1;
$by = 1; //coordinates of B
$cx = 2;
$cy = 5; //coordinates of C
echo $ax + $cx - $bx , ", ",
     $ay + $cy - $by;
  
// This code is contributed by aj_36
?>


Output:

6, 4

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter