# CSES Solutions – Point Location Test

Last Updated : 22 Apr, 2024

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, y2 = 3, x3 = 2, y3 = 3
Output: LEFT

Input: x1 = 1, y1 = 1, x2 = 5, y2 = 3, x3 = 4, y3 = 1
Output: RIGHT

Approach:

The cross product of two vectors a and b is a vector that is perpendicular to both a and b and thus normal to the plane containing them. The direction of the cross product vector is given by the right-hand rule, and its magnitude is equal to the area of the parallelogram that the vectors span.

But for this problem, we are not interested in the resulting vector itself, but rather the sign of its z-component (since we are working in 2D space, this is the only component of the cross product). The sign of the z-component of the cross product tells us whether p3 is to the left or right of the line from p1 to p2:

• If the cross product is positive, p3 is on the left side of the line.
• If the cross product is negative, p3 is on the right side of the line.
• If the cross product is zero, p3 is on the line.

Steps to solve the problem:

• Calculate the cross product using the formula: (x2 – x1) * (y3 – y1) – (y2 – y1) * (x3 – x1).
• If the cross product is less than 0, then point p3 is on the right side of the line passing through p1 and p2. Print “RIGHT“.
• If the cross product is greater than 0, then point p3 is on the left side of the line passing through p1 and p2. Print “LEFT“.
• If the cross product is equal to 0, then point p3 lies on the line passing through p1 and p2. Print “TOUCH“.

Below are the implementation of the above approach:

C++ ```#include <bits/stdc++.h> using namespace std; int main() { // Coordinates of points p1, p2, and p3 long long x1 = 1, y1 = 1, x2 = 5, y2 = 3, x3 = 2, y3 = 3; // Calculate the cross product to determine the position of p3 relative to the line through p1 and p2 long long crossProduct = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1); // If cross product is less than 0, p3 is on the right of the line if (crossProduct < 0) cout << "RIGHT" << endl; // If cross product is more than 0, p3 is on the left of the line else if (crossProduct > 0) cout << "LEFT" << endl; // If cross product is 0, p3 is on the line else cout << "TOUCH" << endl; return 0; } ``` Java ```import java.util.*; public class Main { public static void main(String[] args) { // Coordinates of points p1, p2, and p3 long x1 = 1, y1 = 1, x2 = 5, y2 = 3, x3 = 2, y3 = 3; // Calculate the cross product to determine the position of p3 relative to the line through p1 and p2 long crossProduct = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1); // If cross product is less than 0, p3 is on the right of the line if (crossProduct < 0) System.out.println("RIGHT"); // If cross product is more than 0, p3 is on the left of the line else if (crossProduct > 0) System.out.println("LEFT"); // If cross product is 0, p3 is on the line else System.out.println("TOUCH"); } } ``` Python3 ```# Python code to determine the position of a point relative to a line # Coordinates of points p1, p2, and p3 x1, y1 = 1, 1 x2, y2 = 5, 3 x3, y3 = 2, 3 # Calculate the cross product to determine the position of p3 relative to the line through p1 and p2 crossProduct = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1) # If cross product is less than 0, p3 is on the right of the line if crossProduct < 0: print("RIGHT") # If cross product is more than 0, p3 is on the left of the line elif crossProduct > 0: print("LEFT") # If cross product is 0, p3 is on the line else: print("TOUCH") ``` JavaScript ```function main() { // Coordinates of points p1, p2, and p3 let x1 = 1, y1 = 1, x2 = 5, y2 = 3, x3 = 2, y3 = 3; // Calculate the cross product to determine the position of p3 relative to the line through p1 and p2 let crossProduct = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1); // If cross product is less than 0, p3 is on the right of the line if (crossProduct < 0) console.log("RIGHT"); // If cross product is more than 0, p3 is on the left of the line else if (crossProduct > 0) console.log("LEFT"); // If cross product is 0, p3 is on the line else console.log("TOUCH"); } // Call the main function main(); ```

Output
```LEFT

```

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