Java Program to Find All Possible Coordinates of Parallelogram

In the previous article, we have seen Java Program to Find Circumference of a Parallelogram

In this article we are going to see how to find all possible coordinates of parallelogram using Java programming language.

Java Program to Find All Possible Coordinates of Parallelogram

Before Jumping into the program directly let’s see how we can find All Possible Coordinates of Parallelogram

Explanation:

Let us take 3 distinct points A, B, C randomly.

Where,
The coordinate of A is (ax,ay)
The coordinate of B is (bx,by) 
The coordinate of C is (cx,cy)

Now, in order to get a parallelogram ABCD we must know the coordinate of D

So, the possible coordinates of D to form the parallelogram ABCD are:
(ax+bx-cx), (ay+by-cy)
(ax+cx-bx), (ay+cy-by)
(cx+bx-ax), (cy+by-ay)

Example:

a = (1,2)
b = (3,4)
c = (5,6)

So the Possible coordinates of D to form a parallelogram ABCD are
(ax + bx - cx) , (ay + by - cy) = (1+3-5),(2+4-6) = -1,0
(ax + cx - bx) , (ay + cy - by) = (1+5-3),(2+6-4) = 3,4
(cx + bx - ax) , (cy + by - ay) = (5+3-1),(6+4-2) = 7,8

Let’s see the different ways to find all possible coordinates of parallelogram.

Method-1: Java Program to Find All Possible Coordinates of Parallelogram By Using Static Value

Approach:

  • Declare integer variable say “ax”, “ay”, “bx”, “by”, “cx”, “cy” assign the value to it, which holds the values for the coordinates of A, B, C.
  • Find the possible coordinates of the parallelogram using the formula.
  • Print the result.

Program:

class Main
{  
    public static void main(String[] args) 
    { 
        //coordinates of A 
        int ax = 5;
        int ay = 0;
        //coordinates of B 
        int bx = 1;
        int by = 1;
        //coordinates of C 
        int cx = 2;
        int cy = 5; 
        System.out.println("The possible coordinates are:");
        System.out.println((ax+bx-cx)+", "+(ay+by-cy)); 
        System.out.println((ax+cx-bx)+", "+(ay+cy-by)); 
        System.out.println((cx+bx-ax)+", "+(cy+by-ay));
    } 
} 
Output:

The possible coordinates are:
4, -4
6, 4
-2, 6

Method-2: Java Program to Find All Possible Coordinates of Parallelogram By Using User Input Value

Approach:

  • Declare integer variable say “ax”, “ay”, “bx”, “by”, “cx”, “cy” and take the values as user input, which holds the values for the coordinates of A, B, C.
  • Find the possible coordinates of the parallelogram using the formula.
  • Print the result.

Program:

import java.util.Scanner;
public class Main 
{
    public static void main(String [] args)
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the coordinate of A in the form of (ax,ay)");
        int ax = s.nextInt();
        int ay = s.nextInt();          //coordinates of A 
        System.out.println("Enter the coordinate of B in the form of (bx,by)");
        int bx = s.nextInt();
        int by = s.nextInt();         //coordinates of B 
        System.out.println("Enter the coordinate of C in the form of (cx,cy)");
        int cx = s.nextInt();
        int cy = s.nextInt();          //coordinates of C 
        System.out.println("The possible coordinates are: ");
        System.out.println((ax + bx - cx) +", "+ (ay + by - cy)); 
        System.out.println((ax + cx - bx) +", "+ (ay + cy - by)); 
        System.out.println((cx + bx - ax) +", "+ (cy + by - ay)); 
        System.out.println((ax + cx - bx) +", "+ (ay + cy - by));
    }
}
Output:

Enter the coordinate of A in the form of (ax,ay)
5 1
Enter the coordinate of B in the form of (bx,by)
2 5
Enter the coordinate of C in the form of (cx,cy)
5 2
The possible coordinates are: 
2, 4
8, -2
2, 6
8, -2

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Articles: