Given three points p1, p2 and p3, the task is to determine the orientation of these three points.
Orientation of an ordered triplet of points in the plane can be
- counterclockwise
- clockwise
- collinear
The following diagram shows different possible orientations of (a,b,c)

If orientation of (p1, p2, p3) is collinear, then orientation of (p3, p2, p1) is also collinear.
If orientation of (p1, p2, p3) is clockwise, then orientation of (p3, p2, p1) is counterclockwise and vice versa is also true.
Example:
Input: p1 = {0, 0}, p2 = {4, 4}, p3 = {1, 2}
Output: CounterClockWise
Input: p1 = {0, 0}, p2 = {4, 4}, p3 = {1, 1}
Output: Collinear
How to compute Orientation?
The idea is to use slope.

Slope of line segment (p1, p2): ? = (y2 - y1)/(x2 - x1)
Slope of line segment (p2, p3): ? = (y3 - y2)/(x3 - x2)
If ? > ?, the orientation is clockwise (right turn)
Using above values of ? and ?, we can conclude that,
the orientation depends on sign of below expression:
(y2 - y1)*(x3 - x2) - (y3 - y2)*(x2 - x1)
Above expression is negative when ? < ?, i.e., counterclockwise
Below is the implementation of above idea.
C++
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
int orientation(Point p1, Point p2, Point p3)
{
int val = (p2.y - p1.y) * (p3.x - p2.x)
- (p2.x - p1.x) * (p3.y - p2.y);
if (val == 0)
return 0;
return (val > 0) ? 1 : 2;
}
int main()
{
Point p1 = { 0, 0 }, p2 = { 4, 4 }, p3 = { 1, 2 };
int o = orientation(p1, p2, p3);
if (o == 0)
cout << "Linear" ;
else if (o == 1)
cout << "Clockwise" ;
else
cout << "CounterClockwise" ;
cout << endl;
p1 = { 0, 0 }, p2 = { 4, 4 }, p3 = { 1, 1 };
o = orientation(p1, p2, p3);
if (o == 0)
cout << "Linear" ;
else if (o == 1)
cout << "Clockwise" ;
else
cout << "CounterClockwise" ;
cout << endl;
p1 = { 1, 2 }, p2 = { 4, 4 }, p3 = { 0, 0 };
o = orientation(p1, p2, p3);
if (o == 0)
cout << "Linear" ;
else if (o == 1)
cout << "Clockwise" ;
else
cout << "CounterClockwise" ;
return 0;
}
|
Java
class Point
{
int x, y;
Point( int x, int y){
this .x=x;
this .y=y;
}
}
class GFG {
public static int orientation(Point p1, Point p2,
Point p3)
{
int val = (p2.y - p1.y) * (p3.x - p2.x) -
(p2.x - p1.x) * (p3.y - p2.y);
if (val == 0 ) return 0 ;
return (val > 0 )? 1 : 2 ;
}
public static void main(String[] args)
{
Point p1 = new Point( 0 , 0 );
Point p2 = new Point( 4 , 4 );
Point p3 = new Point( 1 , 2 );
int o = orientation(p1, p2, p3);
if (o== 0 )
System.out.print( "Linear" );
else if (o == 1 )
System.out.print( "Clockwise" );
else
System.out.print( "CounterClockwise" );
}
}
|
Python3
class Point:
def __init__( self , x, y):
self .x = x
self .y = y
def orientation(p1, p2, p3):
val = ( float (p2.y - p1.y) * (p3.x - p2.x)) - \
( float (p2.x - p1.x) * (p3.y - p2.y))
if (val > 0 ):
return 1
elif (val < 0 ):
return 2
else :
return 0
p1 = Point( 0 , 0 )
p2 = Point( 4 , 4 )
p3 = Point( 1 , 2 )
o = orientation(p1, p2, p3)
if (o = = 0 ):
print ( "Linear" )
elif (o = = 1 ):
print ( "Clockwise" )
else :
print ( "CounterClockwise" )
|
C#
using System;
public class Point
{
public int x, y;
public Point( int x, int y)
{
this .x = x;
this .y = y;
}
}
class GFG
{
public static int orientation(Point p1, Point p2,
Point p3)
{
int val = (p2.y - p1.y) * (p3.x - p2.x) -
(p2.x - p1.x) * (p3.y - p2.y);
if (val == 0) return 0;
return (val > 0)? 1: 2;
}
<strong>
public static void Main(String[] args)
{
Point p1 = new Point(0, 0);
Point p2 = new Point(4, 4);
Point p3 = new Point(1, 2);
int o = orientation(p1, p2, p3);
if (o == 0)
Console.WriteLine( "Linear" );
else if (o == 1)
Console.WriteLine( "Clockwise" );
else
Console.WriteLine( "CounterClockwise" );
}
}
|
Javascript
<script>
class Point {
constructor(x, y) {
this .x = x;
this .y = y;
}
}
function orientation(p1, p2, p3) {
let val = (p2.y - p1.y) * (p3.x - p2.x) -
(p2.x - p1.x) * (p3.y - p2.y);
if (val == 0) return 0;
return (val > 0) ? 1 : 2;
}
let p1 = new Point(0, 0);
let p2 = new Point(4, 4);
let p3 = new Point(1, 2);
let o = orientation(p1, p2, p3);
if (o == 0)
document.write( "Linear" );
else if (o == 1)
document.write( "Clockwise" );
else
document.write( "CounterClockwise" );
</script>
|
Output
CounterClockwise
Linear
Clockwise
Time Complexity: O(1)
Auxiliary Space: O(1)
The concept of orientation is used in below articles:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Approach#2: Using slope
This approach checks the orientation of 3 ordered points in the plane by calculating the slopes of the line segments formed by the points. If the slopes are equal, then the points are collinear. If the slope of the line segment formed by the first two points is less than the slope of the line segment formed by the last two points, then the orientation is counter-clockwise, otherwise it is clockwise.
Algorithm
1. Calculate slope of lines formed by (p1,p2) and (p2,p3)
2. If slopes are equal, then points are collinear
3. If slope of (p1,p2) < slope of (p2,p3), then points are in counter clockwise orientation
4. If slope of (p1,p2) > slope of (p2,p3), then points are in clockwise orientation
C++
#include <iostream>
using namespace std;
string orientation( int p1[], int p2[], int p3[])
{
int slope1 = (p2[1] - p1[1]) * (p3[0] - p2[0]);
int slope2 = (p3[1] - p2[1]) * (p2[0] - p1[0]);
if (slope1 == slope2) {
return "Collinear" ;
}
else if (slope1 < slope2) {
return "CounterClockWise" ;
}
else {
return "ClockWise" ;
}
}
int main()
{
int p1[] = { 0, 0 };
int p2[] = { 4, 4 };
int p3[] = { 1, 1 };
cout << orientation(p1, p2, p3) << endl;
int p4[] = { 0, 0 };
int p5[] = { 4, 4 };
int p6[] = { 1, 2 };
cout << orientation(p4, p5, p6) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static String orientation( int p1[], int p2[], int p3[])
{
int slope1 = (p2[ 1 ] - p1[ 1 ]) * (p3[ 0 ] - p2[ 0 ]);
int slope2 = (p3[ 1 ] - p2[ 1 ]) * (p2[ 0 ] - p1[ 0 ]);
if (slope1 == slope2) {
return "Collinear" ;
}
else if (slope1 < slope2) {
return "CounterClockWise" ;
}
else {
return "ClockWise" ;
}
}
public static void main (String[] args) {
int p1[] = { 0 , 0 };
int p2[] = { 4 , 4 };
int p3[] = { 1 , 1 };
System.out.println(orientation(p1, p2, p3));
int p4[] = { 0 , 0 };
int p5[] = { 4 , 4 };
int p6[] = { 1 , 2 };
System.out.println(orientation(p4, p5, p6));
}
}
|
Python3
def orientation(p1, p2, p3):
slope1 = (p2[ 1 ] - p1[ 1 ]) * (p3[ 0 ] - p2[ 0 ])
slope2 = (p3[ 1 ] - p2[ 1 ]) * (p2[ 0 ] - p1[ 0 ])
if slope1 = = slope2:
return "Collinear"
elif slope1 < slope2:
return "CounterClockWise"
else :
return "ClockWise"
p1 = [ 0 , 0 ]
p2 = [ 4 , 4 ]
p3 = [ 1 , 1 ]
print (orientation(p1, p2, p3))
p1 = [ 0 , 0 ]
p2 = [ 4 , 4 ]
p3 = [ 1 , 2 ]
print (orientation(p1, p2, p3))
|
C#
using System;
class Program
{
static string Orientation( int [] p1, int [] p2, int [] p3)
{
int slope1 = (p2[1] - p1[1]) * (p3[0] - p2[0]);
int slope2 = (p3[1] - p2[1]) * (p2[0] - p1[0]);
if (slope1 == slope2)
{
return "Collinear" ;
}
else if (slope1 < slope2)
{
return "CounterClockWise" ;
}
else
{
return "ClockWise" ;
}
}
static void Main()
{
int [] p1 = { 0, 0 };
int [] p2 = { 4, 4 };
int [] p3 = { 1, 1 };
Console.WriteLine(Orientation(p1, p2, p3));
int [] p4 = { 0, 0 };
int [] p5 = { 4, 4 };
int [] p6 = { 1, 2 };
Console.WriteLine(Orientation(p4, p5, p6));
}
}
|
Javascript
function orientation(p1, p2, p3) {
let slope1 = (p2[1] - p1[1]) * (p3[0] - p2[0]);
let slope2 = (p3[1] - p2[1]) * (p2[0] - p1[0]);
if (slope1 == slope2) {
return "Collinear" ;
} else if (slope1 < slope2) {
return "CounterClockWise" ;
} else {
return "ClockWise" ;
}
}
let p1 = [0, 0];
let p2 = [4, 4];
let p3 = [1, 1];
console.log(orientation(p1, p2, p3));
p1 = [0, 0];
p2 = [4, 4];
p3 = [1, 2];
console.log(orientation(p1, p2, p3));
|
Output
Collinear
CounterClockWise
Time Complexity: O(1)
Space Complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Oct, 2023
Like Article
Save Article