
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Orientation of 3 Ordered Points in Java
In this article we are going to find the orientation of 3 ordered points. Here orientation means that the given points form clockwise, anticlockwise or collinear shape in space.
In the above diagram a, b, c are the three points which check the orientation of shape in space. We find the orientation of three given points by calculating the slope.
To calculate slope and to calculate the orientation of 3 ordered points.
Slope of line segment
Slope of line segment $(a,b):\theta=\left ( y_{b}-y_{a} \right )/\left ( x_{b} -x_{a}\right )$
Slope of line segment $(b,c):\phi=\left ( y_{c} -y_{b}\right )/(x_{c}-x_{b})$
And hence, the orientation depends on the following expression:
$$(y_{b}-y_{a})(x_{c}-x_{b})-(y_{c}-y_{b})(x_{b}-x_{a})\:Or\:(y2-y1)*(x3-x2)-(y3-y2)*(x2-x1)$$
i.e., whether it is positive, negative or
If the expression is zero, then ? = ?. Hence the orientation is collinear.
If the expression is negative, then ? < ?. Hence the orientation is anti-clockwise.
If the expression is positive, then ? > ?. Hence the orientation is clockwise.
Let's start!
To show you some instances
Instance-1
Suppose the 3 ordered points are (0,3), (4,2), (3,1)
After checking the orientation of 3 ordered points the result will be:
The given 3 points form: Clockwise
Instance-2
Suppose the 3 ordered points are (0,3), (1,2), (9,5)
After checking the orientation of 3 ordered points the result will be:
The given 3 points form: CounterClockwise
Instance-3
Suppose the 3 ordered points are (2,2), (3,3), (4,4)
After checking the orientation of 3 ordered points the result will be:
The given 3 points form: Linear
Algorithm
Step 1 ? Declare the 3 ordered points.
Step 2 ? Pass the three given points to the expression i.e. (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y).
Step 3 ? Check for the condition of linear, clockwise and counterclockwise.
Step 4 ? Print the result.
Multiple Approaches
We have provided the solution in different approaches.
By taking Static Input
By Using User Defined Method
Let's see the program along with its output one by one.
Approach-1: By Using Static Input
In the approach, first the 3 points will be passed to expression to check for the condition of linear, clockwise and counter-clockwise. And the result will be printed to output.
Example
public class Main{ //main method public static void main(String[] args){ //Declaring variables int x1=0, y1=1; int x2=4, y2=3; int x3=3, y3=2; //expression to check for 3 ordered point int val = (y2 - y1) * (x3 - x2) - (x2 - x1) * (y3 - y2); // check for collinear if (val == 0){ //printing collinear orientation System.out.print("The given 3 points form : Linear"); } //check for clockwise else if(val > 0){ //printing clockwise orientation System.out.print("The given 3 points form: Clockwise"); } else { //printig counter clockwise orientation System.out.print("The given 3 points form: CounterClockwise"); } } }
Output
The given 3 points form: Clockwise
Approach-2: By Using User Defined Method
In the approach, first the 3 points will be passed to expression using a user defined method to check for the condition of linear, clockwise and counter-clockwise. And the result will be printed to output.
Example
public class Main { public static void main(String[] args){ Point a = new Point(2, 2); Point b = new Point(3, 3); Point c = new Point(4, 4); //calling user defined method int o = orientation(a, b, c); //check for Linear orientation if (o==0) //printing Linear orientation System.out.print("The given 3 points form : Linear"); //check for Clockwise orientation else if (o == 1) //printing clockwise orientation System.out.print("The given 3 points form : Clockwise"); else //printing counter clockwise orientation System.out.print("The given 3 points form : CounterClockwise"); } // user defined method public static int orientation(Point a, Point b, Point c){ //expression to check for 3 ordered point int val = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y); // check for collinear if (val == 0) return 0; // check for clock or counterclock wise return (val > 0)? 1: 2; } } class Point{ int x, y; Point(int x,int y){ this.x=x; this.y=y; } }
Output
The given 3 points form : Linear
In this article, we explored how to check orientation of 3 ordered points by using Java programming language.