# 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 might lie behind the line segment, in that case we assume imaginary line by extending the line segment and determine the direction of point.

* There are only three cases, either the point is on left side, or right side or on the line segment itself.

This is a very fundamental Problem and is commonly encountered for directions in **online map**,

**Example** : Suppose a user A has to go to Point C in the following map, the user first reaches point B but after that how does user A know that whether he has to make a right turn or left turn.

Knowing the direction of a point from a line segment also acts a building block to solve more complicated problem such as :

**Line segment Intersection**: finding if two line segment intersect**Convex Hull**of a set of Points.

The co-ordinate system we’ll use is a cartesian plane, as most 2-Dimensional problem uses cartesian plane and since this is a 2-Dimensional Problem.

This Problem can be solved using **cross-product** of vector algebra

Cross-Product of two point A and B is : **A _{x} * B_{y} – A_{y} * B_{x}**

where A

_{x}and A

_{y}are x and y co-ordinates of A respectively. Similarly B

_{x}and B

_{y}are x and y co-ordinates of B respectively.

The **Cross-Product** has an interesting Property which will be used to determine direction of a point from a line segment. That is, the cross-product of two points is positive if and only if the angle of those point at origin (0, 0) is in counter-clockwise. And conversely the cross-product is negative if and only if the angle of those point at origin is in clockwise direction.

**An example would certainly clarify it**,

In the following figure, the angle BOP is anti-clockwise and the cross-product of B X P = 29*28 – 15*(-15) = 1037 which is positive.

This helps us to make a conclusion that a point on right side must a have positive cross-product and a point on left side must have a negative cross product. Also note that we assumed one point of line segment to be origin and hence we need to convert any three point system such that one point of line segment is origin.

**Following example explains the concept** :

The three point A, B and P were converted into **A’, B’** and **P’** so as to make A as origin (this can be simply done by subtracting co-ordinates of A from point P and B), and then calculate the cross-product : 59*18 – (-25)*18 = 2187

Since this is positive, the Point P is on right side of line Segment AB.

## C++

`// C++ Program to Determine Direction of Point ` `// from line segment ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// structure for point in cartesian plane. ` `struct` `point { ` ` ` `int` `x, y; ` `}; ` ` ` `// constant integers for directions ` `const` `int` `RIGHT = 1, LEFT = -1, ZERO = 0; ` ` ` `int` `directionOfPoint(point A, point B, point P) ` `{ ` ` ` `// subtracting co-ordinates of point A from ` ` ` `// B and P, to make A as origin ` ` ` `B.x -= A.x; ` ` ` `B.y -= A.y; ` ` ` `P.x -= A.x; ` ` ` `P.y -= A.y; ` ` ` ` ` `// Determining cross Product ` ` ` `int` `cross_product = B.x * P.y - B.y * P.x; ` ` ` ` ` `// return RIGHT if cross product is positive ` ` ` `if` `(cross_product > 0) ` ` ` `return` `RIGHT; ` ` ` ` ` `// return LEFT if cross product is negative ` ` ` `if` `(cross_product < 0) ` ` ` `return` `LEFT; ` ` ` ` ` `// return ZERO if cross product is zero. ` ` ` `return` `ZERO; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `point A, B, P; ` ` ` `A.x = -30; ` ` ` `A.y = 10; ` `// A(-30, 10) ` ` ` `B.x = 29; ` ` ` `B.y = -15; ` `// B(29, -15) ` ` ` `P.x = 15; ` ` ` `P.y = 28; ` `// P(15, 28) ` ` ` ` ` `int` `direction = directionOfPoint(A, B, P); ` ` ` `if` `(direction == 1) ` ` ` `cout << ` `"Right Direction"` `<< endl; ` ` ` `else` `if` `(direction == -1) ` ` ` `cout << ` `"Left Direction"` `<< endl; ` ` ` `else` ` ` `cout << ` `"Point is on the Line"` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to Determine Direction of Point ` `// from line segment ` `class` `GFG ` `{ ` ` ` `// structure for point in cartesian plane. ` `static` `class` `point ` `{ ` ` ` `int` `x, y; ` `}; ` ` ` `// constant integers for directions ` `static` `int` `RIGHT = ` `1` `, LEFT = -` `1` `, ZERO = ` `0` `; ` ` ` `static` `int` `directionOfPoint(point A, ` ` ` `point B, point P) ` `{ ` ` ` `// subtracting co-ordinates of point A ` ` ` `// from B and P, to make A as origin ` ` ` `B.x -= A.x; ` ` ` `B.y -= A.y; ` ` ` `P.x -= A.x; ` ` ` `P.y -= A.y; ` ` ` ` ` `// Determining cross Product ` ` ` `int` `cross_product = B.x * P.y - B.y * P.x; ` ` ` ` ` `// return RIGHT if cross product is positive ` ` ` `if` `(cross_product > ` `0` `) ` ` ` `return` `RIGHT; ` ` ` ` ` `// return LEFT if cross product is negative ` ` ` `if` `(cross_product < ` `0` `) ` ` ` `return` `LEFT; ` ` ` ` ` `// return ZERO if cross product is zero. ` ` ` `return` `ZERO; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `point A = ` `new` `point(), ` ` ` `B = ` `new` `point(), P = ` `new` `point(); ` ` ` `A.x = -` `30` `; ` ` ` `A.y = ` `10` `; ` `// A(-30, 10) ` ` ` `B.x = ` `29` `; ` ` ` `B.y = -` `15` `; ` `// B(29, -15) ` ` ` `P.x = ` `15` `; ` ` ` `P.y = ` `28` `; ` `// P(15, 28) ` ` ` ` ` `int` `direction = directionOfPoint(A, B, P); ` ` ` `if` `(direction == ` `1` `) ` ` ` `System.out.println(` `"Right Direction"` `); ` ` ` `else` `if` `(direction == -` `1` `) ` ` ` `System.out.println(` `"Left Direction"` `); ` ` ` `else` ` ` `System.out.println(` `"Point is on the Line"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed ` `// by Princi Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to Determine Direction of Point ` `// from line segment ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// structure for point in cartesian plane. ` `public` `class` `point ` `{ ` ` ` `public` `int` `x, y; ` `}; ` ` ` `// constant integers for directions ` `static` `int` `RIGHT = 1, LEFT = -1, ZERO = 0; ` ` ` `static` `int` `directionOfPoint(point A, ` ` ` `point B, point P) ` `{ ` ` ` `// subtracting co-ordinates of point A ` ` ` `// from B and P, to make A as origin ` ` ` `B.x -= A.x; ` ` ` `B.y -= A.y; ` ` ` `P.x -= A.x; ` ` ` `P.y -= A.y; ` ` ` ` ` `// Determining cross Product ` ` ` `int` `cross_product = B.x * P.y - B.y * P.x; ` ` ` ` ` `// return RIGHT if cross product is positive ` ` ` `if` `(cross_product > 0) ` ` ` `return` `RIGHT; ` ` ` ` ` `// return LEFT if cross product is negative ` ` ` `if` `(cross_product < 0) ` ` ` `return` `LEFT; ` ` ` ` ` `// return ZERO if cross product is zero. ` ` ` `return` `ZERO; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `point A = ` `new` `point(), ` ` ` `B = ` `new` `point(), ` ` ` `P = ` `new` `point(); ` ` ` `A.x = -30; ` ` ` `A.y = 10; ` `// A(-30, 10) ` ` ` `B.x = 29; ` ` ` `B.y = -15; ` `// B(29, -15) ` ` ` `P.x = 15; ` ` ` `P.y = 28; ` `// P(15, 28) ` ` ` ` ` `int` `direction = directionOfPoint(A, B, P); ` ` ` `if` `(direction == 1) ` ` ` `Console.WriteLine(` `"Right Direction"` `); ` ` ` `else` `if` `(direction == -1) ` ` ` `Console.WriteLine(` `"Left Direction"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"Point is on the Line"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

Right Direction

This article is contributed by **Shubham Rana**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find an Integer point on a line segment with given two ends
- Find middle point segment from given segment lengths
- Check if it is possible to draw a straight line with the given direction cosines
- Find the other end point of a line with given one end and mid
- Reflection of a point about a line in C++
- Check whether the point (x, y) lies on a given line
- Program to find the mid-point of a line
- Perpendicular distance between a point and a Line in 2 D
- Shortest distance between a Line and a Point in a 3-D plane
- Find foot of perpendicular from a point in 2 D plane to a Line
- Section formula (Point that divides a line in given ratio)
- Reflection of a point at 180 degree rotation of another point
- Rotation of a point about another point in C++
- Slope of the line parallel to the line with the given slope
- Program to find area of a Circular Segment
- Mirror of a point through a 3 D plane
- Distance between a point and a Plane in 3 D
- Triangle with no point inside
- Chain Code for 2D Line
- Slope of perpendicular to line