Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Geometric Algorithms
  • Basic Geometry
  • Computational Geometry
  • Slope of Line
  • Point of Intersection of Two Lines
  • Closest Pair of Points
  • Convex Hull
  • Pythagorean Quadruple
  • Polygon Triangulation
Open In App
Next Article:
Count Integral points inside a Triangle
Next article icon

Orientation of 3 ordered points

Last Updated : 23 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Approach:

The idea is to find the slopes of line segments formed by points [p1, p2] and [p2, p3] and calculate their cross product. Let the slope formed by point p1 & p2 be x, and the other one be y. Now there are three possibilities:

  • if x – y == 0: If the slopes formed by points are equal, it means that all three points lies in a straight line, and they are collinear.
  • if x – y > 0: If the slope formed by point [p1, p2] is greater than [p2, p3], it means that right turn structure is forming, thus it is clockwise orientation.
  • if x – y < 0: If the slope formed by point [p1, p2] is smaller than [p2, p3], it means that left turn structure is forming, thus it is counterclockwise orientation.Considering the above image, the slope of line segment formed by point p1 and p2 will be: x = (y2 – y1) / (x2 – x1).
    And the slope formed by line segment formed by point p2 and p3 will be:
    y = (y3-y2) / x3 – x2).
    The cross product of both the slopes will be (y2 – y1) * (x3 – x2) – (x2 – x1) * (y3 – y2);

Below is the implementation of above idea: 

C++
// C++ program to find the orientation // of the three points #include <bits/stdc++.h> using namespace std;  // Function to find the orientation void orientation(int x1, int y1, int x2,                  int y2, int x3, int y3) {      // orientation of an (x, y) triplet     int val = ((y2 - y1) * (x3 - x2)) -               ((x2 - x1) * (y3 - y2)) ;      if (val == 0)         cout << "Collinear";     else if (val > 0)         cout << "Clockwise";     else         cout << "CounterClockwise"; }  int main() {     int x1 = 0, y1 = 0, x2 = 4;      int y2 = 4, x3 = 1, y3 = 1;     orientation(x1, y1, x2, y2, x3, y3);     return 0; } 
Java
// Java program to find the orientation // of the three points class GfG {      // Function to find the orientation     static void orientation(int x1, int y1, int x2,                              int y2, int x3, int y3) {          // orientation of an (x, y) triplet         int val = ((y2 - y1) * (x3 - x2)) -                   ((x2 - x1) * (y3 - y2));          if (val == 0)             System.out.println("Collinear");         else if (val > 0)             System.out.println("Clockwise");         else             System.out.println("CounterClockwise");     }      public static void main(String[] args) {         int x1 = 0, y1 = 0, x2 = 4;         int y2 = 4, x3 = 1, y3 = 1;         orientation(x1, y1, x2, y2, x3, y3);     } } 
Python
# Python program to find the orientation # of the three points  # Function to find the orientation def orientation(x1, y1, x2, y2, x3, y3):      # orientation of an (x, y) triplet     val = ((y2 - y1) * (x3 - x2)) - \           ((x2 - x1) * (y3 - y2))      if val == 0:         print("Collinear")     elif val > 0:         print("Clockwise")     else:         print("CounterClockwise")  if __name__ == "__main__":     x1, y1, x2, y2, x3, y3 = 0, 0, 4, 4, 1, 1     orientation(x1, y1, x2, y2, x3, y3) 
C#
// C# program to find the orientation // of the three points using System;  class GfG {      // Function to find the orientation     static void orientation(int x1, int y1, int x2,                              int y2, int x3, int y3) {          // orientation of an (x, y) triplet         int val = ((y2 - y1) * (x3 - x2)) -                   ((x2 - x1) * (y3 - y2));          if (val == 0)             Console.WriteLine("Collinear");         else if (val > 0)             Console.WriteLine("Clockwise");         else             Console.WriteLine("CounterClockwise");     }      static void Main(string[] args) {         int x1 = 0, y1 = 0, x2 = 4;         int y2 = 4, x3 = 1, y3 = 1;         orientation(x1, y1, x2, y2, x3, y3);     } } 
JavaScript
// JavaScript program to find the orientation // of the three points  // Function to find the orientation function orientation(x1, y1, x2, y2, x3, y3) {      // orientation of an (x, y) triplet     const val = ((y2 - y1) * (x3 - x2)) -                 ((x2 - x1) * (y3 - y2));      if (val === 0)         console.log("Collinear");     else if (val > 0)         console.log("Clockwise");     else         console.log("CounterClockwise"); }  const x1 = 0, y1 = 0, x2 = 4, y2 = 4, x3 = 1, y3 = 1; orientation(x1, y1, x2, y2, x3, y3); 

Output
Collinear

Time Complexity: O(1)
Auxiliary Space: O(1)

The concept of orientation is used in below articles: 

  • Find Simple Closed Path for a given set of points
  • How to check if two given line segments intersect?
  • Convex Hull | Set 1 (Jarvis’s Algorithm or Wrapping)
  • Convex Hull | Set 2 (Graham Scan)


Next Article
Count Integral points inside a Triangle

R

Rajeev Agrawal
Improve
Article Tags :
  • DSA
  • Geometric
Practice Tags :
  • Geometric

Similar Reads

  • Check if given four points form a square
    Given coordinates of four points in a plane, find if the four points form a square or not. Examples: Input: p1 = { 20, 10 }, p2 = { 10, 20 }, p3 = { 20, 20 }, p4 = { 10, 10 }Output: YesExplanation: Input: p1 = { 20, 20 }, p2 = { 10, 20 }, p3 = { 20, 20 }, p4 = { 10, 10 }Output: No To check for squar
    10 min read
  • Find intersection point of lines inside a section
    Given N lines in two-dimensional space in y = mx + b form and a vertical section. We need to find out whether there is an intersection point inside the given section or not. Examples: In below diagram four lines are there, L1 : y = x + 2 L2 : y = -x + 7 L3 : y = -3 L4 : y = 2x – 7 and vertical secti
    9 min read
  • Rotation of a point about another point
    We have already discussed the rotation of a point P about the origin in the Set 1 and Set 2. The rotation of point P about origin with an angle ? in the anti-clockwise direction is given as under: Rotation of P about origin: P * polar(1.0, ?)Rotation of P about point Q Now, we have to rotate the poi
    8 min read
  • Count Integral points inside a Triangle
    Given three non-collinear integral points in XY plane, find the number of integral points inside the triangle formed by the three points. (A point in XY plane is said to be integral/lattice point if both its co-ordinates are integral). Example: Input: p = (0, 0), q = (0, 5) and r = (5,0) Output: 6Ex
    8 min read
  • Direction of a Point from a Line Segment
    Direction of given point P from a line segment simply means given the co-ordinates of a point P and line segment (say AB), and we have to determine the direction of point P from the line segment. That is whether the Point lies to the Right of Line Segment or to the Left of Line Segment. The point mi
    8 min read
  • Find Simple Closed Path for a given set of points
    Given a set of points, connect the dots without crossing. Example: Input: points[] = {(0, 3), (1, 1), (2, 2), (4, 4), (0, 0), (1, 2), (3, 1}, {3, 3}}; Output: Connecting points in following order would not cause any crossing {(0, 0), (3, 1), (1, 1), (2, 2), (3, 3), (4, 4), (1, 2), (0, 3)} We strongl
    11 min read
  • Section formula for 3 D
    Given two coordinates (x1, y1, z1) and (x2, y2, z2) in 3D, and m and n, find the co-ordinates that divides the line joining (x1, y1, Z1) and (x2, y2, Z2) in the ratio m : n. Examples:  Input : x1 = 2, y1 = -1, Z1 = 4, x2 = 4, y2 = 3, Z2 = 2, m = 2, n = 3 Output : (2.8, .6, 3.2)Explanation: co-ordina
    6 min read
  • 2D Transformation | Rotation of objects
    We have to rotate an object by a given angle about a given pivot point and print the new co-ordinates.Examples: Input : {(100, 100), (150, 200), (200, 200), (200, 150)} is to be rotated about (0, 0) by 90 degrees Output : (-100, 100), (-200, 150), (-200, 200), (-150, 200) Input : {(100, 100), (100,
    7 min read
  • Determine position of two points with respect to a 3D plane
    Given four integers a, b, c, and d, which represents the coefficient of the equation of the plane ax + by + cz + d = 0 and two integer coordinates (x1, y1, z1) and (x2, y2, z2), the task is to find whether both the points lie on the same side, or on different sides, or on the surface of the plane. E
    9 min read
  • CSES Solutions - Point Location Test
    There is a line that goes through the points p1 = (x1,y1) and p2 = (x2,y2). There is also a point p3 = (x3,y3). Your task is to determine whether p3 is located on the left or right side of the line or if it touches the line when we are looking from p1 to p2. Example: Input: x1 = 1, y1 = 1, x2 = 5, y
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences